6

I've been using js-hotkeys for a while, love it.

I'd now like to bind to the ? key, but that doesn't appear to be supported. Anyone know why and how to bind to the ? question mark?

$(document).bind('keydown', '?',function (evt) { alert('go'); }); 

The above code does not work.

4 Answers 4

5

What about

$(document).bind('keyup', function (evt) { if (evt.keyCode == 191) alert("go"); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

This only works on QWERTY keyboards because "?" is on the "/" key. When you hit "?", it triggers key code 191, which is "/".
3

I believe the event has a flag for whether the shift key was pressed, so you probably want to do something like this (i've never used js-hotkeys, so I may be completely wrong):

$(document).bind('keydown', '/', function (evt) { if (evt.shiftKey) //or whatever the flag for the shift key may be { alert('go'); } }); 

5 Comments

of course, this also assumes that the ? is the alternative on the / key. If you're using semantic shortcuts, you're probably better off listening for the correct keyCode
i don't get it. You are saying I should use the shift key + ?
I'm saying you should bind it to the shift key plus the / key as that's what creates a ? on a standard qwerty keyboard.
what if the user isn't using a standard qwerty keyboard?
Oh, nevermind, I see that was your own comment already pointing that out.
2

Beware that the following will trigger even inside of an input box:

$(document).bind('keyup', function (evt) { if (evt.keyCode == 191) alert("go"); });

Solution:

$(document).bind('keyup', function(e) { if(e.keyCode === 191 && !$(e.target).is("input")) alert("go"); });

Keep in mind that the same thing will happen for texarea.

Comments

0

Using js-hotkeys, you would bind the Question Mark using the string:

shift+/ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.