Skip to content

Instantly share code, notes, and snippets.

@brandonaaskov
Last active June 8, 2017 15:41
Show Gist options
  • Save brandonaaskov/58c0c65a07a52e5632b7b339fec1ef55 to your computer and use it in GitHub Desktop.
Save brandonaaskov/58c0c65a07a52e5632b7b339fec1ef55 to your computer and use it in GitHub Desktop.
A very straightforward example of leveraging the konami code (uses isEqual from lodash because it reads easier)
const konamiCode = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13]
let konamiTracker = []
const listener = event => {
const key = event.keyCode
const currentSequence = [...konamiTracker, key]
const slice = konamiCode.slice(0, currentSequence.length)
const isPartialMatch = _.isEqual(currentSequence, slice)
const isExactMatch = _.isEqual(currentSequence, konamiCode)
konamiTracker = isPartialMatch ? konamiTracker.concat([key]) : []
if (isExactMatch) console.log('execute the thing')
}
window.konami = window.addEventListener('keydown', listener)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment