WIN_UpdateKeymap: cleanup code a bit

This commit is contained in:
Dimitriy Ryazantcev
2024-03-13 17:04:03 +02:00
committed by Sam Lantinga
parent 0a86f8eb6e
commit 1973edb9b7
+17 -29
View File
@@ -115,13 +115,15 @@ void WIN_UpdateKeymap(SDL_bool send_event)
int i; int i;
SDL_Scancode scancode; SDL_Scancode scancode;
SDL_Keycode keymap[SDL_NUM_SCANCODES]; SDL_Keycode keymap[SDL_NUM_SCANCODES];
BYTE keyboardState[256] = { 0 };
WCHAR buffer[16];
SDL_GetDefaultKeymap(keymap); SDL_GetDefaultKeymap(keymap);
WIN_ResetDeadKeys(); WIN_ResetDeadKeys();
for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) { for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) {
Uint8 vk; int vk, sc, result;
Uint16 sc; Uint32 *ch = 0;
/* Make sure this scancode is a valid character scancode */ /* Make sure this scancode is a valid character scancode */
scancode = windows_scancode_table[i]; scancode = windows_scancode_table[i];
@@ -142,35 +144,21 @@ void WIN_UpdateKeymap(SDL_bool send_event)
continue; continue;
} }
/* Always map VK_A..VK_Z to SDLK_a..SDLK_z codes. result = ToUnicode(vk, sc, keyboardState, buffer, 16, 0);
* This was behavior with MapVirtualKey(MAPVK_VK_TO_CHAR). */ buffer[SDL_abs(result)] = 0;
//if (vk >= 'A' && vk <= 'Z') {
// keymap[scancode] = SDLK_a + (vk - 'A');
//} else {
{
BYTE keyboardState[256] = { 0 };
WCHAR buffer[16] = { 0 };
Uint32 *ch = 0;
int result = ToUnicode(vk, sc, keyboardState, buffer, 16, 0);
buffer[SDL_abs(result)] = 0;
/* Convert UTF-16 to UTF-32 code points */ /* Convert UTF-16 to UTF-32 code points */
ch = (Uint32 *)SDL_iconv_string("UTF-32LE", "UTF-16LE", (const char *)buffer, (SDL_abs(result) + 1) * sizeof(WCHAR)); ch = (Uint32 *)SDL_iconv_string("UTF-32LE", "UTF-16LE", (const char *)buffer, (SDL_abs(result) + 1) * sizeof(WCHAR));
if (ch) { if (ch) {
if (ch[0] != 0 && ch[1] != 0) { /* Windows keyboard layouts can emit several UTF-32 code points on a single key press.
/* We have several UTF-32 code points on a single key press. * Use <U+FFFD REPLACEMENT CHARACTER> since we cannot fit into single SDL_Keycode value in SDL keymap.
* Cannot fit into single SDL_Keycode in keymap. * See https://kbdlayout.info/features/ligatures for a list of such keys. */
* See https://kbdlayout.info/features/ligatures */ keymap[scancode] = ch[1] == 0 ? ch[0] : 0xfffd;
keymap[scancode] = 0xfffd; /* U+FFFD REPLACEMENT CHARACTER */ SDL_free(ch);
} else { }
keymap[scancode] = ch[0];
}
SDL_free(ch);
}
if (result < 0) { if (result < 0) {
WIN_ResetDeadKeys(); WIN_ResetDeadKeys();
}
} }
} }