Skip to content

Instantly share code, notes, and snippets.

@JRiggles
Last active September 14, 2021 17:53
Show Gist options
  • Save JRiggles/f3afe3c687cff302506c879b32bdd346 to your computer and use it in GitHub Desktop.
Save JRiggles/f3afe3c687cff302506c879b32bdd346 to your computer and use it in GitHub Desktop.
Calls the given callback() function when the Konami Code is entered. The 'keyPhrase' array can be modified to whatever you like!
function konami(callback) {
const keyPhrase = [
"ArrowUp", "ArrowUp", "ArrowDown", "ArrowDown", "ArrowLeft", "ArrowRight", "ArrowLeft", "ArrowRight", "b", "a", "Enter"
];
let index = 0;
$(document).on("keydown", (k) => { // get the key that was pressed
k.key === keyPhrase[index] ? index++ : (index = 0); // goto next index for each correct key press
if (index === keyPhrase.length) { // if the entire keyPhrase is entered correctly...
index = 0; // clear the index so we can use this function again
callback(); // call the passed-in function
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment