From 0a9b03b9b52a3371147e739b5168df9f4888800e Mon Sep 17 00:00:00 2001 From: Fabian Vogt Date: Tue, 8 Oct 2024 22:44:44 +0200 Subject: [PATCH] Use scancode from key event for mapping release events to the keypad Fixes #348 --- qtkeypadbridge.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/qtkeypadbridge.cpp b/qtkeypadbridge.cpp index 091a0808..a4b62a0a 100644 --- a/qtkeypadbridge.cpp +++ b/qtkeypadbridge.cpp @@ -156,20 +156,17 @@ void keyToKeypad(QKeyEvent *event) ,{Qt::Key_Return, keymap::enter} }; - // Determine virtual key that correspond to the key we got - auto vkey = event->nativeVirtualKey(); + // Determine physical key that correspond to the key we got, + // to be able to get releases reliably if modifiers change in between: + // Press shift, press 2 (-> "), release shift, release 2 (-> 2) + // results in press of 2 but release of " (example for de layout). + auto physkey = event->nativeScanCode(); + if (physkey < 1) + physkey = event->key(); // (Bad) fallback to the virtual key if needed - // nativeVirtualKey should be working everywhere, but just in case it's not - if (vkey == 0 || vkey == 1) - vkey = event->nativeScanCode(); + auto pressed = pressed_keys.find(physkey); - // If neither of them worked then simply use key code - if (vkey == 0 || vkey == 1) - vkey = event->key(); - - auto pressed = pressed_keys.find(vkey); - - // If vkey is already pressed, then this must the the release event + // If physkey is already pressed, then this must the the release event if (pressed != pressed_keys.end()) { setKeypad(*pressed, false); @@ -197,7 +194,7 @@ void keyToKeypad(QKeyEvent *event) if (translated != QtKeyMap.end()) { - pressed_keys.insert(vkey, *translated); + pressed_keys.insert(physkey, *translated); setKeypad(*translated, true); } }