Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created September 2, 2011 12:17
Show Gist options
  • 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>
@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