如何区分“Enter”和“Enter”和“返回” Javascript 中的键?
某些桌面应用程序以不同方式对待“回车”键和数字键盘的“输入”键。我注意到这两个键在 Javascript (jQuery) 中生成相同的 keyCode (13)。
它们在浏览器环境中是否转换为相等,或者是否可以区分它们(即,使 CR 在文本区域中换行,然后按“回车”键提交其表单?
Some desktop apps treat the 'carriage return' key and the numpad's 'enter' key differently. I've noticed that these two keys generate the same keyCode (13) in Javascript (jQuery).
Are they converted to be equal in the browser environment, or is it possible to differentiate between them (ie. make the CR make a new line in a text area, and the 'enter' key submit it's form ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅 Jan Wolters 的论文 Javascript 疯狂:键盘事件。
Enter
和Numpad Enter
都给出相同的键码,即 13,因为浏览器不区分这两个键。老实说,大多数环境也是如此。可以使用 Windows API(例如)来区分它们,但确实需要付出额外的努力。然而,这超出了浏览器抽象的范围。更新
正如 Bill Thorne 正确提到,
KeyboardEvent
对象运动现在的location
属性。来自 Mozilla 开发者网络:
See Jan Wolters’ treatise on Javascript Madness: Keyboard Events.
Enter
andNumpad Enter
both give the same keycode, i.e. 13, because browsers do not differentiate between the two keys. To be honest, nor do most environments. It is possible to differentiate between them using the Windows API (for example), but it does take extra effort to do so. This, however, falls outside the scope of the browser’s abstraction.UPDATE
As Bill Thorne rightfully mentions, the
KeyboardEvent
object sports alocation
property nowadays.From the Mozilla Developer Network:
我提供更新,因为这个问题仍然出现在谷歌搜索结果的顶部附近。
根据 MDN,
KeyboardEvent.keyCode
和KeyBoardEvent.charCode
是已弃用,不应再使用。KeyboardEvent
键可以通过访问 来确定KeyboardEvent.key
,KeyboardEvent.code
和KeyboardEvent.location
属性(根据需要)。KeyboardEvent.key
通常返回您在文本编辑器中看到的输出键和非输出键的名称(包括区分大小写)。KeyboardEvent.code
返回按键的字符串描述。KeyboardEvent.location
返回 0 到 3 之间的整数,表示该键所在的键盘区域(分别为标准、左、右和小键盘)。了解这些属性之间的差异有助于确定哪些属性将最适合您的具体情况。对于此问题:
event.key
将为“回车”键和“数字键盘输入”键返回相同的输出(“Enter”
),而 < code>event.code 将分别返回"Enter"
和"NumpadEnter"
。在这种情况下,如果您想区分小键盘和键盘按键,可以使用
event.code
。如果您希望它们的操作相同,event.key
将是更好的选择。如果您想区分其他键,例如左右
Ctrl
键,您还需要查看event.location
属性。我添加了一个小型键盘事件游乐场来查看这些事件属性之间的差异。感谢 MDN 提供 这个概念我只在下面稍作修改:
I am providing an update as this question still appears near the top of google search results.
Per MDN,
KeyboardEvent.keyCode
andKeyBoardEvent.charCode
are deprecated and should no longer be used.KeyboardEvent
keys can be determined by accessing theKeyboardEvent.key
,KeyboardEvent.code
, andKeyboardEvent.location
properties as necessary.KeyboardEvent.key
returns generally what you see in a text editor for output keys and the name on non-output keys (including being case-sensitive).KeyboardEvent.code
returns a string description of the key.KeyboardEvent.location
returns an integer between 0 and 3 to signify the area of the keyboard the key is located in (standard, left, right, and numpad respectively).Understanding the difference between these properties can help to determine which will be most appropriate for your given situation. In the case of this question:
event.key
will return the same output ("Enter"
) for both the 'carriage return' and 'numpad enter'keys, whileevent.code
will return"Enter"
and"NumpadEnter"
respectively.In this case, if you wanted to differentiate between numpad and keyboard keys, you could use
event.code
. If you wanted their operation to be the same,event.key
would be a better choice.If you wanted to differentiate between other keys, such as the left and right
Ctrl
keys, you would also want to look at theevent.location
property.I'm adding a small keyboard event playground to see the difference between these event properties. Credit to MDN for providing the concept that I only slightly modified below:
如果键盘上有一个物理上不同的按键,浏览器应用程序应该与桌面应用程序一样能够区分。
在最新版本的 Chrome (39.0.2171.95 m)、Firefox (32.0.3)、IE (11.0.9600.17501) 和 Opera (12.17) 中,键盘事件对象现在具有 location 属性。我认为这个属性已经存在了一段时间,尽管它的记录很少。
onkeydown的测试表明,当按下“正常”回车键时,keyCode=13且location=0;当按下数字键盘 Enter 时,keyCode=13 且 location=3。
所以下面的代码可以用来设置key==13(如果是回车),key==176(如果是小键盘回车):
If there is a key on the keyboard that is physically different, browser applications should be just as capable as desktop applications to differentiate.
With the latest versions of Chrome (39.0.2171.95 m), Firefox (32.0.3), IE (11.0.9600.17501) and Opera (12.17), the keyboard event object now has the location property. I would presume this property has been around for a while, although it is lightly documented.
Tests of onkeydown reveal that when the "normal" enter key is pressed, keyCode=13 and location=0; when the numpad enter is pressed, keyCode=13 and location=3.
So the following code can be used to set key==13 if the enter, key==176 if numpad enter:
您可以通过事件的
code
属性来区分两者。对于小键盘,它返回NumpadEnter
,对于另一个小键盘,它返回Enter
。或者,您还可以使用在所有情况下都有效的location
属性,这与code
属性不同。对于小键盘 Enter,它返回 3,而对于其他 Enter,它返回 0。You can differentiate both by
code
property of the event. For the numpad one, it returnsNumpadEnter
and for the other one it returnsEnter
. Alternatively, you can also uselocation
property which works in all cases, unlikecode
property. For numpad Enter, it returns 3 whereas, for other Enter it returns 0.