Cleaned up and fixed the Windows keyboard mapping code.
authorSam Lantinga <slouken@libsdl.org>
Thu, 28 Feb 2013 21:40:08 -0800
changeset 6938b272d47f70a7
parent 6937 6a0e4dc3a489
child 6939 06a72f2f8b16
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
src/video/windows/SDL_windowskeyboard.c
src/video/windows/SDL_windowskeyboard.h
src/video/windows/SDL_windowsvideo.h
     1.1 --- a/src/video/windows/SDL_windowsevents.c	Thu Feb 28 20:01:17 2013 -0800
     1.2 +++ b/src/video/windows/SDL_windowsevents.c	Thu Feb 28 21:40:08 2013 -0800
     1.3 @@ -28,13 +28,12 @@
     1.4  #include "SDL_vkeys.h"
     1.5  #include "../../events/SDL_events_c.h"
     1.6  #include "../../events/SDL_touch_c.h"
     1.7 +#include "../../events/scancodes_windows.h"
     1.8  
     1.9  /* Dropfile support */
    1.10  #include <shellapi.h>
    1.11  
    1.12  
    1.13 -
    1.14 -
    1.15  /*#define WMMSG_DEBUG*/
    1.16  #ifdef WMMSG_DEBUG
    1.17  #include <stdio.h>	
    1.18 @@ -68,7 +67,7 @@
    1.19  #endif
    1.20  
    1.21  static SDL_Scancode 
    1.22 -WindowsScanCodeToSDLScanCode( int lParam, int wParam, const SDL_Scancode *key_map )
    1.23 +WindowsScanCodeToSDLScanCode( int lParam, int wParam )
    1.24  {
    1.25  	SDL_Scancode code;
    1.26  	char bIsExtended;
    1.27 @@ -133,7 +132,7 @@
    1.28  	if ( nScanCode > 127 )
    1.29  		return SDL_SCANCODE_UNKNOWN;
    1.30  
    1.31 -	code = key_map[nScanCode];
    1.32 +	code = windows_scancode_table[nScanCode];
    1.33  
    1.34  	bIsExtended = ( lParam & ( 1 << 24 ) ) != 0;
    1.35  	if ( !bIsExtended )
    1.36 @@ -159,7 +158,7 @@
    1.37  		case SDL_SCANCODE_INSERT:
    1.38  			return SDL_SCANCODE_KP_0;
    1.39  		case SDL_SCANCODE_DELETE:
    1.40 -			return SDL_SCANCODE_KP_DECIMAL;
    1.41 +			return SDL_SCANCODE_KP_PERIOD;
    1.42  		case SDL_SCANCODE_PRINTSCREEN:
    1.43  			return SDL_SCANCODE_KP_MULTIPLY;
    1.44  		default:
    1.45 @@ -393,7 +392,7 @@
    1.46      case WM_SYSKEYDOWN:
    1.47      case WM_KEYDOWN:
    1.48          {
    1.49 -			SDL_Scancode code =  WindowsScanCodeToSDLScanCode( lParam, wParam, data->videodata->key_layout );
    1.50 +			SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
    1.51  			if ( code != SDL_SCANCODE_UNKNOWN ) {
    1.52                  SDL_SendKeyboardKey(SDL_PRESSED, code );
    1.53              }
    1.54 @@ -404,15 +403,13 @@
    1.55      case WM_SYSKEYUP:
    1.56      case WM_KEYUP:
    1.57          {
    1.58 -			SDL_Scancode code =  WindowsScanCodeToSDLScanCode( lParam, wParam, data->videodata->key_layout );
    1.59 -			if ( code != SDL_SCANCODE_UNKNOWN ) {
    1.60 -				if (wParam == VK_SNAPSHOT
    1.61 -				    && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
    1.62 -				    SDL_RELEASED) {
    1.63 -				    SDL_SendKeyboardKey(SDL_PRESSED,
    1.64 -				                         code);
    1.65 -				}
    1.66 -                SDL_SendKeyboardKey(SDL_RELEASED, code );
    1.67 +            SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
    1.68 +            if ( code != SDL_SCANCODE_UNKNOWN ) {
    1.69 +                if (code == SDL_SCANCODE_PRINTSCREEN &&
    1.70 +                    SDL_GetKeyboardState(NULL)[code] == SDL_RELEASED) {
    1.71 +                    SDL_SendKeyboardKey(SDL_PRESSED, code);
    1.72 +                }
    1.73 +                SDL_SendKeyboardKey(SDL_RELEASED, code);
    1.74              }
    1.75          }
    1.76          returnCode = 0;
     2.1 --- a/src/video/windows/SDL_windowskeyboard.c	Thu Feb 28 20:01:17 2013 -0800
     2.2 +++ b/src/video/windows/SDL_windowskeyboard.c	Thu Feb 28 21:40:08 2013 -0800
     2.3 @@ -48,50 +48,10 @@
     2.4  #endif
     2.5  
     2.6  /* Alphabetic scancodes for PC keyboards */
     2.7 -BYTE alpha_scancodes[26] = {
     2.8 -    30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
     2.9 -    25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
    2.10 -};
    2.11 -
    2.12 -BYTE keypad_scancodes[10] = {
    2.13 -    82, 79, 80, 81, 75, 76, 77, 71, 72, 73
    2.14 -};
    2.15 -
    2.16  void
    2.17  WIN_InitKeyboard(_THIS)
    2.18  {
    2.19      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    2.20 -    int i;
    2.21 -
    2.22 -    /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
    2.23 -    if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
    2.24 -#if 0
    2.25 -        printf
    2.26 -            ("Fixing alpha scancode map, assuming US QWERTY layout!\nPlease send the following 26 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
    2.27 -#endif
    2.28 -        for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
    2.29 -            alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
    2.30 -#if 0
    2.31 -            printf("%d = %d\n", i, alpha_scancodes[i]);
    2.32 -#endif
    2.33 -        }
    2.34 -    }
    2.35 -    if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
    2.36 -#if 0
    2.37 -        printf
    2.38 -            ("Fixing keypad scancode map!\nPlease send the following 10 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
    2.39 -#endif
    2.40 -        for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
    2.41 -            keypad_scancodes[i] =
    2.42 -                MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
    2.43 -#if 0
    2.44 -            printf("%d = %d\n", i, keypad_scancodes[i]);
    2.45 -#endif
    2.46 -        }
    2.47 -    }
    2.48 -
    2.49 -	// windows scancode to SDL scancode table
    2.50 -    data->key_layout = windows_scancode_table;
    2.51  
    2.52      data->ime_com_initialized = SDL_FALSE;
    2.53      data->ime_threadmgr = 0;
    2.54 @@ -151,42 +111,36 @@
    2.55      SDL_Scancode scancode;
    2.56      SDL_Keycode keymap[SDL_NUM_SCANCODES];
    2.57  
    2.58 -	for (i = 0; i < SDL_arraysize(keymap); ++i)
    2.59 -	{
    2.60 -		keymap[i] = SDL_SCANCODE_TO_KEYCODE( i );
    2.61 -	}
    2.62 +    SDL_GetDefaultKeymap(keymap);
    2.63  
    2.64      for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) {
    2.65 -		int vk;
    2.66 +        int vk;
    2.67          /* Make sure this scancode is a valid character scancode */
    2.68          scancode = windows_scancode_table[i];
    2.69          if (scancode == SDL_SCANCODE_UNKNOWN ) {
    2.70              continue;
    2.71          }
    2.72 -		/* Don't allow the number keys right above the qwerty row to translate or the top left key (grave/backquote) */
    2.73 -		/* not mapping numbers fixes the AZERTY layout (french) causing non-shifted number to appear by default */
    2.74 -		if ( scancode == SDL_SCANCODE_GRAVE ) {
    2.75 -			keymap[scancode]  = SDLK_BACKQUOTE;
    2.76 -			continue;
    2.77 -		}
    2.78 -		if ( scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_0 ) {
    2.79 -			 keymap[scancode] = SDLK_1 + ( scancode - SDL_SCANCODE_1 );
    2.80 -			continue;
    2.81 -		}
    2.82  
    2.83 -		vk =  MapVirtualKey(i, MAPVK_VSC_TO_VK);
    2.84 -		if ( vk ) {
    2.85 -			int ch;
    2.86 -			ch = (MapVirtualKey( vk, MAPVK_VK_TO_CHAR ) & 0x7FFF);
    2.87 -			 if ( ch )
    2.88 -			 {
    2.89 -				 if ( ch >= 'A' && ch <= 'Z' )
    2.90 -					keymap[scancode] =  SDLK_a + ( ch - 'A' );
    2.91 -				 else
    2.92 -					 keymap[scancode] = ch;
    2.93 -			 }
    2.94 +        /* If this key is one of the non-mappable keys, ignore it */
    2.95 +        /* Don't allow the number keys right above the qwerty row to translate or the top left key (grave/backquote) */
    2.96 +        /* Not mapping numbers fixes the French layout, giving numeric keycodes for the number keys, which is the expected behavior */
    2.97 +        if ((keymap[scancode] & SDLK_SCANCODE_MASK) ||
    2.98 +            scancode == SDL_SCANCODE_GRAVE ||
    2.99 +            (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_0) ) {
   2.100 +            continue;
   2.101 +        }
   2.102  
   2.103 -		}
   2.104 +        vk =  MapVirtualKey(i, MAPVK_VSC_TO_VK);
   2.105 +        if ( vk ) {
   2.106 +            int ch = (MapVirtualKey( vk, MAPVK_VK_TO_CHAR ) & 0x7FFF);
   2.107 +            if ( ch ) {
   2.108 +                if ( ch >= 'A' && ch <= 'Z' ) {
   2.109 +                    keymap[scancode] =  SDLK_a + ( ch - 'A' );
   2.110 +                } else {
   2.111 +                    keymap[scancode] = ch;
   2.112 +                }
   2.113 +            }
   2.114 +        }
   2.115      }
   2.116  
   2.117      SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
     3.1 --- a/src/video/windows/SDL_windowskeyboard.h	Thu Feb 28 20:01:17 2013 -0800
     3.2 +++ b/src/video/windows/SDL_windowskeyboard.h	Thu Feb 28 21:40:08 2013 -0800
     3.3 @@ -23,9 +23,6 @@
     3.4  #ifndef _SDL_windowskeyboard_h
     3.5  #define _SDL_windowskeyboard_h
     3.6  
     3.7 -extern BYTE alpha_scancodes[26];
     3.8 -extern BYTE keypad_scancodes[10];
     3.9 -
    3.10  extern void WIN_InitKeyboard(_THIS);
    3.11  extern void WIN_UpdateKeymap(void);
    3.12  extern void WIN_QuitKeyboard(_THIS);
     4.1 --- a/src/video/windows/SDL_windowsvideo.h	Thu Feb 28 20:01:17 2013 -0800
     4.2 +++ b/src/video/windows/SDL_windowsvideo.h	Thu Feb 28 21:40:08 2013 -0800
     4.3 @@ -115,7 +115,6 @@
     4.4  {
     4.5      int render;
     4.6  
     4.7 -    const SDL_Scancode *key_layout;
     4.8  	DWORD clipboard_count;
     4.9  
    4.10  	/* Touch input functions */