秘密入口链接的按键组合

Keypress combo for a secret entrance link

本文关键字:组合 入口 链接 秘密      更新时间:2023-09-26

当我在页面上时,我想为我的网站提供一些代码,可以是任何页面,但对于此示例,我们将使用带有"herp"且没有格式的纯白色页面。在此页面上,我希望用户随后在页面中键入"derp",而不是文本框,它将用户带到下一页。这可能吗?容易?任何建议将不胜感激。

我想这是可能的,至少在 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

如果你想从头开始这样做,你会侦听键下和键上事件。 如果您通过按键检查事件对象,您还会发现其他一些隐藏的宝石。

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