Last active
August 17, 2018 17:06
-
-
Save jRimbault/fc2d2a5ab5fa331869119d4c601bd35e to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Trigger callback when the user inputs the konami code | |
* @param {Function} callback | |
* @returns {Function} event handler | |
*/ | |
function konami(callback) { | |
'use strict' | |
let kkeys = [] | |
// up,up,down,down,left,right,left,right,B,A | |
const konami = '38,38,40,40,37,39,37,39,66,65' | |
return event => { | |
kkeys.push(event.keyCode) | |
if (kkeys.toString().indexOf(konami) >= 0) { | |
callback(event) | |
kkeys = [] | |
} | |
} | |
} | |
const handler = konami(console.log) | |
window.addEventListener('keydown', handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment