From fff359a46fcbb6def2f79b23f5b8654c024d9853 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 28 Feb 2013 21:40:08 -0800 Subject: [PATCH] Cleaned up and fixed the Windows keyboard mapping code. Use KP_PERIOD instead of KP_DECIMAL Don't remap keys which are always keycode named versions of scancodes --- src/video/windows/SDL_windowsevents.c | 27 ++++---- src/video/windows/SDL_windowskeyboard.c | 88 ++++++------------------- src/video/windows/SDL_windowskeyboard.h | 3 - src/video/windows/SDL_windowsvideo.h | 1 - 4 files changed, 33 insertions(+), 86 deletions(-) diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c index 5c2be9ad8..9887a0bcb 100644 --- a/src/video/windows/SDL_windowsevents.c +++ b/src/video/windows/SDL_windowsevents.c @@ -28,13 +28,12 @@ #include "SDL_vkeys.h" #include "../../events/SDL_events_c.h" #include "../../events/SDL_touch_c.h" +#include "../../events/scancodes_windows.h" /* Dropfile support */ #include - - /*#define WMMSG_DEBUG*/ #ifdef WMMSG_DEBUG #include @@ -68,7 +67,7 @@ #endif static SDL_Scancode -WindowsScanCodeToSDLScanCode( int lParam, int wParam, const SDL_Scancode *key_map ) +WindowsScanCodeToSDLScanCode( int lParam, int wParam ) { SDL_Scancode code; char bIsExtended; @@ -133,7 +132,7 @@ WindowsScanCodeToSDLScanCode( int lParam, int wParam, const SDL_Scancode *key_ma if ( nScanCode > 127 ) return SDL_SCANCODE_UNKNOWN; - code = key_map[nScanCode]; + code = windows_scancode_table[nScanCode]; bIsExtended = ( lParam & ( 1 << 24 ) ) != 0; if ( !bIsExtended ) @@ -159,7 +158,7 @@ WindowsScanCodeToSDLScanCode( int lParam, int wParam, const SDL_Scancode *key_ma case SDL_SCANCODE_INSERT: return SDL_SCANCODE_KP_0; case SDL_SCANCODE_DELETE: - return SDL_SCANCODE_KP_DECIMAL; + return SDL_SCANCODE_KP_PERIOD; case SDL_SCANCODE_PRINTSCREEN: return SDL_SCANCODE_KP_MULTIPLY; default: @@ -393,7 +392,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_SYSKEYDOWN: case WM_KEYDOWN: { - SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam, data->videodata->key_layout ); + SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam ); if ( code != SDL_SCANCODE_UNKNOWN ) { SDL_SendKeyboardKey(SDL_PRESSED, code ); } @@ -404,15 +403,13 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_SYSKEYUP: case WM_KEYUP: { - SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam, data->videodata->key_layout ); - if ( code != SDL_SCANCODE_UNKNOWN ) { - if (wParam == VK_SNAPSHOT - && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] == - SDL_RELEASED) { - SDL_SendKeyboardKey(SDL_PRESSED, - code); - } - SDL_SendKeyboardKey(SDL_RELEASED, code ); + SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam ); + if ( code != SDL_SCANCODE_UNKNOWN ) { + if (code == SDL_SCANCODE_PRINTSCREEN && + SDL_GetKeyboardState(NULL)[code] == SDL_RELEASED) { + SDL_SendKeyboardKey(SDL_PRESSED, code); + } + SDL_SendKeyboardKey(SDL_RELEASED, code); } } returnCode = 0; diff --git a/src/video/windows/SDL_windowskeyboard.c b/src/video/windows/SDL_windowskeyboard.c index fd2510413..74f078f86 100644 --- a/src/video/windows/SDL_windowskeyboard.c +++ b/src/video/windows/SDL_windowskeyboard.c @@ -48,50 +48,10 @@ static void IME_Quit(SDL_VideoData *videodata); #endif /* Alphabetic scancodes for PC keyboards */ -BYTE alpha_scancodes[26] = { - 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24, - 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44 -}; - -BYTE keypad_scancodes[10] = { - 82, 79, 80, 81, 75, 76, 77, 71, 72, 73 -}; - void WIN_InitKeyboard(_THIS) { SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; - int i; - - /* Make sure the alpha scancodes are correct. T isn't usually remapped */ - if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) { -#if 0 - printf - ("Fixing alpha scancode map, assuming US QWERTY layout!\nPlease send the following 26 lines of output to the SDL mailing list , including a description of your keyboard hardware.\n"); -#endif - for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) { - alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC); -#if 0 - printf("%d = %d\n", i, alpha_scancodes[i]); -#endif - } - } - if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) { -#if 0 - printf - ("Fixing keypad scancode map!\nPlease send the following 10 lines of output to the SDL mailing list , including a description of your keyboard hardware.\n"); -#endif - for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) { - keypad_scancodes[i] = - MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC); -#if 0 - printf("%d = %d\n", i, keypad_scancodes[i]); -#endif - } - } - - // windows scancode to SDL scancode table - data->key_layout = windows_scancode_table; data->ime_com_initialized = SDL_FALSE; data->ime_threadmgr = 0; @@ -151,42 +111,36 @@ WIN_UpdateKeymap() SDL_Scancode scancode; SDL_Keycode keymap[SDL_NUM_SCANCODES]; - for (i = 0; i < SDL_arraysize(keymap); ++i) - { - keymap[i] = SDL_SCANCODE_TO_KEYCODE( i ); - } + SDL_GetDefaultKeymap(keymap); for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) { - int vk; + int vk; /* Make sure this scancode is a valid character scancode */ scancode = windows_scancode_table[i]; if (scancode == SDL_SCANCODE_UNKNOWN ) { continue; } - /* Don't allow the number keys right above the qwerty row to translate or the top left key (grave/backquote) */ - /* not mapping numbers fixes the AZERTY layout (french) causing non-shifted number to appear by default */ - if ( scancode == SDL_SCANCODE_GRAVE ) { - keymap[scancode] = SDLK_BACKQUOTE; - continue; - } - if ( scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_0 ) { - keymap[scancode] = SDLK_1 + ( scancode - SDL_SCANCODE_1 ); - continue; - } - vk = MapVirtualKey(i, MAPVK_VSC_TO_VK); - if ( vk ) { - int ch; - ch = (MapVirtualKey( vk, MAPVK_VK_TO_CHAR ) & 0x7FFF); - if ( ch ) - { - if ( ch >= 'A' && ch <= 'Z' ) - keymap[scancode] = SDLK_a + ( ch - 'A' ); - else - keymap[scancode] = ch; - } + /* If this key is one of the non-mappable keys, ignore it */ + /* Don't allow the number keys right above the qwerty row to translate or the top left key (grave/backquote) */ + /* Not mapping numbers fixes the French layout, giving numeric keycodes for the number keys, which is the expected behavior */ + if ((keymap[scancode] & SDLK_SCANCODE_MASK) || + scancode == SDL_SCANCODE_GRAVE || + (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_0) ) { + continue; + } - } + vk = MapVirtualKey(i, MAPVK_VSC_TO_VK); + if ( vk ) { + int ch = (MapVirtualKey( vk, MAPVK_VK_TO_CHAR ) & 0x7FFF); + if ( ch ) { + if ( ch >= 'A' && ch <= 'Z' ) { + keymap[scancode] = SDLK_a + ( ch - 'A' ); + } else { + keymap[scancode] = ch; + } + } + } } SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); diff --git a/src/video/windows/SDL_windowskeyboard.h b/src/video/windows/SDL_windowskeyboard.h index abe8a63e9..c79d1d0ff 100644 --- a/src/video/windows/SDL_windowskeyboard.h +++ b/src/video/windows/SDL_windowskeyboard.h @@ -23,9 +23,6 @@ #ifndef _SDL_windowskeyboard_h #define _SDL_windowskeyboard_h -extern BYTE alpha_scancodes[26]; -extern BYTE keypad_scancodes[10]; - extern void WIN_InitKeyboard(_THIS); extern void WIN_UpdateKeymap(void); extern void WIN_QuitKeyboard(_THIS); diff --git a/src/video/windows/SDL_windowsvideo.h b/src/video/windows/SDL_windowsvideo.h index 8a3c737ed..199a8ba8f 100644 --- a/src/video/windows/SDL_windowsvideo.h +++ b/src/video/windows/SDL_windowsvideo.h @@ -115,7 +115,6 @@ typedef struct SDL_VideoData { int render; - const SDL_Scancode *key_layout; DWORD clipboard_count; /* Touch input functions */