diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-10-05 16:36:12 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-10-05 16:36:12 +0200 |
| commit | 852ed2e007deac47292d3e83a374070683c29894 (patch) | |
| tree | cacff78f2bc63c77cdb458863fc576043561adb1 /client/lib/keys.js | |
| parent | 0ca3983dd9e00a93cc1ed1c55b2ad7a4a6b14bf2 (diff) | |
inequality client
Diffstat (limited to 'client/lib/keys.js')
| -rw-r--r-- | client/lib/keys.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/client/lib/keys.js b/client/lib/keys.js new file mode 100644 index 0000000..c9e51ac --- /dev/null +++ b/client/lib/keys.js @@ -0,0 +1,39 @@ +const keys = {} +const key_numbers = {} +const letters = "zxcvbnmasdfghjklqwertyuiop" +const numbers = "1234567890" + +let callback = function(){} + +letters.toUpperCase().split("").map(function(k,i){ + keys[k.charCodeAt(0)] = i +}) + +numbers.split("").map(function(k,i){ + keys[k.charCodeAt(0)] = i+letters.length + key_numbers[k.charCodeAt(0)] = true +}) + +window.addEventListener("keydown", keydown, true) +function keydown (e) { + if (e.altKey || e.ctrlKey || e.metaKey) { + e.stopPropagation() + return + } + if (document.activeElement instanceof HTMLInputElement && + (e.keyCode in key_numbers)) { + e.stopPropagation() + return + } + if (! (e.keyCode in keys)) return + var index = keys[e.keyCode] + if (e.shiftKey) index += letters.length + index -= 7 + callback(index) +} + +function listen (fn) { + callback = fn +} + +export default { listen }
\ No newline at end of file |
