From 896aee7ebd56b01e2e2c4c0280ff8789b7750300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adriano=20Anast=C3=A1cio?= Date: Mon, 27 Jul 2026 17:20:54 +0100 Subject: [PATCH] Added key detection --- lib/js/{consoleApi.js => jsApi.js} | 0 lib/odin/js_input/input.odin | 36 ++++++++++++++++++++++++++++++ src/game.odin | 8 +++++-- template/web/index.html | 4 ++-- 4 files changed, 44 insertions(+), 4 deletions(-) rename lib/js/{consoleApi.js => jsApi.js} (100%) create mode 100644 lib/odin/js_input/input.odin diff --git a/lib/js/consoleApi.js b/lib/js/jsApi.js similarity index 100% rename from lib/js/consoleApi.js rename to lib/js/jsApi.js diff --git a/lib/odin/js_input/input.odin b/lib/odin/js_input/input.odin new file mode 100644 index 0000000..9e5fc63 --- /dev/null +++ b/lib/odin/js_input/input.odin @@ -0,0 +1,36 @@ +package js_input + +import "core:sys/wasm/js" +import "core:log" + +DEFAULT_INPUT_EVENT_CALLBACKS :: Input_Event_Callbacks{ + key_down = register_key_down_callback, + key_up = register_key_up_callback, +} + +Input_State :: struct { + // store keys +} + +Input_Event_Callbacks :: struct { + key_down: proc(e: js.Event), + key_up: proc(e: js.Event), +} + +init_input :: proc(input: ^Input_State, callbacks := DEFAULT_INPUT_EVENT_CALLBACKS) { + js.add_document_event_listener(js.Event_Kind.Key_Down, input, callbacks.key_down) + js.add_document_event_listener(js.Event_Kind.Key_Up, input, callbacks.key_up) +} + +register_key_down_callback :: proc(event: js.Event) { + log.infof("Key down: %s", event.key.key) +} + +register_key_up_callback :: proc(event: js.Event) { + d := get_user_data_as(Input_State, event) + log.infof("Key up: %s", event.key.key) +} + +get_user_data_as :: proc($T: typeid, e: js.Event) -> ^T { + return cast(^T)e.user_data +} \ No newline at end of file diff --git a/src/game.odin b/src/game.odin index 9781290..a42f941 100644 --- a/src/game.odin +++ b/src/game.odin @@ -1,17 +1,21 @@ package game -import js_console "../lib/odin/js_console" +import "../lib/odin/js_console" +import "../lib/odin/js_input" import log "core:log" import "base:runtime" +input_state: js_input.Input_State + init :: proc(ctx: ^runtime.Context) { ctx.logger = js_console.create_js_console_logger() log.info("Initialized") + + js_input.init_input(&input_state) } // return false to exit update :: proc() -> bool { - log.info("Running frame") return true } diff --git a/template/web/index.html b/template/web/index.html index a502cb5..929855e 100644 --- a/template/web/index.html +++ b/template/web/index.html @@ -9,7 +9,7 @@ - +