summaryrefslogtreecommitdiff
path: root/src/engine/keyboard.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/keyboard.js')
-rw-r--r--src/engine/keyboard.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/engine/keyboard.js b/src/engine/keyboard.js
new file mode 100644
index 0000000..d634519
--- /dev/null
+++ b/src/engine/keyboard.js
@@ -0,0 +1,23 @@
+export function setUpKeyboard() {
+ document.addEventListener("keyup", k => trigger(k))
+ document.addEventListener("keydown", k => trigger(k))
+}
+
+export const keys = {
+ right: false,
+ left: false,
+ up: false,
+ down: false,
+ shoot: false,
+}
+
+function trigger(event) {
+ let on = event.type == "keydown"
+ switch (event.key) {
+ case "ArrowRight": keys.right = on; break;
+ case "ArrowLeft": keys.left = on; break;
+ case "ArrowUp": keys.up = on; break;
+ case "ArrowDown": keys.down = on; break;
+ case "z": case "Z": keys.shoot = on; break;
+ }
+}