src/video/win32/SDL_win32keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 24 Jan 2010 21:10:53 +0000
changeset 3697 f7b03b6838cb
parent 2859 99210400e8b9
child 4465 3e69e077cb95
permissions -rw-r--r--
Fixed bug #926

Updated copyright to LGPL version 2.1 and year 2010
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@2735
    44
slouken@2317
    45
BYTE keypad_scancodes[10] = {
slouken@2317
    46
    82, 79, 80, 81, 75, 76, 77, 71, 72, 73
slouken@2317
    47
};
slouken@2311
    48
slouken@1895
    49
void
slouken@1895
    50
WIN_InitKeyboard(_THIS)
slouken@1895
    51
{
slouken@1895
    52
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
    53
    SDL_Keyboard keyboard;
slouken@2311
    54
    int i;
slouken@2311
    55
slouken@2311
    56
    /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
bob@2324
    57
    if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
slouken@2724
    58
#if 0
slouken@2726
    59
        printf
slouken@2726
    60
            ("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@2724
    61
#endif
slouken@2311
    62
        for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
bob@2324
    63
            alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
slouken@2724
    64
#if 0
bob@2324
    65
            printf("%d = %d\n", i, alpha_scancodes[i]);
slouken@2724
    66
#endif
slouken@2311
    67
        }
slouken@2311
    68
    }
slouken@2317
    69
    if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
slouken@2724
    70
#if 0
slouken@2726
    71
        printf
slouken@2726
    72
            ("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@2724
    73
#endif
slouken@2317
    74
        for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
bob@2324
    75
            keypad_scancodes[i] =
bob@2324
    76
                MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
slouken@2724
    77
#if 0
bob@2324
    78
            printf("%d = %d\n", i, keypad_scancodes[i]);
slouken@2724
    79
#endif
slouken@2317
    80
        }
slouken@2317
    81
    }
slouken@1895
    82
slouken@2308
    83
    data->key_layout = win32_scancode_table;
slouken@2308
    84
slouken@1895
    85
    SDL_zero(keyboard);
slouken@1895
    86
    data->keyboard = SDL_AddKeyboard(&keyboard, -1);
slouken@2311
    87
    WIN_UpdateKeymap(data->keyboard);
slouken@2308
    88
slouken@2308
    89
    SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
slouken@2308
    90
    SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
slouken@2308
    91
    SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
slouken@2308
    92
}
slouken@2308
    93
slouken@2308
    94
void
slouken@2311
    95
WIN_UpdateKeymap(int keyboard)
slouken@2308
    96
{
slouken@2308
    97
    int i;
slouken@2308
    98
    SDL_scancode scancode;
slouken@2308
    99
    SDLKey keymap[SDL_NUM_SCANCODES];
slouken@2308
   100
slouken@2308
   101
    SDL_GetDefaultKeymap(keymap);
slouken@2308
   102
slouken@2308
   103
    for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
slouken@2308
   104
slouken@2308
   105
        /* Make sure this scancode is a valid character scancode */
slouken@2308
   106
        scancode = win32_scancode_table[i];
slouken@2308
   107
        if (scancode == SDL_SCANCODE_UNKNOWN ||
slouken@2308
   108
            (keymap[scancode] & SDLK_SCANCODE_MASK)) {
slouken@2308
   109
            continue;
slouken@2308
   110
        }
slouken@2311
   111
slouken@2311
   112
        /* Alphabetic keys are handled specially, since Windows remaps them */
slouken@2311
   113
        if (i >= 'A' && i <= 'Z') {
bob@2324
   114
            BYTE vsc = alpha_scancodes[i - 'A'];
slouken@2311
   115
            keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
slouken@2311
   116
        } else {
slouken@2311
   117
            keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
slouken@2311
   118
        }
slouken@2308
   119
    }
slouken@2311
   120
    SDL_SetKeymap(keyboard, 0, keymap, SDL_NUM_SCANCODES);
slouken@1895
   121
}
slouken@1895
   122
slouken@1895
   123
void
slouken@1895
   124
WIN_QuitKeyboard(_THIS)
slouken@1895
   125
{
slouken@1895
   126
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   127
slouken@1895
   128
    SDL_DelKeyboard(data->keyboard);
slouken@1895
   129
}
slouken@1895
   130
slouken@1895
   131
/* vi: set ts=4 sw=4 expandtab: */