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

前端 未结 4 1678
不知归路
不知归路 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:20

    The easiest way to do it

    $(document).ready(function(){
      $("html").keydown(function(e) {
    
          if (e.shiftKey) {
             if (event.location == 1) console.log('left shift');
             if (event.location == 2) console.log('right shift');
          }
    
      });
    });

    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.

提交回复
热议问题