代码之家  ›  专栏  ›  技术社区  ›  Oscar Godson

使用jquery/javascript进行键组合

  •  7
  • Oscar Godson  · 技术社区  · 14 年前

    我很好奇,在这个问题的最后,我编写了下面的jquery插件代码,如何实现键组合。到目前为止,它的工作原理是允许用户通过执行类似jquery的常规语法来创建键命令,并为键命令提供一个事件,如下所示:

    $(window).jkey('a',function(){
       alert('you pressed the a key!');
    });
    

    $(window).jkey('b c d',function(){
       alert('you pressed either the b, c, or d key!');
    });
    

    最后,我想做的是有能力去做,但我不知道:

    $(window).jkey('alt+n',function(){
       alert('you pressed alt+n!');
    });
    

    我知道如何在插件外执行此操作(在keyup上设置var false,在keydown上设置var true,并在按下另一个键时检查var是否为true),但我不知道当您不知道将要按下哪些键以及要按下多少键时如何执行此操作。如何添加此支持?我想让他们做 alt+shift+a a+s+d+f 如果他们愿意的话。我就是不知道该怎么实施。有什么想法吗?

    我将把它作为一个开源插件发布,我想给给给我权利的人,工作的人,在博客文章和代码上回答一些问题。事先谢谢!

    (function($) {
      $.fn.jkey = function(keyCombo,callback) {
        if(keyCombo.indexOf(' ') > -1){ //If multiple keys are selected
            var keySplit = keyCombo.split(' ');
        }
        else{ //Else just store this single key
            var keySplit = [keyCombo];
        }
        for(x in keySplit){ //For each key in the array...
    
            if(keySplit[x].indexOf('+') > -1){
                //Key selection by user is a key combo... what now?
            }
            else{
                //Otherwise, it's just a normal, single key command
            }
    
            switch(keySplit[x]){
                case 'a':
                    keySplit[x] = 65;
                    break;
                case 'b':
                    keySplit[x] = 66;
                    break;
                case 'c':
                    keySplit[x] = 67;
                    break;
                //And so on for all the rest of the keys
            }
        }
        return this.each(function() {
            $this = $(this);
            $this.keydown(function(e){
                if($.inArray(e.keyCode, keySplit) > -1){ //If the key the user pressed is matched with any key the developer set a key code with...
                    if(typeof callback == 'function'){ //and they provided a callback function
                        callback(); //trigger call back and...
                        e.preventDefault(); //cancel the normal
                    }
                }
            });
        });
      }
    })(jQuery);
    
    4 回复  |  直到 14 年前
        1
  •  2
  •   sebnitu    14 年前

    (function($) { 
      $.fn.jkey = function(keyCombo,callback) {
    
        // Save the key codes to JSON object
        var keyCodes = { 
          'a'   : 65,
          'b'   : 66,
          'c'   : 67,
          'alt' : 18
        };
    
        var x = '';
        var y = '';
    
        if(keyCombo.indexOf(' ') > -1){ //If multiple keys are selected
            var keySplit = keyCombo.split(' ');
        }
        else{ //Else just store this single key
            var keySplit = [keyCombo];
        }
    
        for(x in keySplit){ //For each key in the array...
    
          if(keySplit[x].indexOf('+') > -1){
            //Key selection by user is a key combo
            // Create a combo array and split the key combo
            var combo = Array();
            var comboSplit = keySplit[x].split('+');
    
            // Save the key codes for each element in the key combo
            for(y in comboSplit){
              combo[y] = keyCodes[ comboSplit[y] ];
            }
    
            keySplit[x] = combo;
    
          } else {
            //Otherwise, it's just a normal, single key command
            keySplit[x] = keyCodes[ keySplit[x] ];
          }
    
        }
    
        return this.each(function() {
            $this = $(this);
    
            // Create active keys array
            // This array will store all the keys that are currently being pressed
            var activeKeys = Array();
    
            $this.keydown(function(e){
    
              // Save the current key press
              activeKeys[ e.keyCode ] = e.keyCode;
    
              if($.inArray(e.keyCode, keySplit) > -1){ // If the key the user pressed is matched with any key the developer set a key code with...
    
                if(typeof callback == 'function'){ //and they provided a callback function
                  callback(); //trigger call back and...
                  e.preventDefault(); //cancel the normal
                }
    
              } else { // Else, the key did  not match which means it's either a key combo or just dosn't exist
    
                // Check if the individual items in the key combo match what was pressed
                for(x in keySplit){
                  if($.inArray(e.keyCode, keySplit[x]) > -1){
    
                    // Initiate the active variable
                    var active = 'unchecked';
    
                    // All the individual keys in the combo with the keys that are currently being pressed
                    for(y in keySplit[x]) {
                      if(active != false) {
                        if($.inArray(keySplit[x][y], activeKeys) > -1){
                          active = true;
                        } else {
                          active = false;
                        }
                      }
                    }
    
                    // If all the keys in the combo are being pressed, active will equal true
                    if(active === true){
                      if(typeof callback == 'function'){ //and they provided a callback function
                        callback(); //trigger call back and...
                        e.preventDefault(); //cancel the normal
                      }
                    }
                  }
                }
    
              } // end of if in array
    
            }).keyup(function(e) {
              // Remove the current key press
              activeKeys[ e.keyCode ] = '';
            });
    
        });
    
      }
    })(jQuery);
    
        2
  •  5
  •   Josh Stodola    14 年前

    reference altKey ctrlKey shiftKey

    $(document).keypress(function(e) {
      var key = String.fromCharCode(e.which);
      var alt = e.altKey;
      var ctrl = e.ctrlKey
      var shift = e.shiftKey;
      alert("Key:" + key + "\nAlt:" + alt + "\nCtrl:" + ctrl + "\nShift:" + shift);
    });
    

    String.fromCharCode

    a+s+d+f

    live demo

        3
  •  0
  •   Infotekka    14 年前