Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created September 2, 2011 12:17
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsaniel/1188477 to your computer and use it in GitHub Desktop.
Save tsaniel/1188477 to your computer and use it in GitHub Desktop.
Konami Code easter egg
function(
a, // callback function
b // placeholder for key stack
){
document.onkeyup = function(e){
/113302022928$/.test( // check if the latest pressed codes are Konami Code
b += [((e||self.event).keyCode-37)]) // push the code it into the stack
) && a() // callback if pressed Konami Code
}
}
function(f,a){document.onkeyup=function(e){/113302022928$/.test(a+=[((e||self.event).keyCode-37)])&&f()}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "KonamiCodeEasterEgg",
"description": "The famous Konami Code easter egg.",
"keywords": [
"Konami Code",
"easter egg",
"fun"
]
}
<!DOCTYPE html>
<title>Konami Code easter egg</title>
<div>Expected value: <b>Try to press (↑ ↑ ↓ ↓ ← → ← → B A)</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(f,a){document.onkeyup=function(e){/113302022928$/.test(a+=[((e||self.event).keyCode-37)])&&f()}};
myFunction(function(){alert('onKonamiCode triggered!')})
</script>
@p01
Copy link

p01 commented Sep 2, 2011

126 bytes version

function(f,a){document.onkeyup=function(e){(a+=String.fromCharCode((e||window.event).keyCode)).slice(-10)=="&&((%'%'BA"&&f()}}

Do we actually need the document. and window. ?

@tsaniel
Copy link
Author

tsaniel commented Sep 3, 2011

Great idea !
And maybe the document. and window. are for the compatibility.

@p01
Copy link

p01 commented Sep 3, 2011

Fair enough about the compat.

Thanks for reminding me the syntax of the eval('"\\x'...+'"') trick. Good idea to use a /...^$/.test(...) thus saving the .slice(-10). I tried using which instead of keyCode but IE8 didn't seem too happy.

Replacing window. by self. saves another 2 bytes ;)

@tsaniel
Copy link
Author

tsaniel commented Sep 3, 2011

I also have to thank you for reminding me the "codes to characters" and the self. tricks :)

@atk
Copy link

atk commented Sep 13, 2011

eval('"\\x'+...+'"') is rather big. Without it, we have

function(f,a){document.onkeyup=function(e){/38384040373739396566$/.test(a+=''+(e||self.event).keyCode)&&f()}}

saving another 4 bytes - Though I have to admit it is not near as precise as the conversion.

@p01
Copy link

p01 commented Sep 13, 2011

Yes, it is more prone to false positive but who cares ? Finding and entering such false positive should be rewarded too!

3 bytes shorter ;)
function(f,a){document.onkeyup=function(e){/113302022928$/.test(a+=''+((e||self.event).keyCode-37))&&f()}}

@tsaniel
Copy link
Author

tsaniel commented Sep 15, 2011

Nice.
Maybe there are more shorter versions through this way.

@atk
Copy link

atk commented Sep 15, 2011

Three more bytes saved by not using the regexp:

function(f,a){document.onkeyup=function(e){2200806046112155E4^(a=a*94+((e||self.event).keyCode))||f()}}

Btw, you forgot to change the regexp in the annotated version.

@p01
Copy link

p01 commented Sep 15, 2011

@atk: Unless I'm missing something, this implementation will only work once, and only if the konami code is the first thing the user enters.

A shorter approach I thought of but found compatibility issues with was:
function(f,a){document.onkeyup="/113302022928$/.test(a+=''+(self.event.keyCode-37))&&"+f+"()"}

@atk
Copy link

atk commented Sep 15, 2011

You're right - I'll have to work on that. But this way, "f" cannot be an anonymous function, but needs to be somewhere in the global scope.

@tsaniel
Copy link
Author

tsaniel commented Sep 15, 2011

I think we should not sacrifice the compatibility in this case...

@p01
Copy link

p01 commented Sep 15, 2011

@atk: IINM there's no problem with f being anonymous. What is "injected" in the string set to document.onkeyup is the string representation of f

Also it should be possible to get your approach working repeatedly using the correct magic 32bits mask of the Konami code. Multiplying a and adding the last keyCode will make the oldest keyCodes overflow. That would bring us down to ~97 bytes with something like:

function(f,a){document.onkeyup=function(e){0x12345678^(a=a*1337+((e||self.event).keyCode))||f()}}

@atk
Copy link

atk commented Sep 15, 2011

I take it the correct values for 0x12345678 and 1337 are yet to be determined?

@p01
Copy link

p01 commented Sep 18, 2011

Correct.

http://jsbin.com/ofitoj/edit#preview works fine in IE8, Chrome Canary, Opera 12 and Firefox Aurora. I think it's safe to get rid of the self.

@atk
Copy link

atk commented Sep 18, 2011

The magic limit is 4294967296, so we'll need to fit the 10 keycodes 38, 38, 40, 40, 37, 37, 39, 39, 65, 66 inside it.

@tsaniel
Copy link
Author

tsaniel commented Oct 12, 2011

Thanks to the string coercion with array literal trick, we save another byte.

@plugnburn
Copy link

Just 78 bytes:

function(f,a){onkeyup=function(e){/113302022928$/.test(a+=[e.which-37])&&f()}}

@atk
Copy link

atk commented Apr 11, 2014

If you set a as 0, you can get away with

function(f,a){document.onkeyup=function(e){(a+=[(e||self.event).keyCode-37])%1e12==113302022928&&f()}}

// as in
(function(f,a){document.onkeyup=function(e){(a+=[(e||self.event).keyCode-37])%1e12==113302022928&&f()}})(callback, 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment