Can javascript tell the difference between left and right shift key?

前端 未结 4 1673
不知归路
不知归路 2020-11-30 09:41

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?

4条回答
  •  抹茶落季
    2020-11-30 10:31

    You can use event.code (the physical keyboard string) instead of event.key (the numeric ascii value).

    event.code MDN docs

    The KeyboardEvent.code property represents a physical key on the keyboard (as opposed to the character generated by pressing the key).

    If you scroll down to "Code values" at the bottom, you can find the two distinct shift keys:

    "ShiftLeft", "ShiftRight"

提交回复
热议问题