jQuery's 'keypress' doesn't work for some keys in Chrome. How to work around?
Solution 1
Try handling keydown
instead.
Solution 2
use keydown. keypress doesn't work with ESC in Chrome (not sure about other browsers).
$(newTag).keydown(function(e) { //keypress did not work with ESC;
if (event.which == '13') {
ProfilePage.saveNewTag(search_id, $(newTag).val());
}
else if (window.event.which){
$(newTag).remove();
}
});
Solution 3
For ESC key:
$(document).keydown(function(e) {
if(e.keyCode == 27) { /* Run code */ }
}
For letter keys, like 'L':
$(document).keypress(function(e) {
if(e.which == 108) { }
});
Works in both Chrome and Firefox
Solution 4
After the second alert add also
e.preventDefault();
This will prevent the default action of the event to be triggered.
More info about this method here
Your code should look like
$("body").keypress(function(e) {
alert("any key pressed");
if (e.keyCode == 27) {
alert("escape pressed");
e.preventDefault();
}});
Solution 5
keypress 'ESC'
e.which: 0
e.keyCode: 27
keyup 'ESC'
e.which: 27
e.keyCode: 27
For non-printable characters better use keyup
.
![DaveDev](https://i.stack.imgur.com/NQuFs.jpg?s=256&g=1)
DaveDev
Updated on July 05, 2022Comments
-
DaveDev about 2 years
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 secondalert
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?
-
Kato about 12 yearsThe trick to this answer, which I think is very important to note, is that "try handling
keydown
" is not a general purpose solution, but specific to "some keys". You still needkeypress
to handle the ascii characters correctly. So it's really a (muddled) multi-step process. -
Gustus over 9 yearsThank you for mentioning the Chrome bug. It will be helpful to those wondering why keypress is actually working for them in other browsers.
-
Max Strater over 9 yearsDoes anyone know why this is implemented as such?
-
SLaks over 9 years@MaxStrater:
keypress
is only for actual characters. -
Daniel Tonon almost 9 years
keyup
is slightly better for replicatingkeypress
functionality since it doesn't fire until the user releases the key. -
SLaks almost 9 years@DanT: On the contrary. If he wants to replicate
keypress
, he'll want the repeatedkeydown
s. -
DimaSan over 7 yearsWhile this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
-
Mark Baijens over 6 yearsNote: keydown fires multiple times when the key is hold down. While keypress should only fire once when pressing a key. They are not the same.
-
pouya almost 6 yearsneither
keypress
norkeyup
doesn't work for ESC key on Chromium 69.0.3497.92. on linux.keydown
works flawlessly