国际键盘上常见字符的关键代码 [英] Key code for common characters on international keyboards
问题描述
好的,从特殊符号中捕获关键代码会在具有不同布局的键盘上产生不同的结果。但是像a-z这样的'常见'字符呢?如果您有QWERTY键盘,当您键入 q
时,您将获得密钥代码 81
。当你有一个AZERTY键盘时,当你按 a
时,你会得到代码 81
,因为 a
是 q
'应该'的地方?或者映射是否有所不同?
Ok, capturing key codes from special symbols produces different results on keyboards with different layouts. But how about the 'common' characters, like a-z? If you have a QWERTY-keyboard, you'd get key code 81
when you type q
. When you have an AZERTY-keyboard, do you get code 81
when you press a
, since a
is where q
'should' be? Or is the mapping done differently?
编辑:
我接受的答案当你捕获键并且想要确定'a'确实是'a'时,这可能是最好的解决方案,但正如我在下面的评论中解释的那样,我仍然很好奇使用int时关键代码是如何翻译的键盘。那就是:消息来源建议至少az应该是一致的,但我找不到支持(或实际尝试过的人)。
The answer I accepted is probably the best solution when you're capturing keys and want to be sure 'a' is really 'a', but as I explain in the comment underneath that, I still am curious how the key codes are 'translated' when using int'l keyboards. That is: sources suggest at least a-z should be consistent, but I cannot find support for this (or someone who actually tried).
推荐答案
如果您使用 keypress
事件而不是 keyup
或 keydown
然后问题就消失了,因为在那种情况下你会得到字符代码而不是密码。
If you use the keypress
event rather than keyup
or keydown
then the problem goes away because in that event you get character codes rather than key codes.
示例:
document.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "undefined") ? e.keyCode : e.which;
alert( String.fromCharCode(charCode) );
};
这里是JavaScript中关键处理的权威资源: http://unixpapa.com/js/key.html
And here's the definitive resource on key handling in JavaScript: http://unixpapa.com/js/key.html
这篇关于国际键盘上常见字符的关键代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!