From 2bf20e38b05e8aea457e077a4411b85c190f6eda Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Fri, 18 Mar 2022 07:20:06 +1000 Subject: [PATCH] wasm: fix android input when fast typing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes two issues: When user fast types, more than one character would get sent sometimes. Also, occasionally, 'Process' would appear along side of the typed character. Change-Id: I2ea3bfcbe987703bcbf298e0a0301bed6b8fccb3 Done-with: Aleksandr Reviakin Pick-to: 6.3 Reviewed-by: David Skoland Reviewed-by: Morten Johan Sørvig --- .../platforms/wasm/qwasminputcontext.cpp | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/wasm/qwasminputcontext.cpp b/src/plugins/platforms/wasm/qwasminputcontext.cpp index 20a6da9ebd..9ab7bfa4d9 100644 --- a/src/plugins/platforms/wasm/qwasminputcontext.cpp +++ b/src/plugins/platforms/wasm/qwasminputcontext.cpp @@ -43,13 +43,22 @@ using namespace qstdweb; static void inputCallback(emscripten::val event) { - QString str = QString::fromStdString(event["target"]["value"].as()); - QWasmInputContext *wasmInput = - reinterpret_cast(event["target"]["data-context"].as()); - wasmInput->inputStringChanged(str, wasmInput); + // android sends all the characters typed since the user started typing in this element + int length = event["target"]["value"]["length"].as(); + if (length <= 0) + return; - // this stops suggestions - // but allows us to send only one character like a normal keyboard + // use only last character + emscripten::val _incomingCharVal = event["target"]["value"][length - 1]; + if (_incomingCharVal != emscripten::val::undefined() && _incomingCharVal != emscripten::val::null()) { + + QString str = QString::fromStdString(_incomingCharVal.as()); + QWasmInputContext *wasmInput = + reinterpret_cast(event["target"]["data-context"].as()); + wasmInput->inputStringChanged(str, wasmInput); + } + // this clears the input string, so backspaces do not send a character + // but stops suggestions event["target"].set("value", ""); } @@ -168,10 +177,12 @@ int QWasmInputContext::androidKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData) { + // we get Enter, Backspace and function keys via emscripten on target window Q_UNUSED(eventType) QString strKey(keyEvent->key); - if (strKey == "Unidentified") + if (strKey == "Unidentified" || strKey == "Process") return false; + QWasmInputContext *wasmInput = reinterpret_cast(userData); wasmInput->inputStringChanged(strKey, wasmInput);