Skip to content

Instantly share code, notes, and snippets.

@robozevel
Last active May 10, 2019 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robozevel/e8d6eec927ef71e6c57b52b35a5f1943 to your computer and use it in GitHub Desktop.
Save robozevel/e8d6eec927ef71e6c57b52b35a5f1943 to your computer and use it in GitHub Desktop.
function KonamiCodeMixin (callback) {
const KEYS = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA']
let i = 0
return {
mounted () {
window.addEventListener('keyup', this.onKeyUp)
},
destroyed () {
window.removeEventListener('keyup', this.onKeyUp)
},
methods: {
onKeyUp ({ code }) {
i = KEYS[i] === code ? i + 1 : 0
if (i !== KEYS.length) return
i = 0
if (typeof callback === 'string') this[callback]()
if (typeof callback === 'function') callback.call(this)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment