views:

964

answers:

2

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.

+4  A: 

Try something like this:

$(document).keydown(function(e) {
    // ESCAPE key pressed
    if (e.keyCode == 27) {
        window.close();
    }
});
Gumbo
A: 

Remember that you must use the function @Gumbo posted in the popup-window... So you will need to include JQuery in the popup and execute the function there, not the window that opens the popup.

PatrikAkerstrand
What is Gumbo? (meaningless spacing for Stackoverflow)
Andrew Arnott
Machine is referring to the function that Gumbo posted (that you accepted).
Darryl Hein