annotate src/video/win32/SDL_win32keyboard.c @ 2311:54e21acfec5a

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