English 中文(简体)
A. 检测主要压力组合系列
原标题:Detect keypress combination series with Javascript

就复活节而言,我很想在我所开发的场所“东Egg Hunt”略感惊讶。 其中2个Easter Eggs 我的藏匿将受到关键压制。 这只赢了一种“压力CTRL和TAB同时”的类型,但将是一种“UP”三倍,然后是三倍”。 这将寻求一系列关键压力,而不是一劳永逸地施加压力。 我放弃了这一职能,但出于某种原因,它没有像它那样做。

NOTE: The script below is looking for the following keypress series:
surprise1 - LEFT (x3), RIGHT (x3), UP (x3), DOWN (x3)
surprise2 - 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;
    }
};

告诉我为什么不工作? 在我看来,它应当努力。

最佳回答
问题回答

这里我是如何解决这一问题的。

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();
  }
});

如果你知道顺序是多少,就会ku;

这一点更好:

$(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 );  
   } 
}
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签