English 中文(简体)
j Qu 检测主要中风,并相应确定变量
原标题:jQuery detect keystrokes and set variables accordingly

我在守则中设立了一条主线,以便在(不是同时)进行下列关键风暴时启动:Enter + c + l + o + r + s:

isEnter = 0; isC = 0; isO = 0; isL = 0; isR = 0; isS = 0;
$(window).keydown(function(e){
    if(e.which==13) isEnter = 1; if(e.which==67) isC = 1; if(e.which==79) isO = 1; 
    if(e.which==76) isL = 1; if(e.which==82) isR = 1; if(e.which==83) isS = 1;
    ColorMap();
});

function ColorMap(){
  if(isEnter==1 && isC==1 && isO==1 && isL==1 && isR==1 && isS==1){
     //DO FUNCTION//
     isEnter = 0; isC = 0; isO = 0; isL = 0; isR = 0; isS = 0; //SET VARS BACK TO 0
  };
};

如果除这些关键因素之外的其他东西被施压......它们必须使用<代码>,我需要为关键缩编职能增加重新设定功能,以重新确定所有变量。 进入+c + o + l + o + r + s,或该表重新编号,必须重新开始......(这将使东埃格人更难以[或至少更不可能通过流体或随机中线]。

最佳回答

你们也希望检查这些问题。 现在,你可以把钥匙推向任何顺序,并继续启动东埃克(以下简称“代码”)。

我会按顺序保存你要查找的值,就像这样:

//       Enter, c, o, l, o, r, s
var keys = [13,67,79,76,79,82,83];

然后,您可以轻松跟踪用户在序列中的位置:

var nextKey = 0;
$(window).keydown(function(e){
    var key = e.which;
    if (key === keys[nextKey])
        nextKey++;

    ColorMap();
});

缩略语 主要 每次你都与你所期待的下一个关键相吻合。 <代码>next 关键变量从0指数开始,这意味着我们开始寻找<代码>。 见。 现在,我们需要检查<编码>ColorMap功能中的终点。

function ColorMap() {
  var maxKeyIndex = keys.length - 1;
  if(nextKey >= maxKeyIndex) {
     //DO FUNCTION//

     nextKey = 0;
  }
}

此解决方案应允许您更改 keys 变量中的特殊序列,而不需要在代码的其他位置进行更改。

EDIT: If you want the answer to be contiguous, as you probably do, then you can reset the nextKey variable when the match fails.

if (key === keys[nextKey])
    nextKey++;
else
    nextKey = 0;

为了额外的学分,您可以将此切换为使用字符串来保存复活节彩蛋,然后使用String.fromCharCode将其转换为e.which返回的字符代码。

问题回答

我最近回答了一个类似的复活节彩蛋问题,所以我只需要把那个问题指给你看。

它采用了与您所做的不同的方法,并且不需要重置。

JavaScript/jQuery按键记录

http://www.un.org。 这里的更新版使关键历史永远不会消失。

$(document).ready(function() {

    var easterEgg =  egg ;
    var eggLength = easterEgg.length;
    var keyHistory =   ;
    var match;
        $(document).keypress(function(e) {
            keyHistory += String.fromCharCode(e.which)
            match = keyHistory.match(easterEgg); 
            if(match) {
                alert(match);
                keyHistory = match =   ;
            } else if (keyHistory.length > 30) {
                keyHistory = keyHistory.substr((keyHistory.length - eggLength - 1));
            }
        });
});

现在,你会再次让用户以任何顺序播送魔.钥匙,而这种定律不胜。 我把魔法的顺序划入一个阵列,而成功的关键中风与你在阵列中向前迈进相吻合。 如果你结束,那就会浪费时间。

<>edit/strong> 确切为@Endangered Massa写道。

这里还有另一种方法:

<script type="text/javascript">
$(function() {
  $(window).keypress(function(e) {
    var input = $("#input");
    input.val(function(i, v) { return v + String.fromCharCode(e.which) });

    if(input.val().indexOf( colors ) > -1) {
      alert( Easter Egg );
      input.val(  );
    } 
    else if( colors .indexOf(input.val())) {
      //Clear value, they ve hit another key
      input.val(  );
    }        
  });
 });
</script>
<input id="input" type="text" />

它会跟踪文本框中输入的当前颜色部分,便于调试,测试完成后只需添加 style="display: none;"





相关问题
passing form variables into a url with php

I have the following form which allows a user to select dates/rooms for a hotel reservation. <form action="booking-form.php" method="post"> <fieldset> <div class="select-date">...

Error: "Cannot modify the return value" c#

I m using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error ...

C-style Variable initialization in PHP

Is there such a thing as local, private, static and public variables in PHP? If so, can you give samples of each and how their scope is demonstrated inside and outside the class and inside functions?

C#/.NET app doesn t recognize Environment Var Change (PATH)

In my C# app, I am programmatically installing an Oracle client if one is not present, which requires adding a dir to the PATH system environment variable. This all works fine, but it doesn t take ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I m trying to use to dynamically create variable. Example for( i = 0 ; i &lt 3 ; i++) { var MyVar+i = i; } after this for loop runs, i would like to ...

热门标签