Initial revision
[blender.git] / source / gameengine / GamePlayer / common / windows / GPW_KeyboardDevice.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include "GPW_KeyboardDevice.h"
34
35
36 // Key code values not found in winuser.h
37 #ifndef VK_MINUS
38 #define VK_MINUS 0xBD
39 #endif // VK_MINUS
40 #ifndef VK_SEMICOLON
41 #define VK_SEMICOLON 0xBA
42 #endif // VK_SEMICOLON
43 #ifndef VK_PERIOD
44 #define VK_PERIOD 0xBE
45 #endif // VK_PERIOD
46 #ifndef VK_COMMA
47 #define VK_COMMA 0xBC
48 #endif // VK_COMMA
49 #ifndef VK_QUOTE
50 #define VK_QUOTE 0xDE
51 #endif // VK_QUOTE
52 #ifndef VK_BACK_QUOTE
53 #define VK_BACK_QUOTE 0xC0
54 #endif // VK_BACK_QUOTE
55 #ifndef VK_SLASH
56 #define VK_SLASH 0xBF
57 #endif // VK_SLASH
58 #ifndef VK_BACK_SLASH
59 #define VK_BACK_SLASH 0xDC
60 #endif // VK_BACK_SLASH
61 #ifndef VK_EQUALS
62 #define VK_EQUALS 0xBB
63 #endif // VK_EQUALS
64 #ifndef VK_OPEN_BRACKET
65 #define VK_OPEN_BRACKET 0xDB
66 #endif // VK_OPEN_BRACKET
67 #ifndef VK_CLOSE_BRACKET
68 #define VK_CLOSE_BRACKET 0xDD
69 #endif // VK_CLOSE_BRACKET
70
71
72
73 GPW_KeyboardDevice::GPW_KeyboardDevice(void)
74 {
75         m_seperateLeftRight = false;
76         m_seperateLeftRightInitialized = false;
77
78         m_reverseKeyTranslateTable['A'                             ] = KX_AKEY                    ;                  
79         m_reverseKeyTranslateTable['B'                             ] = KX_BKEY                    ;                  
80         m_reverseKeyTranslateTable['C'                             ] = KX_CKEY                    ;                  
81         m_reverseKeyTranslateTable['D'                             ] = KX_DKEY                    ;                  
82         m_reverseKeyTranslateTable['E'                             ] = KX_EKEY                    ;                  
83         m_reverseKeyTranslateTable['F'                             ] = KX_FKEY                    ;                  
84         m_reverseKeyTranslateTable['G'                             ] = KX_GKEY                    ;                  
85         m_reverseKeyTranslateTable['H'                             ] = KX_HKEY                    ;                  
86         m_reverseKeyTranslateTable['I'                             ] = KX_IKEY                    ;                  
87         m_reverseKeyTranslateTable['J'                             ] = KX_JKEY                    ;                  
88         m_reverseKeyTranslateTable['K'                             ] = KX_KKEY                    ;                  
89         m_reverseKeyTranslateTable['L'                             ] = KX_LKEY                    ;                  
90         m_reverseKeyTranslateTable['M'                             ] = KX_MKEY                    ;                  
91         m_reverseKeyTranslateTable['N'                             ] = KX_NKEY                    ;                  
92         m_reverseKeyTranslateTable['O'                             ] = KX_OKEY                    ;                  
93         m_reverseKeyTranslateTable['P'                             ] = KX_PKEY                    ;                  
94         m_reverseKeyTranslateTable['Q'                             ] = KX_QKEY                    ;                  
95         m_reverseKeyTranslateTable['R'                             ] = KX_RKEY                    ;                  
96         m_reverseKeyTranslateTable['S'                             ] = KX_SKEY                    ;                  
97         m_reverseKeyTranslateTable['T'                             ] = KX_TKEY                    ;                  
98         m_reverseKeyTranslateTable['U'                             ] = KX_UKEY                    ;                  
99         m_reverseKeyTranslateTable['V'                             ] = KX_VKEY                    ;                  
100         m_reverseKeyTranslateTable['W'                             ] = KX_WKEY                    ;                  
101         m_reverseKeyTranslateTable['X'                             ] = KX_XKEY                    ;                  
102         m_reverseKeyTranslateTable['Y'                             ] = KX_YKEY                    ;                  
103         m_reverseKeyTranslateTable['Z'                             ] = KX_ZKEY                    ;                  
104
105         m_reverseKeyTranslateTable['0'                             ] = KX_ZEROKEY                 ;                  
106         m_reverseKeyTranslateTable['1'                             ] = KX_ONEKEY                  ;                  
107         m_reverseKeyTranslateTable['2'                             ] = KX_TWOKEY                  ;                  
108         m_reverseKeyTranslateTable['3'                             ] = KX_THREEKEY                ;                  
109         m_reverseKeyTranslateTable['4'                             ] = KX_FOURKEY                 ;                  
110         m_reverseKeyTranslateTable['5'                             ] = KX_FIVEKEY                 ;                  
111         m_reverseKeyTranslateTable['6'                             ] = KX_SIXKEY                  ;                  
112         m_reverseKeyTranslateTable['7'                             ] = KX_SEVENKEY                ;                  
113         m_reverseKeyTranslateTable['8'                             ] = KX_EIGHTKEY                ;                  
114         m_reverseKeyTranslateTable['9'                             ] = KX_NINEKEY                 ;
115         
116         // Middle keyboard area keys
117         m_reverseKeyTranslateTable[VK_PAUSE                        ] = KX_PAUSEKEY                ;                  
118         m_reverseKeyTranslateTable[VK_INSERT                       ] = KX_INSERTKEY               ;                  
119         m_reverseKeyTranslateTable[VK_DELETE                       ] = KX_DELKEY                  ;                  
120         m_reverseKeyTranslateTable[VK_HOME                         ] = KX_HOMEKEY                 ;                  
121         m_reverseKeyTranslateTable[VK_END                          ] = KX_ENDKEY                  ;                  
122         m_reverseKeyTranslateTable[VK_PRIOR                        ] = KX_PAGEUPKEY               ;                  
123         m_reverseKeyTranslateTable[VK_NEXT                         ] = KX_PAGEDOWNKEY             ;                  
124         
125         // Arrow keys
126         m_reverseKeyTranslateTable[VK_UP                           ] = KX_UPARROWKEY              ;                  
127         m_reverseKeyTranslateTable[VK_DOWN                         ] = KX_DOWNARROWKEY            ;                  
128         m_reverseKeyTranslateTable[VK_LEFT                         ] = KX_LEFTARROWKEY            ;                  
129         m_reverseKeyTranslateTable[VK_RIGHT                        ] = KX_RIGHTARROWKEY           ;                  
130
131         // Function keys
132         m_reverseKeyTranslateTable[VK_F1                           ] = KX_F1KEY                   ;                  
133         m_reverseKeyTranslateTable[VK_F2                           ] = KX_F2KEY                   ;                  
134         m_reverseKeyTranslateTable[VK_F3                           ] = KX_F3KEY                   ;                  
135         m_reverseKeyTranslateTable[VK_F4                           ] = KX_F4KEY                   ;                  
136         m_reverseKeyTranslateTable[VK_F5                           ] = KX_F5KEY                   ;                  
137         m_reverseKeyTranslateTable[VK_F6                           ] = KX_F6KEY                   ;                  
138         m_reverseKeyTranslateTable[VK_F7                           ] = KX_F7KEY                   ;                  
139         m_reverseKeyTranslateTable[VK_F8                           ] = KX_F8KEY                   ;                  
140         m_reverseKeyTranslateTable[VK_F9                           ] = KX_F9KEY                   ;                  
141         m_reverseKeyTranslateTable[VK_F10                          ] = KX_F10KEY                  ;                  
142         m_reverseKeyTranslateTable[VK_F11                          ] = KX_F11KEY                  ;                  
143         m_reverseKeyTranslateTable[VK_F12                          ] = KX_F12KEY                  ;                  
144
145         // Numpad keys
146         m_reverseKeyTranslateTable[VK_NUMPAD0                      ] = KX_PAD0                     ;                  
147         m_reverseKeyTranslateTable[VK_NUMPAD1                      ] = KX_PAD1                     ;                  
148         m_reverseKeyTranslateTable[VK_NUMPAD2                      ] = KX_PAD2                     ;                  
149         m_reverseKeyTranslateTable[VK_NUMPAD3                      ] = KX_PAD3                     ;                  
150         m_reverseKeyTranslateTable[VK_NUMPAD4                      ] = KX_PAD4                     ;                  
151         m_reverseKeyTranslateTable[VK_NUMPAD5                      ] = KX_PAD5                     ;                  
152         m_reverseKeyTranslateTable[VK_NUMPAD6                      ] = KX_PAD6                     ;                  
153         m_reverseKeyTranslateTable[VK_NUMPAD7                      ] = KX_PAD7                     ;                  
154         m_reverseKeyTranslateTable[VK_NUMPAD8                      ] = KX_PAD8                     ;                                                                                                       
155         m_reverseKeyTranslateTable[VK_NUMPAD9                      ] = KX_PAD9                     ;                  
156         m_reverseKeyTranslateTable[VK_MULTIPLY                     ] = KX_PADASTERKEY              ;                                                                                                                                                                                                                                   
157         m_reverseKeyTranslateTable[VK_ADD                          ] = KX_PADPLUSKEY               ;                  
158         m_reverseKeyTranslateTable[VK_DECIMAL                      ] = KX_PADPERIOD                ;                  
159         m_reverseKeyTranslateTable[VK_SUBTRACT                     ] = KX_PADMINUS                 ;                  
160         m_reverseKeyTranslateTable[VK_DIVIDE                       ] = KX_PADSLASHKEY            ;                 
161         m_reverseKeyTranslateTable[VK_SEPARATOR                    ] = KX_PADENTER                 ;                  
162
163         // Other keys
164         m_reverseKeyTranslateTable[VK_CAPITAL                      ] = KX_CAPSLOCKKEY              ;                  
165         m_reverseKeyTranslateTable[VK_ESCAPE                       ] = KX_ESCKEY                   ;                  
166         m_reverseKeyTranslateTable[VK_TAB                          ] = KX_TABKEY                   ;                  
167         //m_reverseKeyTranslateTable[VK_RETURN                       ] = KX_RETKEY                   ;                  
168         m_reverseKeyTranslateTable[VK_SPACE                        ] = KX_SPACEKEY                 ;                  
169         m_reverseKeyTranslateTable[VK_RETURN                           ] = KX_LINEFEEDKEY                      ;                  
170         m_reverseKeyTranslateTable[VK_BACK                         ] = KX_BACKSPACEKEY             ;                  
171         m_reverseKeyTranslateTable[VK_SEMICOLON                    ] = KX_SEMICOLONKEY             ;                  
172         m_reverseKeyTranslateTable[VK_PERIOD                       ] = KX_PERIODKEY                ;                  
173         m_reverseKeyTranslateTable[VK_COMMA                        ] = KX_COMMAKEY                 ;                  
174         m_reverseKeyTranslateTable[VK_QUOTE                        ] = KX_QUOTEKEY                 ;                  
175         m_reverseKeyTranslateTable[VK_BACK_QUOTE                   ] = KX_ACCENTGRAVEKEY           ;                  
176         m_reverseKeyTranslateTable[VK_MINUS                            ] = KX_MINUSKEY                 ;                  
177         m_reverseKeyTranslateTable[VK_SLASH                                ] = KX_SLASHKEY                 ;                  
178         m_reverseKeyTranslateTable[VK_BACK_SLASH                   ] = KX_BACKSLASHKEY             ;                  
179         m_reverseKeyTranslateTable[VK_EQUALS                           ] = KX_EQUALKEY                 ;                  
180         m_reverseKeyTranslateTable[VK_OPEN_BRACKET                     ] = KX_LEFTBRACKETKEY           ;                  
181         m_reverseKeyTranslateTable[VK_CLOSE_BRACKET                    ] = KX_RIGHTBRACKETKEY          ;                  
182
183         /* 
184          * Need to handle Ctrl, Alt and Shift keys differently.
185          * Win32 messages do not discriminate left and right keys.
186          */
187         m_reverseKeyTranslateTable[VK_LCONTROL                     ] = KX_LEFTCTRLKEY              ;                  
188         m_reverseKeyTranslateTable[VK_RCONTROL                     ] = KX_RIGHTCTRLKEY             ;                  
189         m_reverseKeyTranslateTable[VK_LMENU                        ] = KX_LEFTALTKEY               ;                  
190         m_reverseKeyTranslateTable[VK_RMENU                        ] = KX_RIGHTALTKEY              ;                  
191         m_reverseKeyTranslateTable[VK_RSHIFT                       ] = KX_RIGHTSHIFTKEY            ;                  
192         m_reverseKeyTranslateTable[VK_LSHIFT                       ] = KX_LEFTSHIFTKEY             ;                  
193 }
194
195
196 GPW_KeyboardDevice::~GPW_KeyboardDevice(void)
197 {
198 }
199
200
201 void GPW_KeyboardDevice::ConvertWinEvent(WPARAM wParam, bool isDown)
202 {
203         if ((wParam == VK_SHIFT) || (wParam == VK_MENU) || (wParam == VK_CONTROL)) {
204                 ConvertModifierKey(wParam, isDown);
205         }
206         else {
207                 ConvertEvent(wParam, isDown);
208         }
209 }
210
211
212 void GPW_KeyboardDevice::ConvertModifierKey(WPARAM wParam, bool isDown)
213 {
214         /*
215         GetKeyState and GetAsyncKeyState only work with Win95, Win98, NT4,
216         Terminal Server and Windows 2000.
217         But on WinME it always returns zero. These two functions are simply
218         skipped by Millenium Edition!
219
220         Official explanation from Microsoft:
221         Intentionally disabled.
222         It didn't work all that well on some newer hardware, and worked less 
223         well with the passage of time, so it was fully disabled in ME.
224         */
225         if (!m_seperateLeftRightInitialized && isDown) {
226                 CheckForSeperateLeftRight(wParam);
227         }
228         if (m_seperateLeftRight) {
229                 bool down = HIBYTE(::GetKeyState(VK_LSHIFT)) != 0;
230                 ConvertEvent(VK_LSHIFT, down);
231                 down = HIBYTE(::GetKeyState(VK_RSHIFT)) != 0;
232                 ConvertEvent(VK_RSHIFT, down);
233                 down = HIBYTE(::GetKeyState(VK_LMENU)) != 0;
234                 ConvertEvent(VK_LMENU, down);
235                 down = HIBYTE(::GetKeyState(VK_RMENU)) != 0;
236                 ConvertEvent(VK_RMENU, down);
237                 down = HIBYTE(::GetKeyState(VK_LCONTROL)) != 0;
238                 ConvertEvent(VK_LCONTROL, down);
239                 down = HIBYTE(::GetKeyState(VK_RCONTROL)) != 0;
240                 ConvertEvent(VK_RCONTROL, down);
241         }
242         else {
243                 bool down = HIBYTE(::GetKeyState(VK_SHIFT)) != 0;
244                 ConvertEvent(VK_LSHIFT, down);
245                 ConvertEvent(VK_RSHIFT, down);
246                 down = HIBYTE(::GetKeyState(VK_MENU)) != 0;
247                 ConvertEvent(VK_LMENU, down);
248                 ConvertEvent(VK_RMENU, down);
249                 down = HIBYTE(::GetKeyState(VK_CONTROL)) != 0;
250                 ConvertEvent(VK_LCONTROL, down);
251                 ConvertEvent(VK_RCONTROL, down);
252         }
253 }
254
255
256 void GPW_KeyboardDevice::CheckForSeperateLeftRight(WPARAM wParam)
257 {
258         // Check whether this system supports seperate left and right keys
259         switch (wParam) {
260                 case VK_SHIFT:
261                         m_seperateLeftRight = 
262                                 (HIBYTE(::GetKeyState(VK_LSHIFT)) != 0) ||
263                                 (HIBYTE(::GetKeyState(VK_RSHIFT)) != 0) ?
264                                 true : false;
265                         break;
266                 case VK_CONTROL:
267                         m_seperateLeftRight = 
268                                 (HIBYTE(::GetKeyState(VK_LCONTROL)) != 0) ||
269                                 (HIBYTE(::GetKeyState(VK_RCONTROL)) != 0) ?
270                                 true : false;
271                         break;
272                 case VK_MENU:
273                         m_seperateLeftRight = 
274                                 (HIBYTE(::GetKeyState(VK_LMENU)) != 0) ||
275                                 (HIBYTE(::GetKeyState(VK_RMENU)) != 0) ?
276                                 true : false;
277                         break;
278         }
279         m_seperateLeftRightInitialized = true;
280 }