Key code for common characters on international keyboards

Well, grabbing key codes from special characters gives different results on keyboards with different layouts. But what about "common" characters like az? If you have a QWERTY keyboard, you will get key code 81 when you type q . When you have an AZERTY keyboard, you get code 81 when you press a , since a where q should be? Or is another mapping performed?

EDIT:

The answer I accepted is probably the best solution when you grab the keys and want to be sure that β€œa” is really β€œa”, but as I explain in the comment below, I'm still curious how the key codes are β€œtranslated” when using the built-in keyboards. That is: sources suggest that at least az should be consistent, but I cannot find support for this (or the one who really tried).

+2
source share
1 answer

If you use the keypress event rather than keyup or keydown , the problem disappears, because in this case you get character codes, not key codes.

Example:

 document.onkeypress = function(e) { e = e || window.event; var charCode = (typeof e.which == "undefined") ? e.keyCode : e.which; alert( String.fromCharCode(charCode) ); }; 

And here is the final key processing resource in JavaScript: http://unixpapa.com/js/key.html

+1
source

Source: https://habr.com/ru/post/954932/


All Articles