I tried implementing several unique Twitter tricks, Konami commands, etc. using JavaScript.

I tried implementing several unique Twitter tricks, Konami commands, etc. using JavaScript.

Hello, this is naouniverse ( @naouniverse708 ).

It may have passed its season as a joke, but there is a unique trick on the browser version Twitter , where when you enter "up, down, down, left, right, left BA", the blue bird mark will rotate once, and it is also implemented on this site. I tried it.

[Up, down, left, right, left and right BA] Twitter supports Konami commands | Netorabo

via: I tried implementing Konami commands with JS.

In the method on the above site that I referred to,

  1. Set any key code to each element of the array
  2. Check array elements in order for each keystroke
  3. Increment hitCount if it matches, reset if it doesn't match
  4. If hitCount exceeds the set number of key codes, perform some processing

I guess this is what it looks like in summary.

With that in mind, I tried a slightly different method and ended up with something like the one below.

document.onkeydown = commandCheck; var command = new Array(2); command[0] = "38;38;40;40;37;39;37;39;66;65;"; //Konami command command[1 ] = "80;80;39;75;80;"; //Shunkokusatsu var input = new Array(2); //Input key code is stored in each element in sequence function commandCheck(evt) { // Store the key code, the method differs depending on the browser var key; //document.all can only be used in IE if (document.all) { key = event.keyCode; } else { key = evt.which; } //string Retry is supported by intermittent for (i = 0; i < command.length; i++) { input[i] += key + ";"; while (command[i].length < input[i].length) { input[i] = input[i].substr(1); } } //Activated when the input key codes match if (command[0] == input[0]) { //Optional processing alert( "Konami Command"); input[0] = ""; } if (command[1] == input[1]) { //Optional processing alert("Shun Gokusatsu"); input[1] = "" ; } }

The point is,

  1. Multiple implementations possible, command sets a whole key code in one element, separated by ";"
  2. hitCount is eliminated
  3. Check if command and input have the same index for each keystroke
  4. If there is a match, perform some processing and reset the corresponding index of input

It's like that.

For now, the alert is displayed (as of the date and time this article was posted), but it seems like you can do a lot of things depending on your ideas.

  • Add this entry to Hatena Bookmarks

At "naouniverse.com", the administrator writes about things, things, and the world that interest him.
Gadgets, cameras, design, programming, L'Arc~en~Ciel, etc...