Keypress组合用于秘密入口链接

时间:2013-09-28 05:06:38

标签: javascript jquery html keypress

我想为我的网站添加一些代码,当我在一个页面上时,可能是任何页面,但是对于这个例子,我们将使用带有“herp”的纯白页并且没有格式化。在这个页面上,我想如果用户然后在页面中键入'derp'而不是文本框,它会将用户带到下一页。这可能吗?简单?任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

我认为有可能,至少在Chrome中,如果您将keypress处理程序附加到body,则可以跟踪按下的键并识别所需单词的输入时间。下面是一个附加事件处理程序的示例代码。我不确定这是否可以跨浏览器移植。

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  </head>
  <body>
    <script>
      $("body").keypress(function (event) {
        console.log(event);
      });
    </script>
  </body>
</html>

http://plnkr.co/edit/pFHaSedMcxxHajywFJ0m

答案 1 :(得分:0)

如果你想从头开始这样做,你会听取按键和按键事件。如果你从按键中检查事件对象,你也会发现其他一些隐藏的宝石。

var body = document.querySelector('body'),
    downKeys={};

body.addEventListener(
    'keydown',
    keyDown
);

body.addEventListener(
    'keyup',
    keyUp
);

function keyDown(e){
    downKeys[e.keyCode]=true;
    testMagicCombo();
}

function keyUp(e){
    downKeys[e.keyCode]=false;
}

function testMagicCombo(){
    //you can add a lot more logic here to do more testing
    //but this is the gist of it.
    //the codes below are for ctrl+alt+shift+L (why L? why not?)
    //you can easily see all the key stats by logging the downKeys object
    if(downKeys[16]&&downKeys[17]&&downKeys[18]&&downKeys[76])
        console.log('unicorn greets you at magic entry');
}