問題描述
這主要是一個健全性檢查.兩個 shift 鍵的鍵碼都是 16.這是否意味著實際上無法在瀏覽器中區(qū)分左右 shift 事件?
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 規(guī)范中,為位置定義了 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){
}
這篇關(guān)于javascript可以區(qū)分左右shift鍵嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!