如何实现用户可自定义的键盘快捷键(在javascript / web上)

时间:2015-12-02 09:26:32

标签: javascript keyboard-shortcuts

标题大部分都是这样说的。我想要一个javascript / html解决方案,了解如何制作持久的,用户可配置的键盘快捷键侦听器(以及如果你有&#em; em,如何真正保留首选项的任何好的想法)。

在同一时间看起来既简单又有点棘手:)

谢谢!

2 个答案:

答案 0 :(得分:0)

如果你想让用户获得像" Control" +(...)这样的快捷方式,你只需要按下按下的键,所以检查" Control"按下这样的按键进行操作。

这是一个简单而不好的例子:

var Pressed={
   CTRL:false,
   Spacebar:false
},Keyboard={ // an object containing Keyboard keys code
   CTRL:17,
   Spacebar:32
};
window.onkeydown=function(e){ // user started pressing an key (event)
   var Key=(e.keyCode||e.which||e.key); // capture key pressed from event
   if(Key==Keyboard.CTRL){ // CTRL is pressed
      Pressed.CTRL=true
      Pressed.Spacebar=false // CTRL has to be pressed before than Spacebar
   }else if(Key==Keyboard.Spacebar){ // Spacebar is pressed
      Pressed.Spacebar=true
   }
   if(Pressed.CTRL&&Pressed.Spacebar){ // Case CTRL+Spacebar are pressed
      e.preventDefault(); // prevent Opera from quitting from the page
      console.log("CTRL+Spacebar were pressed.")
   }
},
window.onkeyup=function(e){ // event when an key is released
   var Key=(e.keyCode||e.which||e.key); // capture key released from event
   if(Key==Keyboard.CTRL){ // CTRL is released
      Pressed.CTRL=false
   }else if(Key==Keyboard.Spacebar){ // Spacebar is released
      Pressed.Spacebar=false
   }
};

更新......

答案 1 :(得分:0)

您可以尝试这样的事情:

出于演示目的,我刚刚记录了AltCtrlShift +'Key',如果在快捷方式中定义了密钥,则打印Special action else'正常行动'。此外,我已禁用传播的关键事件传播,但如果不需要,可以删除此部分。

JSFiddle

代码

(function keyLogger() {
  var isCtrlPressed = false;
  var isAltPressed = false;
  var isShiftPressed = false;
  
  var shortcutKeys = [
    13, // Enter
    32, // Space
    37, // Left Arrow
    38, // Up Arrow
    39, // Right Arrow
    40, // Down Arrow
    90, // z
  ]

  function registerEvents() {
    document.onkeydown = function(event) {
      var keyCode = event.keyCode ? event.keyCode : event.which;
      if (keyCode >= 16 && keyCode <= 18) {
        updateKeyFlags(keyCode, true);
      }

      if (isCtrlPressed || isAltPressed || isShiftPressed) {
        if (shortcutKeys.indexOf(keyCode) >= 0) {
          console.log("Special action");
        } else {
          console.log("Normal action");
        }
      }
    }

    document.onkeyup = function(event) {
      var keyCode = event.keyCode ? event.keyCode : event.which;
      if (keyCode >= 16 && keyCode <= 18) {
        updateKeyFlags(keyCode, false);
      }
    }

    // To prevent logging from Inputs.
    // Can be removed if this action is required.
    var inputs = document.getElementsByTagName("input");
    for (var i in inputs) {
      inputs[i].onkeyup = function(event) {
        event.stopPropagation();
      }
      inputs[i].onkeydown = function(event) {
        event.stopPropagation();
      }
    }
  }

  function updateKeyFlags(keyCode, flag) {
    switch (keyCode) {
      case 16:
        isShiftPressed = flag;
        break;
      case 17:
        isCtrlPressed = flag;
        break;
      case 18:
        isAltPressed = flag;
        break;
    }
  }

  registerEvents();
})();
<div>
  <input type="text">
  <input type="text">
</div>