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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Internet explorer is capable of distinguishing left and right shift with the shiftLeft property:
shiftLeft property (event)
Otherwise, they are indistinguishable.
In newer browsers supporting
DOM3
you can useevent.location
to check the location.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:
The easiest way to do it
Note: You have to click the inside white space when you run code snippet to activate keyboard keys. This is tested in Chrome and Safari.
You can use
event.code
(the physical keyboard string) instead ofevent.key
(the numeric ascii value).event.code
MDN docsIf you scroll down to "Code values" at the bottom, you can find the two distinct shift keys:
"ShiftLeft"
,"ShiftRight"