【问题标题】:Detect keypress combination series with Javascript用Javascript检测按键组合系列
【发布时间】:2021-06-17 04:44:44
【问题描述】:

对于复活节假期,我想在我开发的网站上有一个小小的惊喜“复活节彩蛋狩猎”。我隐藏的这五个复活节彩蛋中的两个将被按键指定。这不会像“同时按 CTRL 和 TAB”类型的交易,而是“按 UP 3 次然后 RIGHT 3 次”类型的事情。这将寻找一系列按键,而不是一次按下两个按键。我已经设置了这个功能,但由于某些奇怪的原因,它不能正常工作。

注意: 下面的脚本正在寻找以下按键系列:
惊喜 1 - 左 (x3)、右 (x3)、上 (x3)、下 (x3)
惊喜2 - SHIFT (x3)、TAB (x3)、CTRL (x3)

$(document.body).keydown(function(e) {
            surprise1(e);
            surprise2(e);
});

function surprise1(e) {
    var ev = (e) ? e : window.event;
    var k = ev.keyCode;
    if (k > 36 && k < 41) {
        typekeys[k] = isNaN(typekeys[k]) ? 0 : typekeys[k];
        typekeys[k]++;
        if (typekeys[37] == 3) {
            if (typekeys[37] == 3 && typekeys[39] == 3) {
                if (typekeys[37] == 3 && typekeys[39] == 3 && typekeys[38] == 3) {
                    if (typekeys[37] == 3 && typekeys[39] == 3 && typekeys[38] == 3 && typekeys[40] == 3) {
                        alert("You've found Surprise 1! Contact the site admin ASAP to get your prize!");
                        typekeys[37] = typekeys[39] = typekeys[38] = typekeys[40] = 0;
                    }
                } else {
                    typekeys[40] = 0;
                }
            } else {
                typekeys[38] = typekeys[40] = 0;
            }
        } else {
            if (typekeys[37] > 3) {
                typekeys[37] = 0;
            }
            typekeys[39] = typekeys[38] = typekeys[40] = 0;
        }
    } else {
        typekeys[37] = typekeys[39] = typekeys[38] = typekeys[40] = 0;
    }
};

function surprise2(e) {
    var ev = (e) ? e : window.event;
    var k = ev.keyCode;
    if (k > 8 && k < 18) {
        typekeys[k] = isNaN(typekeys[k]) ? 0 : typekeys[k];
        typekeys[k]++;
        if (typekeys[16] == 3) {
            if (typekeys[9] == 3) {
                if (typekeys[16] == 3 && typekeys[9] == 3 && typekeys[17] == 3) {
                    alert("You've found Surprise 2! Contact the site admin ASAP to get your prize!");
                    typekeys[16] = typekeys[9] = typekeys[17] = 0;
                }
            }
        } else {
            if (typekeys[16] > 3) {
                typekeys[16] = 0;
            }
            typekeys[9] = typekeys[17] = 0;
        }
    } else {
        typekeys[16] = typekeys[9] = typekeys[17] = 0;
    }
};

介意告诉我为什么这不起作用吗?在我看来它应该可以工作。

【问题讨论】:

    标签: javascript jquery jquery-events


    【解决方案1】:

    试试这个:我正在使用https://github.com/madrobby/keymaster jquery 插件

    $(function () {
       var combination = ''
       key('left', function(){ 
           combination = 'left';
           checkCombination();
       });
       key('right', function(){ 
           combination+= 'right';
           checkCombination();
       });
       key('up', function(){ 
           combination+= 'up';
           checkCombination();
       });
       key('down', function(){ 
           combination+= 'down';
           checkCombination();
       });
    
       function checkCombination() {
          if(combination === 'leftrightupdown') {
            alert('surprise 1');  
          } 
       }
    });​
    

    演示:http://jsfiddle.net/codef0rmer/BSdCq/

    【讨论】:

    • 你用的是哪个版本的IE?
    • IE 9 但它也可能是 jsFiddle,没有在其他任何地方复制代码
    • @Stefan:它会起作用的。我认为 Jsfiddle 在 IE 中不包含 keymaster.js - 你可以查看控制台。
    【解决方案2】:

    这是我的解决方案。我不得不做一些有趣的事情来比较here 中描述的数组。我相信你可以根据你的需要来适应这个脚本的一般要点......

    var seqs = [ [37,37,37,38,38,38,39,39,39,40,40,40], [9,9,9,16,16,16,17,17,17] ];
    var seq  = [];
    
    var messages=["You've found Surprise 1! Contact the site admin ASAP to get your prize!", "You've found Surprise 2! Contact the site admin ASAP to get your prize!"];
    
    window.addEventListener("keydown", function(e){
        seq.push(e.keyCode);
        var eq = function(a,b){ return !( a<b || b<a ); };
        for ( var i = 0; i < seqs.length; i++ ) {
            if ( eq( seq, seqs[i].slice(0,seq.length) )) {
              if ( eq(seq, seqs[i]) ) {
                alert( messages[i] );
                seq = [];
              }
            return;
            }
        }
        seq = [];
    });
    

    【讨论】:

      【解决方案3】:

      这是我解决这个问题的方法...

      var nums = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
      var n = nums.slice();
      $(document).keydown(function(e){
        if(e.which == n[0]){
          n.shift();
        } else n = nums.slice();
        if(n.length == 0) {
          //success!
          n = nums.slice();
        }
      });
      

      如果您知道顺序是什么,请多多支持。 ;)

      【讨论】:

      • 科乐美代码 :-)
      【解决方案4】:

      这样更好:

      $(function () {
        var combination = ''
         key('left', function(){ 
         combination = 'left';
         checkCombination();
       });
      key('right', function(){ 
          combination+= 'right';
          checkCombination();
      });
      key('up', function(){ 
          combination+= 'up';
          checkCombination();
      });
      key('down', function(){ 
          combination+= 'down';
          checkCombination();
      });
      
      key(!'down' && !'left' && !'right' && !'up',function() {
          combination = '';
      });
      
      function checkCombination() {
         if(combination === 'leftrightupdown') {
           alert('surprise 1');  
         } 
      }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        • 1970-01-01
        相关资源
        最近更新 更多