From 536d4e75fe54b9f2b060b0e52f7af627b7a80d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 15 Mar 2024 17:05:28 +0100 Subject: [PATCH] Platform: emit both the text input and key press event in EmscriptenApp. This is what both SDL and GLFW do, so it makes sense to be consistent. Without it it's also impossible to handle keyboard shortcuts such as Ctrl-C when editing text, which is rather silly. --- doc/changelog.dox | 6 ++++++ src/Magnum/Platform/EmscriptenApplication.cpp | 21 ++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 96c11b0d4..7a6652d70 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -1194,6 +1194,12 @@ See also: work on Firefox again. The reported renderer string got changed, possibly in relation to @webgl_extension{WEBGL,debug_renderer_info} being deprecated, which broke Firefox detection in the workaround enabling code. +- @ref Platform::EmscriptenApplication was emitting only a text input event + if text input was active, not a key press event as well, making it + impossible to handle keyboard shortcuts such as copy / paste when editing + text. Now it emits both, consistently with @ref Platform::Sdl2Application + and @ref Platform::GlfwApplication, and in the same order as in those. See + [mosra/magnum#637](https://github.com/mosra/magnum/issues/637). @subsection changelog-latest-deprecated Deprecated APIs diff --git a/src/Magnum/Platform/EmscriptenApplication.cpp b/src/Magnum/Platform/EmscriptenApplication.cpp index 8ece3d0aa..95e6c2a21 100644 --- a/src/Magnum/Platform/EmscriptenApplication.cpp +++ b/src/Magnum/Platform/EmscriptenApplication.cpp @@ -583,16 +583,23 @@ void EmscriptenApplication::setupCallbacks(bool resizable) { ([](int, const EmscriptenKeyboardEvent* event, void* userData) -> Int { EmscriptenApplication& app = *static_cast(userData); const Containers::StringView key = event->key; + KeyEvent e{*event}; + app.keyPressEvent(e); + bool accepted = e.isAccepted(); + /* If the key name is a single letter or a start of an UTF-8 - sequence, pass it to the text input event as well */ + sequence, pass it to the text input event as well. Both SDL and + GLFW emit key press first and text input after, do it in the + same order here. */ if(app.isTextInputActive() && key.size() == 1 || (key.size() >= 1 && UnsignedByte(key[0]) > 127)) { - TextInputEvent e{*event, key}; - app.textInputEvent(e); - return e.isAccepted(); + TextInputEvent te{*event, key}; + app.textInputEvent(te); + accepted = accepted || te.isAccepted(); } - KeyEvent e{*event}; - app.keyPressEvent(e); - return e.isAccepted(); + + /* Accepting either the key event, the text input event, or both + should stop it from propagating further */ + return accepted; })); emscripten_set_keyup_callback(keyboardListeningElement, this, false,