Last active
September 14, 2021 17:53
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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