Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Cleaned up and fixed the Windows keyboard mapping code.
Browse files Browse the repository at this point in the history
Use KP_PERIOD instead of KP_DECIMAL
Don't remap keys which are always keycode named versions of scancodes
  • Loading branch information
slouken committed Mar 1, 2013
1 parent 89af5d1 commit 756ff05
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 88 deletions.
27 changes: 12 additions & 15 deletions src/video/windows/SDL_windowsevents.c
Expand Up @@ -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 <shellapi.h>




/*#define WMMSG_DEBUG*/
#ifdef WMMSG_DEBUG
#include <stdio.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand All @@ -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:
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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;
Expand Down
92 changes: 23 additions & 69 deletions src/video/windows/SDL_windowskeyboard.c
Expand Up @@ -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 <sdl@libsdl.org>, 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 <sdl@libsdl.org>, 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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/video/windows/SDL_windowskeyboard.h
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/video/windows/SDL_windowsvideo.h
Expand Up @@ -115,7 +115,6 @@ typedef struct SDL_VideoData
{
int render;

const SDL_Scancode *key_layout;
DWORD clipboard_count;

/* Touch input functions */
Expand Down

0 comments on commit 756ff05

Please sign in to comment.