Html 如果使用SHIFT,如何获取字符的keyCode?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18804348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get keyCode of character if SHIFT is used?
提问by Rajesh Paul
If a character is entered through keyboard using the SHIFTkeythen the keydownor keyupevent only can trace the keyCodeof SHIFTkeyi.e. 16.
如果一个角色是通过使用键盘输入SHIFT键,则keydown或keyup事件只能跟踪keyCode的SHIFT关键,即16。
Then how to trace the keyCodeof the character actually printed?
那么如何追踪实际打印keyCode的字符呢?
I am using the following code-
我正在使用以下代码-
onkeyup='alert(event.keyCode)';// Always shows a message 16(keyCode of SHIFT) irrespective of the actual character
or
或者
onkeydown='alert(event.keyCode)';// Always shows a message 16(keyCode of SHIFT) irrespective of the actual character
Then how to get keyCodeof the actual character printed???
那么如何获得keyCode打印的实际字符???
采纳答案by Rajesh Paul
Got the solution
得到了解决方案
I had to use onKeyPressevent which does not treatSHIFTas keypress but the resultant character instead.
我不得不使用不视为按键而是结果字符的onKeyPress事件。SHIFT
Hence I can get the keyCodeof the actual resultant character using onKeyPressevent.
因此,我可以keyCode使用onKeyPress事件获得实际结果字符的。
Syntax:
句法:
onkeypress='alert(event.keyCode)';
Now If I press SHIFT+Ait prompts the keyCode of Ai.e. 65.
现在,如果我按SHIFT+A它会提示Aie 65的 keyCode 。
回答by Artyom Neustroev
You can see the property shiftKeyof the passed event object.
可以看到shiftKey传递的事件对象的属性。
Take a look at this Fiddle
看看这个小提琴
When you press Shiftyou can see the keyCode of it and shiftKeyproperty true. Press any button together, i.e. Shift+Aand the console outputs:
当你按下Shift你可以看到它的 keyCode 和shiftKeyproperty true。同时按下任意按钮,即Shift+A和控制台输出:
65
true
65
真
Which is the code of Aand shiftKeyproperty again.
又是A和shiftKey属性的代码。

