Last active
May 10, 2019 12:13
-
-
Save robozevel/e8d6eec927ef71e6c57b52b35a5f1943 to your computer and use it in GitHub Desktop.
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 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