From 0fa5c8c06c3891b36a27945ce0488d423c917f41 Mon Sep 17 00:00:00 2001 From: Dannii Willis Date: Sat, 18 May 2024 11:29:48 +1000 Subject: [PATCH] Working on mobile input --- arthur.html | 3 ++- journey.html | 3 ++- sfrotz.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- shogun.html | 3 ++- styles.css | 5 +++++ zorkzero.html | 3 ++- 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/arthur.html b/arthur.html index e6a9467..2099da8 100644 --- a/arthur.html +++ b/arthur.html @@ -7,8 +7,9 @@ -

Arthur: The Quest for Excalibur

+

Arthur

+

Other games on Infocom Frotz

diff --git a/journey.html b/journey.html index 5c81ad4..d5be6cb 100644 --- a/journey.html +++ b/journey.html @@ -7,8 +7,9 @@ -

Journey: The Quest Begins

+

Journey

+

Other games on Infocom Frotz

diff --git a/sfrotz.js b/sfrotz.js index d8dbf1a..78e50be 100644 --- a/sfrotz.js +++ b/sfrotz.js @@ -32,6 +32,7 @@ if (Module['ENVIRONMENT']) { // Infocom Frotz Emscripten preamble const files = window.files +const textinput_elem = document.getElementById('textinput') Module.arguments = ['/' + files[0], '/' + files[1]] Module.canvas = document.getElementById('canvas') @@ -8729,7 +8730,10 @@ var ASM_CONSTS = { var keyEventHandlerFunc = (e) => { assert(e); - + console.log('keyEventHandlerFunc', e) + if (e.which === 229) { + return + } var keyEventData = JSEvents.keyEvent; HEAPF64[((keyEventData)>>3)] = e.timeStamp; @@ -8761,8 +8765,49 @@ var ASM_CONSTS = { }; return JSEvents.registerOrRemoveHandler(eventHandler); }; - var _emscripten_set_keydown_callback_on_thread = (target, userData, useCapture, callbackfunc, targetThread) => - registerKeyEventCallback(target, userData, useCapture, callbackfunc, 2, "keydown", targetThread); + var _emscripten_set_keydown_callback_on_thread = function emscripten_set_keydown_callback_on_thread(target, userData, useCapture, callbackfunc, targetThread) {console.log('emscripten_set_keydown_callback_on_thread', target, userData, useCapture, callbackfunc) + registerKeyEventCallback(target, userData, useCapture, callbackfunc, 2, "keydown", targetThread) + + // Input handling for mobile + // Process input events + if (userData) { + textinput_elem.addEventListener('input', input_handler) + } + else { + textinput_elem.removeEventListener('input', input_handler) + } + // Ignore keydown events which return 229 - which is most events on mobile Chrome + /*textinput_elem.addEventListener('keydown', ev => { + console.log('keydown event', ev) + if (ev.which === 229) { + ev.stopPropagation() + } + })*/ + }; + + function input_handler(ev) { + console.log('input event', ev) + const char = ev.data + textinput_elem.value = '' + if (char) { + const upper_key = char.toUpperCase() + const keyCode = upper_key.codePointAt(0) + const ev_options = { + code: 'Key' + upper_key, + key: char, + keyCode, + which: keyCode, + } + window.dispatchEvent(new KeyboardEvent('keydown', ev_options)) + window.dispatchEvent(new KeyboardEvent('keypress', ev_options)) + window.dispatchEvent(new KeyboardEvent('keyup', ev_options)) + } + // Even though we have reset and emptied the input, Android acts as though it still has spaces within it, and won't send backspace keydown events until the phantom spaces have all been deleted. Refocusing seems to fix it. + if (char === ' ') { + textinput_elem.blur() + textinput_elem.focus() + } + } var _emscripten_set_keypress_callback_on_thread = (target, userData, useCapture, callbackfunc, targetThread) => registerKeyEventCallback(target, userData, useCapture, callbackfunc, 1, "keypress", targetThread); diff --git a/shogun.html b/shogun.html index d97104e..7d2a48a 100644 --- a/shogun.html +++ b/shogun.html @@ -7,8 +7,9 @@ -

James Clavell's Shōgun

+

Shōgun

+

Other games on Infocom Frotz

diff --git a/styles.css b/styles.css index 3c67b31..d4ca8d4 100644 --- a/styles.css +++ b/styles.css @@ -25,4 +25,9 @@ a { #games h2 img { display: block; margin: 0 auto; +} + +#textinput { + position: absolute; + left: -10000px; } \ No newline at end of file diff --git a/zorkzero.html b/zorkzero.html index 09c4e55..7d6968c 100644 --- a/zorkzero.html +++ b/zorkzero.html @@ -7,8 +7,9 @@ -

Zork Zero: The Revenge of Megaboz

+

Zork Zero

+

Other games on Infocom Frotz