From 483966f6ed5f50e3b33d108a6eba48cc5e184eed Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Fri, 10 Nov 2023 19:24:02 -0500 Subject: [PATCH 1/4] [SDL backend] Use virtual key codes for editing keys for text --- lib/sdl/main_sdl.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/sdl/main_sdl.cpp b/lib/sdl/main_sdl.cpp index 0b5b3ba8401..bb604d0f3de 100644 --- a/lib/sdl/main_sdl.cpp +++ b/lib/sdl/main_sdl.cpp @@ -1413,49 +1413,49 @@ static void inputHandleKeyEvent(SDL_KeyboardEvent *keyEvent) case SDL_KEYDOWN: { unsigned vk = 0; - switch (keyEvent->keysym.scancode) + switch (keyEvent->keysym.sym) { // our "editing" keys for text - case SDL_SCANCODE_LEFT: + case SDLK_LEFT: vk = INPBUF_LEFT; break; - case SDL_SCANCODE_RIGHT: + case SDLK_RIGHT: vk = INPBUF_RIGHT; break; - case SDL_SCANCODE_UP: + case SDLK_UP: vk = INPBUF_UP; break; - case SDL_SCANCODE_DOWN: + case SDLK_DOWN: vk = INPBUF_DOWN; break; - case SDL_SCANCODE_HOME: + case SDLK_HOME: vk = INPBUF_HOME; break; - case SDL_SCANCODE_END: + case SDLK_END: vk = INPBUF_END; break; - case SDL_SCANCODE_INSERT: + case SDLK_INSERT: vk = INPBUF_INS; break; - case SDL_SCANCODE_DELETE: + case SDLK_DELETE: vk = INPBUF_DEL; break; - case SDL_SCANCODE_PAGEUP: + case SDLK_PAGEUP: vk = INPBUF_PGUP; break; - case SDL_SCANCODE_PAGEDOWN: + case SDLK_PAGEDOWN: vk = INPBUF_PGDN; break; - case SDL_SCANCODE_BACKSPACE: + case SDLK_BACKSPACE: vk = INPBUF_BKSPACE; break; - case SDL_SCANCODE_TAB: + case SDLK_TAB: vk = INPBUF_TAB; break; - case SDL_SCANCODE_RETURN: + case SDLK_RETURN: vk = INPBUF_CR; break; - case SDL_SCANCODE_ESCAPE: + case SDLK_ESCAPE: vk = INPBUF_ESC; break; default: @@ -1467,7 +1467,7 @@ static void inputHandleKeyEvent(SDL_KeyboardEvent *keyEvent) { // Take care of adding 'editing' keys that were pressed to the input buffer (for text editing control handling) inputAddBuffer(vk, 0); - debug(LOG_INPUT, "Editing key: 0x%x, %d SDLkey=[%s] pressed", vk, vk, SDL_GetScancodeName(currentKey)); + debug(LOG_INPUT, "Editing key: 0x%x, %d SDLkey=[%s] pressed", vk, vk, SDL_GetKeyName(keyEvent->keysym.sym)); } debug(LOG_INPUT, "Key Code (pressed): 0x%x, %d, SDLscancode=[%s]", currentKey, currentKey, SDL_GetScancodeName(currentKey)); From bf6d02422b06252d58a60b81ddcce5e949a8f6d3 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Fri, 10 Nov 2023 19:24:26 -0500 Subject: [PATCH 2/4] [SDL backend] Shortcut adding to buffer if text input isn't enabled --- lib/sdl/main_sdl.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/sdl/main_sdl.cpp b/lib/sdl/main_sdl.cpp index bb604d0f3de..64c17697af5 100644 --- a/lib/sdl/main_sdl.cpp +++ b/lib/sdl/main_sdl.cpp @@ -1093,6 +1093,12 @@ static InputKey *inputPointerNext(InputKey *p) /* add count copies of the characater code to the input buffer */ static void inputAddBuffer(UDWORD key, utf_32_char unicode) { + if (!GetTextEventsOwner) + { + // Text input events aren't enabled + return; + } + /* Calculate what pEndBuffer will be set to next */ InputKey *pNext = inputPointerNext(pEndBuffer); From 000b87e65bb0b341ea31ea0684855ce531891e59 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Fri, 10 Nov 2023 19:50:33 -0500 Subject: [PATCH 3/4] [SDL backend] Add virtual key codes to text input buffer (so CTRL+V et al is handled) --- lib/sdl/main_sdl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/sdl/main_sdl.cpp b/lib/sdl/main_sdl.cpp index 64c17697af5..71439b2ab7c 100644 --- a/lib/sdl/main_sdl.cpp +++ b/lib/sdl/main_sdl.cpp @@ -1468,14 +1468,19 @@ static void inputHandleKeyEvent(SDL_KeyboardEvent *keyEvent) break; } - SDL_Scancode currentKey = keyEvent->keysym.scancode; if (vk) { // Take care of adding 'editing' keys that were pressed to the input buffer (for text editing control handling) inputAddBuffer(vk, 0); debug(LOG_INPUT, "Editing key: 0x%x, %d SDLkey=[%s] pressed", vk, vk, SDL_GetKeyName(keyEvent->keysym.sym)); } + else + { + // add everything else + inputAddBuffer(keyEvent->keysym.sym, 0); + } + SDL_Scancode currentKey = keyEvent->keysym.scancode; debug(LOG_INPUT, "Key Code (pressed): 0x%x, %d, SDLscancode=[%s]", currentKey, currentKey, SDL_GetScancodeName(currentKey)); KEY_CODE code = sdlScancodeToKeyCode(currentKey); From eb8e55efe9c368e1750c0e365d95e8a2618a0b70 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Fri, 10 Nov 2023 20:08:34 -0500 Subject: [PATCH 4/4] [SDL backend] keyScanToString: Display the *virtual* key name --- lib/sdl/main_sdl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sdl/main_sdl.cpp b/lib/sdl/main_sdl.cpp index 71439b2ab7c..513b1e248f9 100644 --- a/lib/sdl/main_sdl.cpp +++ b/lib/sdl/main_sdl.cpp @@ -1113,6 +1113,7 @@ static void inputAddBuffer(UDWORD key, utf_32_char unicode) pEndBuffer = pNext; } +// Returns the human-readable name for the key *in the current keyboard layout* void keyScanToString(KEY_CODE code, char *ascii, UDWORD maxStringSize) { if (code == KEY_LCTRL) @@ -1146,7 +1147,7 @@ void keyScanToString(KEY_CODE code, char *ascii, UDWORD maxStringSize) if (code < KEY_MAXSCAN) { - snprintf(ascii, maxStringSize, "%s", SDL_GetScancodeName(keyCodeToSDLScancode(code))); + snprintf(ascii, maxStringSize, "%s", SDL_GetKeyName(SDL_GetKeyFromScancode(keyCodeToSDLScancode(code)))); if (ascii[0] >= 'a' && ascii[0] <= 'z' && ascii[1] != 0) { // capitalize