Added key detection

This commit is contained in:
2026-07-27 17:20:54 +01:00
parent 0f97ae9293
commit 896aee7ebd
4 changed files with 44 additions and 4 deletions
+36
View File
@@ -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
}
+6 -2
View File
@@ -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
}
+2 -2
View File
@@ -9,7 +9,7 @@
</head>
<body>
<script type="text/javascript" src="consoleApi.js"></script>
<script type="text/javascript" src="jsApi.js"></script>
<script type="text/javascript" src="odin.js"></script>
<script type="text/javascript">
"use strict";
@@ -17,7 +17,7 @@
let mem = new odin.WasmMemoryInterface()
let apis = {
'console_api' : new ConsoleApi(mem),
console_api : new ConsoleApi(mem),
};
await odin.runWasm("./game.wasm", null, apis, mem);