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

前端 未结 4 1680
不知归路
不知归路 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条回答
  •  旧时难觅i
    2020-11-30 10:21

    In newer browsers supporting DOM3 you can use event.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:

    if (event.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT){
    
    } else if (event.location === KeyboardEvent.DOM_KEY_LOCATION_RIGHT){
    
    }
    

提交回复
热议问题