如何获得javascript中键组合的键代码?

如何获得javascript中键组合的键代码?,javascript,Javascript,有了上面的代码,我就可以得到单键的keycode了。但是,当我使用组合键时,我会得到第一个和第二个键的事件。例如(班次+1),$(班次+4) 如何获取组合键的键代码?您需要记住按下了哪个键。您应该侦听keyup+keydown事件: $(document).keydown(function(event) { doSomething(event); event.preventDefault(); }); 以下是一些可以帮助您的插件: (这个是免

有了上面的代码,我就可以得到单键的keycode了。但是,当我使用组合键时,我会得到第一个和第二个键的事件。例如(班次+1),$(班次+4)


如何获取组合键的键代码?

您需要记住按下了哪个键。您应该侦听keyup+keydown事件:

    $(document).keydown(function(event) {

        doSomething(event);
        event.preventDefault();
    });

以下是一些可以帮助您的插件:

  • (这个是免费的)

  • 您可以使用一个函数来扩展jquerys
    fn
    对象,该函数基于按键和正则表达式匹配来处理回调函数的触发

    我写了一个类似的,在它之前支持单键的正则表达式匹配,我只是稍微修改它以支持多键

    var keyCodesPressed = {};
    
    $(document).keydown(function(event) {
      keyCodesPressed[event.which] = true;
      // here you should have all keys which are currently pressed:
      for (keyCode in keyCodesPressed) {
        // and you can loop over them and detect which are the ones currently pressed
      } 
    });
    
    $(document).keyup(function(event) {
      delete keyCodesPressed[event.which];
    });
    
    例如,您还可以执行
    filter:/.{4,5}/
    ,这将触发同时按下的任何4到5个键

    例如,当按下A+S+D时触发

      $.fn.selectedKey = (function () {
          var keys = "";
          var last = "";
          var key = "";
          return function (cb, data) {
              def.call(data, {
                  ctrlKey: 2, //0: musn't be pressed, 1: must be pressed, 2: both.
                  altKey: 2, // "
                  invert: 0, //inverts the filter
                  filter: /.*/, // A Regular Expression, or a String with a Regular Expression
                  preventDefault: false //Set to true to prevent Default.
              }); //Sets the default Data for the values used,
    
              function validate(e) {
    
                  var exp = new RegExp(e.data.filter.replace(/\\\\(\d)/g, String.fromCharCode("$1"))); //Creates a new RegExp from a String to e.g. allow "\2" to match the keyCode 2
                  var c = !! (e.data.ctrlKey ^ e.ctrlKey ^ 1 > 0); //c == true if the above stated conditions are met e.g Ctrl Key Pressed and `ctrlKey == 1` -> true
                  var a = !! (e.data.altKey ^ e.altKey ^ 1 > 0); //e.g Alt Key Pressed and `altKey == 0` -> false
                  //console.log(keys,exp,c,a)
                  return (exp.test(keys) && (c && a)); //Returns the validation Result
              }
    
              function def(obj) { //a minimal helper for default values
                  for (var prop in obj) {
                      this[prop] = this[prop] || obj[prop];
                  }
              }
              this.keypress(data, function (e) {
    
                  key = e.char = String.fromCharCode(e.keyCode || e.which); //Converts the pressed key to a String
                  keys += ( !! ~ (keys.indexOf(key))) ? "" : key;
                  key = key["to" + (e.shiftKey ? "Upper" : "Lower") + "Case"]();
                  keys = keys["to" + (e.shiftKey ? "Upper" : "Lower") + "Case"](); //case handling
                  if (e.data.preventDefault) e.preventDefault();
    
                  if ((validate(e) != e.data.invert) && keys != last) {
    
                      cb(e);
                      last = keys;
                      //Calls the callback function if the conditions are met
                  }
              });
              if (!this.data("keyupBound")) {
                  this.keyup(data, function (e) {
    
                      key = e.char = String.fromCharCode(e.keyCode || e.which); //Converts 
                      var t = keys.toLowerCase()
                          .split("");
                      t.splice(t.indexOf(e.char), 1);
                      keys = t.join("");
                      last = keys;
                  });
                  this.data("keyupBound", true);
              }
          };
    
      })();
    
    
    $("body").selectedKey(function (e) {
        console.log("All lower characters, Numbers and 'A': " + e.char);
    }, {
        filter: "^[a-z]|[0-9]|A$",
        ctrlKey: 2,
        altKey: 2
    });
    
    $("body").selectedKey(function (e) {
        console.log("KeyCode 2 " + e.char); // Ctrl + b
    }, {
        filter: "\\2",
        ctrlKey: 1,
        altKey: 2
    });
    
    这里有一个关于

    编辑注释。修复了当验证评估为true时,在按住键时持续触发的问题

    Edit2:修复了
    last
    变量,该变量没有正确使用…

    连续键的组合,或者简单地像
    Shift 4
    Ctrl t
    $("body").selectedKey(function (e) {
        console.log("ASD has been pressed"); // Ctrl + b
    }, {
        filter: "^ASD$",
        ctrlKey: 2,
        altKey: 2
    });