我尝试使用 JavaScript 实现几个独特的 Twitter 技巧、Konami 命令等。
你好,这是 naouniverse ( @naouniverse708 )。
也许作为玩笑已经过去了,但浏览器版Twitter,当你输入“上、下、下、左、右、左 BA”时,蓝色的小鸟标记会旋转一次,我也尝试过这个网站。
【上、下、左、右、左、右BA】Twitter支持Konami命令 |
在我提到的上述网站的方法中,
- 将任意键代码设置为数组的每个元素
- 按每次击键的顺序检查数组元素
- 如果匹配则增加 hitCount,如果不匹配则重置
- 如果hitCount超过设定的键码数量,则进行一些处理
我想这就是总结的样子。
document.onkeydown = commandCheck; var command = new Array(2) = "38;38;40;40;37;39;37;39;66;65;"; //科乐美命令 command[1 ] = "80;80;39;75;80;"; //Shunkokusatsu var input = new Array(2); //输入的密钥代码存储在序列中的每个元素中 function commandCheck(evt) { // 存储密钥code,根据浏览器不同,方法也不同 var key; //document.all 只能在 IE 中使用 if (document.all) { key = event.keyCode; } else { key = evt.which } //string Retry;间歇性支持 for (i = 0; i < command.length; i++) { input[i] += key + ";" while (command[i].length < input[i].length) { input[ i] = input[i].substr(1); } } //当输入键码匹配时激活 if (command[0] == input[0]) { //可选处理alert( "Konami Command"); input[0] = ""; } if (command[1] == input[1]) { //可选处理alert("Shun Gokusatsu"); input[1] = "" } }
document.onkeydown = commandCheck; var command = new Array(2) = "38;38;40;40;37;39;37;39;66;65;"; //科乐美命令 command[1 ] = "80;80;39;75;80;"; //Shunkokusatsu var input = new Array(2); //输入的密钥代码存储在序列中的每个元素中 function commandCheck(evt) { // 存储密钥code,根据浏览器不同,方法也不同 var key; //document.all 只能在 IE 中使用 if (document.all) { key = event.keyCode; } else { key = evt.which } //string Retry;间歇性支持 for (i = 0; i < command.length; i++) { input[i] += key + ";" while (command[i].length < input[i].length) { input[ i] = input[i].substr(1); } } //当输入键码匹配时激活 if (command[0] == input[0]) { //可选处理alert( "Konami Command"); input[0] = ""; } if (command[1] == input[1]) { //可选处理alert("Shun Gokusatsu"); input[1] = "" } }
重点是,
- 可能有多种实现方式,命令将整个关键代码设置在一个元素中,并用“;”分隔
- hitCount 被消除
- 检查每次击键的命令和输入是否具有相同的索引
- 如果匹配,则执行一些处理并重置输入的相应索引
就是这样。
目前,警报已显示(截至本文发布的日期和时间),但您似乎可以根据自己的想法做很多事情。