I'm trying to implement key-press functionality which will remove a div when the user hits Esc
. This works for Firefox & IE with the following code:
$("body").keypress(function(e) {
alert("any key pressed");
if (e.keyCode == 27) {
alert("escape pressed");
}
});
If I hit any key, the first alert
is displayed, and if I hit Escape, the second alert
is also displayed.
This doesn't work with Chrome though. The first alert
is always displayed if I hit any of the letter keys, but not when I hit Escape, Tab, Space or any of the numbers.
Why would this be? Is there any way to get Chrome to respond to these key presses?
keypress 'ESC'
keyup 'ESC'
For non-printable characters better use
keyup
.After the second alert add also
This will prevent the default action of the event to be triggered.
More info about this method here
Your code should look like
Using Jquery.hotkey js file you can Make Sortcut key
Try handling
keydown
instead.use keydown. keypress doesn't work with ESC in Chrome (not sure about other browsers).
For ESC key:
For letter keys, like 'L':
Works in both Chrome and Firefox