From b12ca99506c7538a74cb2ed39ce0e1781e934a2a Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Fri, 8 Nov 2024 08:56:45 +0100 Subject: Update input --- index.htm | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'index.htm') diff --git a/index.htm b/index.htm index 3b1fd3b..2c42d74 100644 --- a/index.htm +++ b/index.htm @@ -98,10 +98,11 @@ if (!wait_for_events) { if (sleep_duration > 0) { + let timeout = sleep_duration; sleep_duration = 0; setTimeout(() => { window.requestAnimationFrame(animation_frame); - }, sleep_duration); + }, timeout); } else window.requestAnimationFrame(animation_frame); } @@ -126,24 +127,42 @@ window.requestAnimationFrame(animation_frame); }); - canvas.addEventListener("mouseenter", (ev) => { - console.log(ev); - window.requestAnimationFrame(animation_frame); - }); + let key_from_code = (code) => { + switch (code) { + case "KeyA": return 97; + } + return 0; + }; - canvas.addEventListener("mouseleave", (ev) => { - console.log(ev); - window.requestAnimationFrame(animation_frame); - }); + let mod_from_event = (ev) => { + let mod = 0; + if (ev.ctrlKey) mod |= 1; + if (ev.shiftKey) mod |= 2; + if (ev.altKey) mod |= 4; + if (ev.metaKey) mod |= 8; + return mod; + }; window.addEventListener("keydown", (ev) => { - console.log(ev); - window.requestAnimationFrame(animation_frame); + ev.preventDefault(); + if (!ev.repeat) { + let mod = mod_from_event(ev); + let key = key_from_code(ev.code); + if (key != 0) + program.instance.exports.js_keydown(key, mod); + window.requestAnimationFrame(animation_frame); + } }); window.addEventListener("keyup", (ev) => { - console.log(ev); - window.requestAnimationFrame(animation_frame); + ev.preventDefault(); + if (!ev.repeat) { + let mod = mod_from_event(ev); + let key = key_from_code(ev.code); + if (key != 0) + program.instance.exports.js_keyup(key, mod); + window.requestAnimationFrame(animation_frame); + } }); window.requestAnimationFrame(animation_frame); -- cgit v1.2.3