gpt4 book ai didi

javascript - 使用 Javascript 检测按键组合系列

转载 作者:行者123 更新时间:2023-11-30 10:42:56 25 4
gpt4 key购买 nike

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

注意:下面的脚本正在寻找以下按键系列:
surprise1 - 左 (x3)、右 (x3)、上 (x3)、下 (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;
}
};

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

最佳答案

试试这个:我正在使用 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/

关于javascript - 使用 Javascript 检测按键组合系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10061084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com