I have the following code:
$('.field').keypress(function(event) {
if (event.keyCode == 27) alert(event.keyCode);
});
I need to catch “Escape” pressing and do some actions. But now it code does’t work. I have tried to use this code:
$('.field').keypress(function(event) {
alert(event.keyCode);
});
I was pressing by Escape button, but it didn’t work (I haven’t seen any alerts). Please, tell me, how can I fix it?
Use
keydownorkeyuprather thankeypress.keypressonly fires for keystrokes that result in characters, which (by convention) Esc doesn’t (even though some charsets, such as ASCII, do have a character called “escape”).For more about handling keystrokes in JavaScript: JavaScript Madness: Keyboard Events