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.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

4 Answers4

5

What about

$(document).bind('keyup', function (evt) {
    if (evt.keyCode == 191)
       alert("go");
});
Igor
  • 1,476
  • 10
  • 6
  • 4
    This only works on QWERTY keyboards because "?" is on the "/" key. When you hit "?", it triggers key code 191, which is "/". – nickh Oct 17 '11 at 16:42
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');
  }
});
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    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` – zzzzBov Jan 06 '11 at 22:18
  • i don't get it. You are saying I should use the shift key + ? – AnApprentice Jan 07 '11 at 03:47
  • 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. – zzzzBov Jan 07 '11 at 20:33
  • 2
    what if the user isn't using a standard qwerty keyboard? – Davy8 Mar 25 '11 at 21:09
  • Oh, nevermind, I see that was your own comment already pointing that out. – Davy8 Mar 25 '11 at 21:10
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.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
0

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

shift+/
CleverPatrick
  • 9,261
  • 5
  • 63
  • 86