Created
April 9, 2018 22:18
-
-
Save anyley/2a2c413c2cb5efca639404a580efdec6 to your computer and use it in GitHub Desktop.
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 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