AnApprentice
AnApprentice

Reputation: 110970

js-hotkeys - how to bind to the ? question mark

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.

Upvotes: 6

Views: 3635

Answers (4)

CleverPatrick
CleverPatrick

Reputation: 9483

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

shift+/

Upvotes: 0

Levi Morrison
Levi Morrison

Reputation: 19552

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.

Upvotes: 2

zzzzBov
zzzzBov

Reputation: 179046

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');
  }
});

Upvotes: 3

Igor
Igor

Reputation: 1486

What about

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

Upvotes: 5

Related Questions