From 2d45dce07513a60b26dee0da77e309685c5013bc Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 17 Nov 2023 20:27:42 +0100 Subject: [PATCH] fix: Shortcuts not working correctly with non-western keyboards --- lib/libimhex/include/hex/api/keybinding.hpp | 10 ++++- lib/libimhex/source/api/keybinding.cpp | 50 +++++++++------------ main/gui/source/window/window.cpp | 6 +-- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/lib/libimhex/include/hex/api/keybinding.hpp b/lib/libimhex/include/hex/api/keybinding.hpp index 58c0e27df..9c8db5ca6 100644 --- a/lib/libimhex/include/hex/api/keybinding.hpp +++ b/lib/libimhex/include/hex/api/keybinding.hpp @@ -189,7 +189,15 @@ namespace hex { } bool operator==(const Shortcut &other) const { - return this->m_keys == other.m_keys; + auto thisKeys = this->m_keys; + auto otherKeys = other.m_keys; + + thisKeys.erase(CurrentView); + thisKeys.erase(AllowWhileTyping); + otherKeys.erase(CurrentView); + otherKeys.erase(AllowWhileTyping); + + return thisKeys == otherKeys; } bool isLocal() const { diff --git a/lib/libimhex/source/api/keybinding.cpp b/lib/libimhex/source/api/keybinding.cpp index 048158949..1f2c776b6 100644 --- a/lib/libimhex/source/api/keybinding.cpp +++ b/lib/libimhex/source/api/keybinding.cpp @@ -42,44 +42,36 @@ namespace hex { return pressedShortcut; } - void ShortcutManager::process(const std::unique_ptr ¤tView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { - Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, focused, keyCode); - - if (keyCode != 0) - s_prevShortcut = Shortcut(pressedShortcut.getKeys()); - - if (s_paused) return; - - if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) - return; - - if (currentView->m_shortcuts.contains(pressedShortcut + AllowWhileTyping)) { - currentView->m_shortcuts[pressedShortcut + AllowWhileTyping].callback(); - } else if (currentView->m_shortcuts.contains(pressedShortcut)) { - if (!ImGui::GetIO().WantTextInput) - currentView->m_shortcuts[pressedShortcut].callback(); - } - } - - void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) { - Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode); - - if (keyCode != 0) - s_prevShortcut = pressedShortcut; - + static void processShortcut(const Shortcut &shortcut, const std::map &shortcuts) { if (s_paused) return; if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) return; - if (s_globalShortcuts.contains(pressedShortcut + AllowWhileTyping)) { - s_globalShortcuts[pressedShortcut + AllowWhileTyping].callback(); - } else if (s_globalShortcuts.contains(pressedShortcut)) { + if (shortcuts.contains(shortcut + AllowWhileTyping)) { + shortcuts.at(shortcut + AllowWhileTyping).callback(); + } else if (shortcuts.contains(shortcut)) { if (!ImGui::GetIO().WantTextInput) - s_globalShortcuts[pressedShortcut].callback(); + shortcuts.at(shortcut).callback(); } } + void ShortcutManager::process(const std::unique_ptr ¤tView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { + Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, focused, keyCode); + if (keyCode != 0) + s_prevShortcut = Shortcut(pressedShortcut.getKeys()); + + processShortcut(pressedShortcut, currentView->m_shortcuts); + } + + void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) { + Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode); + if (keyCode != 0) + s_prevShortcut = Shortcut(pressedShortcut.getKeys()); + + processShortcut(pressedShortcut, s_globalShortcuts); + } + void ShortcutManager::clearShortcuts() { s_globalShortcuts.clear(); } diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index e18f1ff59..810cadc5a 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -1044,7 +1044,7 @@ namespace hex { #if !defined(OS_WEB) // Register key press callback - glfwSetKeyCallback(this->m_window, [](GLFWwindow *window, int key, int scancode, int action, int mods) { + glfwSetKeyCallback(this->m_window, [](GLFWwindow *window, int key, int, int action, int mods) { hex::unused(mods); auto win = static_cast(glfwGetWindowUserPointer(window)); @@ -1057,10 +1057,6 @@ namespace hex { if (key == GLFW_KEY_UNKNOWN) return; - auto keyName = glfwGetKeyName(key, scancode); - if (keyName != nullptr) - key = std::toupper(keyName[0]); - if (action == GLFW_PRESS || action == GLFW_REPEAT) { if (key != GLFW_KEY_LEFT_CONTROL && key != GLFW_KEY_RIGHT_CONTROL && key != GLFW_KEY_LEFT_ALT && key != GLFW_KEY_RIGHT_ALT &&