Naota
Naota

Reputation: 668

How to make a div visible when user presses a certain key

Can't think of how to do this, I wan't a div element to unhide itself when the user presses a key on the keyboard, like instead of hovering a button or something.

Is this possible? thinking jquery?

Upvotes: 1

Views: 376

Answers (2)

Alnitak
Alnitak

Reputation: 339816

$(document).keypress(function(ev) {
    if (ev.which === 65 || ev.which === 97) { // 'A' or 'a'
         $('#mydiv').toggle();
    }
});

See http://jsfiddle.net/alnitak/R4rWn/

If you wanted to capture a control key (ctrl, alt, caps-lock, etc) then you have to use .keydown() instead.

Upvotes: 7

Chandu
Chandu

Reputation: 82903

Try this:

$(document).bind("keypress", function(e){
 if(e.which == <YOUR_KEY_CODE>) { 
   //Do something
 }
});

Check this link to get a list of the keycode values.

Upvotes: 2

Related Questions