Alex Rivera | Logout

Which keycode for escape key with jQuery

Asked 2009-07-21T15:39:25.240
591

I have two functions. When enter is pressed the functions runs correctly but when escape is pressed it doesn't. What's the correct number for the escape key?

$(document).keypress(function(e) { 
    if (e.which == 13) $('.save').click();   // enter (works as expected)
    if (e.which == 27) $('.cancel').click(); // esc   (does not work)
});
Edit
Report

3 Answers

27

To find the keycode for any key, use this simple function:

document.onkeydown = function(evt) {
    console.log(evt.keyCode);
}
answered 2012-08-13T07:35:43.313
7

To get the hex code for all the characters: http://asciitable.com/

answered 2009-07-21T17:24:31.143
5

I have always used keyup and e.which to catch escape key.

answered 2012-02-03T08:57:22.267

Your Answer