From a82f19f6904ab036620a524ea93f3eba90003bf0 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 3 Aug 2024 22:40:37 -0700
Subject: [PATCH] Use the shifted version of the letter as the key name
SDL_Keycode is defined as the unshifted key on the keyboard, but the key name is defined as the letter printed on that key, which is usually the shifted capital letter.
Fixes https://github.com/libsdl-org/SDL/issues/5836
---
src/events/SDL_keymap.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/events/SDL_keymap.c b/src/events/SDL_keymap.c
index 18300d9d60200..53bd6edd5b0e5 100644
--- a/src/events/SDL_keymap.c
+++ b/src/events/SDL_keymap.c
@@ -21,6 +21,7 @@
#include "SDL_internal.h"
#include "SDL_keymap_c.h"
+#include "SDL_keyboard_c.h"
#include "../SDL_hashtable.h"
struct SDL_Keymap
@@ -1006,12 +1007,22 @@ const char *SDL_GetKeyName(SDL_Keycode key)
case SDLK_DELETE:
return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
default:
- /* Unaccented letter keys on latin keyboards are normally
- labeled in upper case (and probably on others like Greek or
- Cyrillic too, so if you happen to know for sure, please
- adapt this). */
- if (key >= 'a' && key <= 'z') {
- key -= 32;
+ // SDL_Keycode is defined as the unshifted key on the keyboard,
+ // but the key name is defined as the letter printed on that key,
+ // which is usually the shifted capital letter.
+ if (key > 0x7F || (key >= 'a' && key <= 'z')) {
+ SDL_bool translated = SDL_FALSE;
+ SDL_Scancode scancode = SDL_GetScancodeFromKey(key, SDL_KMOD_NONE);
+ if (scancode != SDL_SCANCODE_UNKNOWN) {
+ SDL_Keycode capital = SDL_GetKeyFromScancode(scancode, SDL_KMOD_SHIFT);
+ if (capital > 0x7F || (capital >= 'A' && capital <= 'Z')) {
+ key = capital;
+ translated = SDL_TRUE;
+ }
+ }
+ if (!translated && key >= 'a' && key <= 'z') {
+ key = 'A' + (key - 'a');
+ }
}
end = SDL_UCS4ToUTF8(key, name);