Javascript: different keyCodes on different browsers?
So I've seen some forums posts about different browse开发者_高级运维rs reporting differenct keyCodes, but everyone seems so avoid the "why?".
I was trying to capture the colon (:) keyCode and realized that Firefox reports back e.keyCode 56. While Chrome reports back 186 (I think that's what it was).
Is there a univeral way of getting the right keyCode across all browsers?
And why are they different if they are the same keys?
I would be more curious as to whether there is a international way of getting the same key press.
Thanks.
It depends whether you're interested in which physical key the user has pressed or which character the user has typed. If it's the character you're after, you can get that reliably in all major browsers (using the keypress
event's which
property in most browsers or keyCode
in IE <= 8), but only in the keypress
event. If you're after the key, use the keydown
or keyup
event and examine the keyCode
property, although the exact key-code mappings do differ somewhat between browsers.
An excellent explanation of and reference for all JavaScript key-related events can be found at http://unixpapa.com/js/key.html.
To detect the user typing a colon character reliably in all the major browsers, you could do the following:
document.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (charCode && String.fromCharCode(charCode) == ":") {
alert("Colon!");
}
};
See http://unixpapa.com/js/key.html for an explanation why they have different keys. I do not know of an international way to match keys.
This is an old question. The modern way to do this is use event.key. See MDN Key
2022 - event.key
gives the "logical" key, event.code
gives the "physical" key
Some examples tested on MacOS Firefox, Windows Edge & Windows Chrome:
event.key |
event.code |
---|---|
"Enter" | "Enter" |
"Enter" | "NumpadEnter" |
"8" | "Digit8" |
"8" | "Numpad8" |
"/" | "Slash" |
"/" | "NumpadDivide" |
edge cases
- Windows Chrome/Edge, when pressing
ctrl+Enter
theevent.key
is"\n"
I think you should make JavaScript to get the keycode of the ':' character, so the script will know what is it in a certain environment. Similar question had been asked here, in stackoverflow.
Have also a look at this GitHub file: https://github.com/bpeacock/key-to-charCode/blob/master/jQuery.getChar.js on how you can use keyDown instead of keyPress events.
I used this for a barcode scanner with a keyboard wedge, on a mobile device, that had a bug on returning (keyPress) data for scanning a hyphen. Works pretty good. Except when I tested the app in a browser with a regular keyboard, I noticed that the hyphen works on Chrome, but not on Firefox. Strange but true. Fixed by adding code 173 in the above JS file, in addition to code 189.
This makes me wonder what the keyboard is actually sending. The keydown code of 173 or 189 for pressing the hyphen key (- _) is apparently not sent by the keyboard itself, but created by the browser that sends the keyDown event to my javascript application.
精彩评论