From a840990bdcabf45fb0d377478ba0ab27222434ae Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Mon, 23 Dec 2024 18:32:29 +0100 Subject: initial commit --- src/engine/keyboard.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/engine/keyboard.js (limited to 'src/engine/keyboard.js') 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; + } +} -- cgit v1.2.3