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