From 46171045ed133762926394df4e2dff2381fcef3c Mon Sep 17 00:00:00 2001 From: David Fort Date: Thu, 11 May 2023 18:17:26 +0200 Subject: [PATCH] [winpr,input] fix GetKeycodeFromVirtualKeyCode(code, KEYCODE_TYPE_XKB) As KEYCODE_TYPE_XKB is 3, in the previous code we were doing some "and masking" and so when calling GetKeycodeFromVirtualKeyCode(code, KEYCODE_TYPE_XKB), the function was always interpreting the virtual key code with the apple layout. This patch fixes that and also mutualize the search in the code array. --- winpr/libwinpr/input/keycode.c | 57 ++++++++++++++-------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/winpr/libwinpr/input/keycode.c b/winpr/libwinpr/input/keycode.c index 0b2449c2e..141b789eb 100644 --- a/winpr/libwinpr/input/keycode.c +++ b/winpr/libwinpr/input/keycode.c @@ -878,41 +878,32 @@ DWORD GetVirtualKeyCodeFromKeycode(DWORD keycode, DWORD dwFlags) DWORD GetKeycodeFromVirtualKeyCode(DWORD vkcode, DWORD dwFlags) { DWORD index; - DWORD keycode = 0; + DWORD* targetArray; + size_t targetSize; - if (dwFlags & KEYCODE_TYPE_APPLE) + switch (dwFlags) { - for (index = 0; index < ARRAYSIZE(KEYCODE_TO_VKCODE_APPLE); index++) - { - if (vkcode == KEYCODE_TO_VKCODE_APPLE[index]) - { - keycode = index; - break; - } - } - } - else if (dwFlags & KEYCODE_TYPE_EVDEV) - { - for (index = 0; index < ARRAYSIZE(KEYCODE_TO_VKCODE_EVDEV); index++) - { - if (vkcode == KEYCODE_TO_VKCODE_EVDEV[index]) - { - keycode = index; - break; - } - } - } - else if (dwFlags & KEYCODE_TYPE_XKB) - { - for (index = 0; index < ARRAYSIZE(KEYCODE_TO_VKCODE_XKB); index++) - { - if (vkcode == KEYCODE_TO_VKCODE_XKB[index]) - { - keycode = index; - break; - } - } + case KEYCODE_TYPE_APPLE: + targetArray = KEYCODE_TO_VKCODE_APPLE; + targetSize = ARRAYSIZE(KEYCODE_TO_VKCODE_APPLE); + break; + case KEYCODE_TYPE_EVDEV: + targetArray = KEYCODE_TO_VKCODE_EVDEV; + targetSize = ARRAYSIZE(KEYCODE_TO_VKCODE_EVDEV); + break; + case KEYCODE_TYPE_XKB: + targetArray = KEYCODE_TO_VKCODE_XKB; + targetSize = ARRAYSIZE(KEYCODE_TO_VKCODE_XKB); + break; + default: + return 0; } - return keycode; + for (index = 0; index < targetSize; index++) + { + if (vkcode == targetArray[index]) + return index; + } + + return 0; }