javascript可以告诉左右键之间的区别吗? [英] Can javascript tell the difference between left and right shift key?
问题描述
大多数情况下,这是一项健全性检查。两个shift键的关键代码是16.这是否意味着在浏览器中区分左右移位事件实际上是不可能的?
Mostly this is a sanity check. The key code for both shift keys is 16. Does that mean it is actually impossible to distinguish a left and right shift events in a browser?
推荐答案
在支持 DOM3
的较新浏览器中,您可以使用 event.location
检查位置。
In newer browsers supporting DOM3
you can use event.location
to check the location.
在 DOM3规范,为位置定义了4个常量, DOM_KEY_LOCATION_STANDARD
, DOM_KEY_LOCATION_LEFT
, DOM_KEY_LOCATION_RIGHT
和 DOM_KEY_LOCATION_NUMPAD
。
In the DOM3 spec, there are 4 constants defined for location, DOM_KEY_LOCATION_STANDARD
, DOM_KEY_LOCATION_LEFT
, DOM_KEY_LOCATION_RIGHT
, andDOM_KEY_LOCATION_NUMPAD
.
在这种情况下,你可以这样做:
In this case, you can do:
if (event.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT){
} else if (event.location === KeyboardEvent.DOM_KEY_LOCATION_RIGHT){
}
这篇关于javascript可以告诉左右键之间的区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!