From c10baf286c8296e5e8d2d906d6f30c91f015ed69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gr=C3=A9goire?= Date: Fri, 6 Nov 2026 07:43:04 +0100 Subject: [PATCH] Use the keyboard layout for shortcut letters in the Emscripten build. The shortcut letters were mapped from the physical key, so Ctrl+A needed the key where QWERTY has A, which is Q on AZERTY. Letters now come from `key`, so Ctrl+A is where the layout has A. Other keys, and letters outside of the Latin alphabet, still use the physical key. The character of a key can change before it is released (releasing AltGr first), so the key sent on the press is remembered for each physical key and released on key up. This works in every browser. --- profiler/src/BackendEmscripten.cpp | 43 +++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/profiler/src/BackendEmscripten.cpp b/profiler/src/BackendEmscripten.cpp index 33e6d8c3..df33b500 100644 --- a/profiler/src/BackendEmscripten.cpp +++ b/profiler/src/BackendEmscripten.cpp @@ -199,6 +199,43 @@ static ImGuiKey TranslateKeyCode( const char* code ) return ImGuiKey_None; } +// What each physical key sent to Dear ImGui when it was pressed. +// The character of a key can change before it is released (AltGr let go first), so the release can't work it out again. +// Two things are left as they are, because the fixes cost more than the bugs: +// - Two keys can send the same ImGuiKey (the key labelled A, and AltGr plus the key at QWERTY's A). Letting go of one +// releases it for both. Counting the presses would fix it, with a second table. +// - Nothing is cleared when the window loses focus. The next press of a key overwrites its old entry anyway. +static ImGuiKey s_sentKey[ImGuiKey_NamedKey_COUNT]; + +// Letters follow the layout, so Ctrl+Z is on the key labelled Z. Everything else uses the physical key, +// including letters that aren't A-Z. +static ImGuiKey TranslateKeyDown( const EmscriptenKeyboardEvent* e ) +{ + const ImGuiKey physical = TranslateKeyCode( e->code ); + if( physical == ImGuiKey_None ) return ImGuiKey_None; + ImGuiKey& sent = s_sentKey[physical - ImGuiKey_NamedKey_BEGIN]; + if( !e->repeat || sent == ImGuiKey_None ) + { + const char c = e->key[0]; + const bool letter = !e->key[1] && ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ); + const ImGuiKey key = letter ? (ImGuiKey)( ImGuiKey_A + ( c & 0x1f ) - 1 ) : physical; + // The key up was missed, so release the old key now. + if( sent != ImGuiKey_None && sent != key ) ImGui::GetIO().AddKeyEvent( sent, false ); + sent = key; + } + return sent; +} + +static ImGuiKey TranslateKeyUp( const EmscriptenKeyboardEvent* e ) +{ + const ImGuiKey physical = TranslateKeyCode( e->code ); + if( physical == ImGuiKey_None ) return ImGuiKey_None; + ImGuiKey& sent = s_sentKey[physical - ImGuiKey_NamedKey_BEGIN]; + const ImGuiKey key = sent != ImGuiKey_None ? sent : physical; + sent = ImGuiKey_None; + return key; +} + // Ctrl+V, Cmd+V and Shift+Insert. static bool IsPasteShortcut( const EmscriptenKeyboardEvent* e, ImGuiKey key ) { @@ -302,13 +339,13 @@ Backend::Backend( const char* title, const std::function& redraw, const } ); emscripten_set_keydown_callback( EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE, [] ( int, const EmscriptenKeyboardEvent* e, void* ) -> EM_BOOL { UpdateKeyModifiers( e ); - const auto code = TranslateKeyCode( e->code ); + const auto code = TranslateKeyDown( e ); if( code == ImGuiKey_None ) return EM_FALSE; ImGui::GetIO().AddKeyEvent( code, true ); // Let the browser handle the shortcut, or it won't send the paste event. if( IsPasteShortcut( e, code ) ) { - // If it doesn't, let the paste event fake it. + // If Dear ImGui won't paste for this chord, the paste event fakes a Ctrl+V. s_pasteShortcut = ImGuiPastes( e, code ); return EM_FALSE; } @@ -317,7 +354,7 @@ Backend::Backend( const char* title, const std::function& redraw, const } ); emscripten_set_keyup_callback( EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE, [] ( int, const EmscriptenKeyboardEvent* e, void* ) -> EM_BOOL { UpdateKeyModifiers( e ); - const auto code = TranslateKeyCode( e->code ); + const auto code = TranslateKeyUp( e ); if( code == ImGuiKey_None ) return EM_FALSE; ImGui::GetIO().AddKeyEvent( code, false ); return EM_TRUE;