Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture an 'Escape' Keypress in WebKit Browsers

I have a dialogue box which appears, and I would like it to be hidden again when you hit the escape key.

That all works fine in IE, Firefox, Opera - the code registers a document.onkeypress event handler when the box is shown, then tests the event keyCode to see if it's the escape key or not (keyCode 27), but this doesn't seen to work in WebKit based browsers (Chrome and Safari (3) ), though other keys can be captured this way.

Do WebKit browsers not to trigger an event at all when escape is pressed?

Does anyone know why this doesn't work, how to fix it, or know of a work-around.

like image 667
PeterJCLaw Avatar asked Apr 05 '09 19:04

PeterJCLaw


1 Answers

Catch keydown/keyup instead. Take a look at this entry for more information.

like image 82
Nicholas Riley Avatar answered Oct 08 '22 12:10

Nicholas Riley