开发者

Hotkeys to redirect to different page

开发者 https://www.devze.com 2023-03-27 11:01 出处:网络
I had a simple script that when you pressed C you\'d be redirected to a different page, same for other letters.

I had a simple script that when you pressed C you'd be redirected to a different page, same for other letters. However, every time you would press the button this would happen, even if you were typing text into an input text field.

Can anyone wr开发者_运维技巧ite a lightweight script to allow me to make multiple hotkeys without them working in input fields or can show me where I can find documented explanations for this?


Okay, so I have the hotkey working, just can't make it stop.

$("input.registerform").keypress(function(e){

   e.stopPropagation();
});

Here is what I have to make it stop, the class of my input form is registerform bgcolor2 but it won't work with input.registerform, nor with input.registerform bgcolor2.

I tried adding an ID to it with registerform as ID; didn't work either :/

Is it being caused by my AJAX? or am I missing something here?


Try this

    $(document).keypress(function(e){

       if(e.which == 13){
      //Enter key is press do what you want
   }
   else if(e.which == 67 || e.which == 99){
      //C key is press do what you want

      //window.location.href = "urlToRedirect";
       alert("C pressed");
   }
    else if(e.which == 32){
        alert("Space pressed");

    }
       //Similarly check for as many conditions you want for different keys(ascii code)

    });

    //To supress this behavior do not happen in input field stop the event propagation

    $("input").keypress(function(e){
       //code goes here
       e.stopPropagation();
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号