Skip to content

Instantly share code, notes, and snippets.

@anyley
Created April 9, 2018 22:18
Show Gist options
  • Save anyley/2a2c413c2cb5efca639404a580efdec6 to your computer and use it in GitHub Desktop.
Save anyley/2a2c413c2cb5efca639404a580efdec6 to your computer and use it in GitHub Desktop.
const TIMEOUT = 500
patterns = {
's-r': [83, 82]
}
handler = e => {
// console.log(e.which)
clearTimeout(handler.timer)
handler.keys[e.which] = true
const result = Object.entries(patterns).find(([seq, pattern]) => pattern.every(key => handler.keys.hasOwnProperty(key)))
if (result) {
window.dispatchEvent(new CustomEvent('keyseq', { detail: result[0] }))
handler.keys = {}
}
handler.timer = setTimeout(() => {
handler.keys = {}
console.log('CLEAR BUFFER')
}, TIMEOUT)
}
handler.timer = null
handler.keys = {}
window.addEventListener('keydown', handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment