How to detect pressed keys on the click of AngularJS

后端 未结 5 1586
渐次进展
渐次进展 2021-02-07 00:44

I\'m using angularjs on a web application that I need to figure out how can I detect is keys like ctrl, shift or alt are pressed when I click so

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 01:25

    There is no "automated" way using pure JS, but it's relatively simple to track modifier keys' state in global variables. E.g.

    window.ctrlDown = false;
    
    document.addEventListener('keydown', function(evt) {
      var e = window.event || evt;
      var key = e.which || e.keyCode;
      if(17 == key) {
        window.ctrlDown = true;
      }
    }, false);
    
    document.addEventListener('keyup', function(evt) {
      var e = window.event || evt;
      var key = e.which || e.keyCode;
      if(17 == key) {
        window.ctrlDown = false;
      }
    }, false);
    

提交回复
热议问题