summaryrefslogtreecommitdiff
path: root/client/lib/keys.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-03-03 05:38:35 +0100
committerJules Laplace <julescarbon@gmail.com>2018-03-03 05:38:35 +0100
commit636b62aa6d0d77e19a4b7bb3c039924eeb217a71 (patch)
tree042fa3404738e347f8d60f419fa600aa6030c61b /client/lib/keys.js
hall demo
Diffstat (limited to 'client/lib/keys.js')
-rw-r--r--client/lib/keys.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/client/lib/keys.js b/client/lib/keys.js
new file mode 100644
index 0000000..5b98ace
--- /dev/null
+++ b/client/lib/keys.js
@@ -0,0 +1,41 @@
+const keys = {}
+const key_numbers = {}
+const letters = "yxcvbnmasdfghjklqwertuiopz"
+const numbers = "1234567890"
+
+let callback = function(){}
+
+letters.toUpperCase().split("").map(function(k,i){
+ keys[k.charCodeAt(0)] = i
+})
+
+keys['Z'.charCodeAt(0)] = 0
+
+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