Last active
June 7, 2022 22:08
-
-
Save nihlton/647fa58e2aaa6bd72120378e57441afd to your computer and use it in GitHub Desktop.
useKonamiCode
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
const code = [ | |
'ArrowUp', 'ArrowUp', 'ArrowDown', | |
'ArrowDown', 'ArrowLeft', 'ArrowRight', | |
'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA' | |
]; | |
const useKonami = () => { | |
const [isActive, setActive] = useState(false); | |
useEffect(() => { | |
let cache = []; | |
const handler = (e) => { | |
cache = [...cache, e.code].slice(-code.length); | |
cache.join() === code.join() && setActive(true); | |
} | |
document.addEventListener('keydown', handler); | |
return () => document.removeEventListener('keydown', handler); | |
}, []) | |
return isActive; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment