How to detect escape key press with pure JS or jQuery?
问题 Possible Duplicate: Which keycode for escape key with jQuery How to detect escape key press in IE, Firefox and Chrome? Below code works in IE and alerts 27 , but in Firefox it alerts 0 $(\'body\').keypress(function(e){ alert(e.which); if(e.which == 27){ // Close my modal window } }); 回答1: Note: keyCode is getting deprecated use key instead. function keyPress (e) { if(e.key === "Escape") { // write your logic here. } } Here is jsfiddle keyCode is Getting deprecated It seems keydown and keyup