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.
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@2311
    44
slouken@1895
    45
void
slouken@1895
    46
WIN_InitKeyboard(_THIS)
slouken@1895
    47
{
slouken@1895
    48
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
    49
    SDL_Keyboard keyboard;
slouken@2311
    50
    int i;
slouken@2311
    51
slouken@2311
    52
    /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
slouken@2311
    53
    if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T'-'A']) {
slouken@2311
    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");
slouken@2311
    55
        for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
slouken@2311
    56
            alpha_scancodes[i] = MapVirtualKey('A'+i, MAPVK_VK_TO_VSC);
slouken@2311
    57
printf("%d = %d\n", i, alpha_scancodes[i]);
slouken@2311
    58
        }
slouken@2311
    59
    }
slouken@1895
    60
slouken@2308
    61
    data->key_layout = win32_scancode_table;
slouken@2308
    62
slouken@1895
    63
    SDL_zero(keyboard);
slouken@1895
    64
    data->keyboard = SDL_AddKeyboard(&keyboard, -1);
slouken@2311
    65
    WIN_UpdateKeymap(data->keyboard);
slouken@2308
    66
slouken@2308
    67
    SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
slouken@2308
    68
    SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
slouken@2308
    69
    SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
slouken@2308
    70
}
slouken@2308
    71
slouken@2308
    72
void
slouken@2311
    73
WIN_UpdateKeymap(int keyboard)
slouken@2308
    74
{
slouken@2308
    75
    int i;
slouken@2308
    76
    SDL_scancode scancode;
slouken@2308
    77
    SDLKey keymap[SDL_NUM_SCANCODES];
slouken@2308
    78
slouken@2308
    79
    SDL_GetDefaultKeymap(keymap);
slouken@2308
    80
slouken@2308
    81
    for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
slouken@2308
    82
slouken@2308
    83
        /* Make sure this scancode is a valid character scancode */
slouken@2308
    84
        scancode = win32_scancode_table[i];
slouken@2308
    85
        if (scancode == SDL_SCANCODE_UNKNOWN ||
slouken@2308
    86
            (keymap[scancode] & SDLK_SCANCODE_MASK)) {
slouken@2308
    87
            continue;
slouken@2308
    88
        }
slouken@2311
    89
slouken@2311
    90
        /* Alphabetic keys are handled specially, since Windows remaps them */
slouken@2311
    91
        if (i >= 'A' && i <= 'Z') {
slouken@2311
    92
            BYTE vsc = alpha_scancodes[i-'A'];
slouken@2311
    93
            keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
slouken@2311
    94
        } else {
slouken@2311
    95
            keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
slouken@2311
    96
        }
slouken@2308
    97
    }
slouken@2311
    98
    SDL_SetKeymap(keyboard, 0, keymap, SDL_NUM_SCANCODES);
slouken@1895
    99
}
slouken@1895
   100
slouken@1895
   101
void
slouken@1895
   102
WIN_QuitKeyboard(_THIS)
slouken@1895
   103
{
slouken@1895
   104
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   105
slouken@1895
   106
    SDL_DelKeyboard(data->keyboard);
slouken@1895
   107
}
slouken@1895
   108
slouken@1895
   109
/* vi: set ts=4 sw=4 expandtab: */