From 1ad8ad623e7ec16f9ffb6b86ddf68bfd8deb43b9 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Wed, 23 Mar 2022 15:01:57 +0200 Subject: [PATCH] Backends: GLFW: Fixed keyboard modifiers events being reported incorrectly on Linux/X11. --- backends/imgui_impl_glfw.cpp | 19 ++++++++++++++++++- docs/CHANGELOG.txt | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/backends/imgui_impl_glfw.cpp b/backends/imgui_impl_glfw.cpp index 6cdf74948c26..5670f106ee0e 100644 --- a/backends/imgui_impl_glfw.cpp +++ b/backends/imgui_impl_glfw.cpp @@ -16,7 +16,8 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after iniitializing backend. +// 2022-03-23: Inputs: Fixed a regression in 1.87 which resulted in keyboard modifiers events being reported incorrectly on Linux/X11. +// 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after initializing backend. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago)with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). @@ -244,6 +245,19 @@ static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key) } } +static int ImGui_ImplGlfw_KeyToModifier(int key) +{ + if (key == GLFW_KEY_LEFT_CONTROL || key == GLFW_KEY_RIGHT_CONTROL) + return GLFW_MOD_CONTROL; + if (key == GLFW_KEY_LEFT_SHIFT || key == GLFW_KEY_RIGHT_SHIFT) + return GLFW_MOD_SHIFT; + if (key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_RIGHT_ALT) + return GLFW_MOD_ALT; + if (key == GLFW_KEY_LEFT_SUPER || key == GLFW_KEY_RIGHT_SUPER) + return GLFW_MOD_SUPER; + return 0; +} + static void ImGui_ImplGlfw_UpdateKeyModifiers(int mods) { ImGuiIO& io = ImGui::GetIO(); @@ -312,6 +326,9 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, i if (action != GLFW_PRESS && action != GLFW_RELEASE) return; + // Workaround: X11 does not include current pressed/released modifier key in 'mods' flags. https://github.com/glfw/glfw/issues/1630 + if (int keycode_to_mod = ImGui_ImplGlfw_KeyToModifier(keycode)) + mods = (action == GLFW_PRESS) ? (mods | keycode_to_mod) : (mods & ~keycode_to_mod); ImGui_ImplGlfw_UpdateKeyModifiers(mods); keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode); diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 1b95abc0d3fe..9fb774ff3a71 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -70,6 +70,8 @@ Other Changes: - Misc: Updated stb_rect_pack.h from 1.00 to 1.01 (minor). (#5075) - Misc: binary_to_compressed_c tool: Added -nostatic option. (#5021) [@podsvirov] - ImVector: Fixed erase() with empty range. (#5009) [@thedmd] +- Backends: GLFW: Fixed a regression in 1.87 which resulted in keyboard modifiers events being + reported incorrectly on Linux/X11, due to a bug in GLFW. [@rokups] - Backends: SDL: Fixed dragging out viewport broken on some SDL setups. (#5012) [@rokups] - Backends: SDL: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2). (#5125) [@sgiurgiu] - Backends: OSX: Monitor NSKeyUp events to catch missing keyUp for key when user press Cmd + key (#5128) [@thedmd]