As per usual, the always brilliant jQuery javascript library makes it particularly easy to detect and act on a keyboard button press action. For this particular snippet we are interesting in binding our function to the user hitting the Escape (ESC) on their keyboard.
To achieve this, we make use of jQuery’s keyup listener method and bind it to the document itself. Then making use of the useful keyCode parameter passed in by the browser, we simply check for the code associated with the ESC key and act.
In practice:
$(document).keyup(function(e) { if (e.keyCode == 27) { alert('Esc key pressed!'); } });
Simple and effective.