How to handle ESC keydown on javascript popup window
I have a javascript window.open popup, and I want the popup to close itself when the user presses the ESC key. I can't figure out how to hook the keydown event (and on what object?) so that I can catch the ESC key. I'm using jQuery. Try something like this: $(document).keydown(function(e) { // ESCAPE key pressed if (e.keyCode == 27) { window.close(); } }); user3874938 It is possible to achieve with JS Without using jQuery. window.onkeydown = function( event ) { if ( event.keyCode == 27 ) { console.log( 'escape pressed' ); } }; event.key === "Escape" No more arbitrary number codes! document