src/video/win32/SDL_win32keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 09 Feb 2008 22:28:27 +0000
changeset 2311 54e21acfec5a
parent 2308 514f7c1651fc
child 2317 f537a293b3da
permissions -rw-r--r--
Friggin' Windows remaps alphabetic keys based on keyboard layout.
We try to figure out what the actual layout independent values are.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_win32video.h"
    25 
    26 #include "../../events/SDL_keyboard_c.h"
    27 #include "../../events/scancodes_win32.h"
    28 
    29 #ifndef MAPVK_VK_TO_VSC
    30 #define MAPVK_VK_TO_VSC     0
    31 #endif
    32 #ifndef MAPVK_VSC_TO_VK
    33 #define MAPVK_VSC_TO_VK     1
    34 #endif
    35 #ifndef MAPVK_VK_TO_CHAR
    36 #define MAPVK_VK_TO_CHAR    2
    37 #endif
    38 
    39 /* Alphabetic scancodes for PC keyboards */
    40 BYTE alpha_scancodes[26] = {
    41     30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
    42     25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
    43 };
    44 
    45 void
    46 WIN_InitKeyboard(_THIS)
    47 {
    48     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    49     SDL_Keyboard keyboard;
    50     int i;
    51 
    52     /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
    53     if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T'-'A']) {
    54 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");
    55         for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
    56             alpha_scancodes[i] = MapVirtualKey('A'+i, MAPVK_VK_TO_VSC);
    57 printf("%d = %d\n", i, alpha_scancodes[i]);
    58         }
    59     }
    60 
    61     data->key_layout = win32_scancode_table;
    62 
    63     SDL_zero(keyboard);
    64     data->keyboard = SDL_AddKeyboard(&keyboard, -1);
    65     WIN_UpdateKeymap(data->keyboard);
    66 
    67     SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
    68     SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
    69     SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
    70 }
    71 
    72 void
    73 WIN_UpdateKeymap(int keyboard)
    74 {
    75     int i;
    76     SDL_scancode scancode;
    77     SDLKey keymap[SDL_NUM_SCANCODES];
    78 
    79     SDL_GetDefaultKeymap(keymap);
    80 
    81     for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
    82 
    83         /* Make sure this scancode is a valid character scancode */
    84         scancode = win32_scancode_table[i];
    85         if (scancode == SDL_SCANCODE_UNKNOWN ||
    86             (keymap[scancode] & SDLK_SCANCODE_MASK)) {
    87             continue;
    88         }
    89 
    90         /* Alphabetic keys are handled specially, since Windows remaps them */
    91         if (i >= 'A' && i <= 'Z') {
    92             BYTE vsc = alpha_scancodes[i-'A'];
    93             keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
    94         } else {
    95             keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
    96         }
    97     }
    98     SDL_SetKeymap(keyboard, 0, keymap, SDL_NUM_SCANCODES);
    99 }
   100 
   101 void
   102 WIN_QuitKeyboard(_THIS)
   103 {
   104     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   105 
   106     SDL_DelKeyboard(data->keyboard);
   107 }
   108 
   109 /* vi: set ts=4 sw=4 expandtab: */