src/video/win32/SDL_win32keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 Aug 2008 05:26:28 +0000
changeset 2724 0e2b65f32298
parent 2324 3202e4826c57
child 2726 f23ebf1ddac4
permissions -rw-r--r--
Added Wacom API headers.
FIXME: Dynamically load the Wacom API functions from wintab32.dll
     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 BYTE keypad_scancodes[10] = {
    45     82, 79, 80, 81, 75, 76, 77, 71, 72, 73
    46 };
    47 
    48 void
    49 WIN_InitKeyboard(_THIS)
    50 {
    51     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    52     SDL_Keyboard keyboard;
    53     int i;
    54 
    55     /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
    56     if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
    57 #if 0
    58         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");
    59 #endif
    60         for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
    61             alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
    62 #if 0
    63             printf("%d = %d\n", i, alpha_scancodes[i]);
    64 #endif
    65         }
    66     }
    67     if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
    68 #if 0
    69         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");
    70 #endif
    71         for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
    72             keypad_scancodes[i] =
    73                 MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
    74 #if 0
    75             printf("%d = %d\n", i, keypad_scancodes[i]);
    76 #endif
    77         }
    78     }
    79 
    80     data->key_layout = win32_scancode_table;
    81 
    82     SDL_zero(keyboard);
    83     data->keyboard = SDL_AddKeyboard(&keyboard, -1);
    84     WIN_UpdateKeymap(data->keyboard);
    85 
    86     SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
    87     SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
    88     SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
    89 }
    90 
    91 void
    92 WIN_UpdateKeymap(int keyboard)
    93 {
    94     int i;
    95     SDL_scancode scancode;
    96     SDLKey keymap[SDL_NUM_SCANCODES];
    97 
    98     SDL_GetDefaultKeymap(keymap);
    99 
   100     for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
   101 
   102         /* Make sure this scancode is a valid character scancode */
   103         scancode = win32_scancode_table[i];
   104         if (scancode == SDL_SCANCODE_UNKNOWN ||
   105             (keymap[scancode] & SDLK_SCANCODE_MASK)) {
   106             continue;
   107         }
   108 
   109         /* Alphabetic keys are handled specially, since Windows remaps them */
   110         if (i >= 'A' && i <= 'Z') {
   111             BYTE vsc = alpha_scancodes[i - 'A'];
   112             keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
   113         } else {
   114             keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
   115         }
   116     }
   117     SDL_SetKeymap(keyboard, 0, keymap, SDL_NUM_SCANCODES);
   118 }
   119 
   120 void
   121 WIN_QuitKeyboard(_THIS)
   122 {
   123     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   124 
   125     SDL_DelKeyboard(data->keyboard);
   126 }
   127 
   128 /* vi: set ts=4 sw=4 expandtab: */