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
This commit is contained in:
Sam Lantinga
2024-08-03 22:40:37 -07:00
parent ad27d73a19
commit a82f19f690

View File

@@ -21,6 +21,7 @@
#include "SDL_internal.h" #include "SDL_internal.h"
#include "SDL_keymap_c.h" #include "SDL_keymap_c.h"
#include "SDL_keyboard_c.h"
#include "../SDL_hashtable.h" #include "../SDL_hashtable.h"
struct SDL_Keymap struct SDL_Keymap
@@ -1006,12 +1007,22 @@ const char *SDL_GetKeyName(SDL_Keycode key)
case SDLK_DELETE: case SDLK_DELETE:
return SDL_GetScancodeName(SDL_SCANCODE_DELETE); return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
default: default:
/* Unaccented letter keys on latin keyboards are normally // SDL_Keycode is defined as the unshifted key on the keyboard,
labeled in upper case (and probably on others like Greek or // but the key name is defined as the letter printed on that key,
Cyrillic too, so if you happen to know for sure, please // which is usually the shifted capital letter.
adapt this). */ if (key > 0x7F || (key >= 'a' && key <= 'z')) {
if (key >= 'a' && key <= 'z') { SDL_bool translated = SDL_FALSE;
key -= 32; 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); end = SDL_UCS4ToUTF8(key, name);