source: branches/4.5.1/src/gui/kernel/qkeymapper_win.cpp@ 559

Last change on this file since 559 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 56.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qkeymapper_p.h"
43
44#include <windows.h>
45#include <qdebug.h>
46#include <private/qevent_p.h>
47#include <private/qlocale_p.h>
48#include <private/qapplication_p.h>
49#include <qwidget.h>
50#include <qapplication.h>
51#include <ctype.h>
52
53QT_BEGIN_NAMESPACE
54
55// Uncommend, to show debugging information for the keymapper
56//#define DEBUG_KEYMAPPER
57
58// Implemented elsewhere
59extern "C" LRESULT CALLBACK QtWndProc(HWND, UINT, WPARAM, LPARAM);
60Q_CORE_EXPORT bool winPostMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
61Q_CORE_EXPORT bool winPeekMessage(MSG* msg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax,
62 UINT wRemoveMsg);
63extern Q_CORE_EXPORT QLocale qt_localeFromLCID(LCID id);
64#ifndef LANG_PASHTO
65#define LANG_PASHTO 0x63
66#endif
67#ifndef LANG_SYRIAC
68#define LANG_SYRIAC 0x5a
69#endif
70#ifndef LANG_DIVEHI
71#define LANG_DIVEHI 0x65
72#endif
73#ifndef VK_OEM_PLUS
74#define VK_OEM_PLUS 0xBB
75#endif
76#ifndef VK_OEM_3
77#define VK_OEM_3 0xC0
78#endif
79
80#if defined(Q_OS_WINCE)
81bool GetKeyboardState(unsigned char* kbuffer)
82{
83 for (int i=0; i< 256; ++i)
84 kbuffer[i] = GetAsyncKeyState(i);
85 return true;
86}
87#endif
88// Key recorder ------------------------------------------------------------------------[ start ] --
89struct KeyRecord {
90 KeyRecord(int c, int a, int s, const QString &t) : code(c), ascii(a), state(s), text(t) {}
91 KeyRecord() {}
92
93 int code;
94 int ascii;
95 int state;
96 QString text;
97};
98
99static const int QT_MAX_KEY_RECORDINGS = 64; // User has LOTS of fingers...
100struct KeyRecorder
101{
102 KeyRecorder() : nrecs(0) {}
103
104 inline KeyRecord *findKey(int code, bool remove);
105 inline void storeKey(int code, int ascii, int state, const QString& text);
106 inline void clearKeys();
107
108 int nrecs;
109 KeyRecord deleted_record; // A copy of last entry removed from records[]
110 KeyRecord records[QT_MAX_KEY_RECORDINGS];
111};
112static KeyRecorder key_recorder;
113
114KeyRecord *KeyRecorder::findKey(int code, bool remove)
115{
116 KeyRecord *result = 0;
117 for (int i = 0; i < nrecs; ++i) {
118 if (records[i].code == code) {
119 if (remove) {
120 deleted_record = records[i];
121 // Move rest down, and decrease count
122 while (i + 1 < nrecs) {
123 records[i] = records[i + 1];
124 ++i;
125 }
126 --nrecs;
127 result = &deleted_record;
128 } else {
129 result = &records[i];
130 }
131 break;
132 }
133 }
134 return result;
135}
136
137void KeyRecorder::storeKey(int code, int ascii, int state, const QString& text)
138{
139 Q_ASSERT_X(nrecs != QT_MAX_KEY_RECORDINGS,
140 "Internal KeyRecorder",
141 "Keyboard recorder buffer overflow, consider increasing QT_MAX_KEY_RECORDINGS");
142
143 if (nrecs == QT_MAX_KEY_RECORDINGS) {
144 qWarning("Qt: Internal keyboard buffer overflow");
145 return;
146 }
147 records[nrecs++] = KeyRecord(code,ascii,state,text);
148}
149
150void KeyRecorder::clearKeys()
151{
152 nrecs = 0;
153}
154// Key recorder --------------------------------------------------------------------------[ end ] --
155
156
157// Key translation ---------------------------------------------------------------------[ start ] --
158// Meaning of values:
159// 0 = Character output key, needs keyboard driver mapping
160// Key_unknown = Unknown Virtual Key, no translation possible, ignore
161static const uint KeyTbl[] = { // Keyboard mapping table
162 // Dec | Hex | Windows Virtual key
163 Qt::Key_unknown, // 0 0x00
164 Qt::Key_unknown, // 1 0x01 VK_LBUTTON | Left mouse button
165 Qt::Key_unknown, // 2 0x02 VK_RBUTTON | Right mouse button
166 Qt::Key_Cancel, // 3 0x03 VK_CANCEL | Control-Break processing
167 Qt::Key_unknown, // 4 0x04 VK_MBUTTON | Middle mouse button
168 Qt::Key_unknown, // 5 0x05 VK_XBUTTON1 | X1 mouse button
169 Qt::Key_unknown, // 6 0x06 VK_XBUTTON2 | X2 mouse button
170 Qt::Key_unknown, // 7 0x07 -- unassigned --
171 Qt::Key_Backspace, // 8 0x08 VK_BACK | BackSpace key
172 Qt::Key_Tab, // 9 0x09 VK_TAB | Tab key
173 Qt::Key_unknown, // 10 0x0A -- reserved --
174 Qt::Key_unknown, // 11 0x0B -- reserved --
175 Qt::Key_Clear, // 12 0x0C VK_CLEAR | Clear key
176 Qt::Key_Return, // 13 0x0D VK_RETURN | Enter key
177 Qt::Key_unknown, // 14 0x0E -- unassigned --
178 Qt::Key_unknown, // 15 0x0F -- unassigned --
179 Qt::Key_Shift, // 16 0x10 VK_SHIFT | Shift key
180 Qt::Key_Control, // 17 0x11 VK_CONTROL | Ctrl key
181 Qt::Key_Alt, // 18 0x12 VK_MENU | Alt key
182 Qt::Key_Pause, // 19 0x13 VK_PAUSE | Pause key
183 Qt::Key_CapsLock, // 20 0x14 VK_CAPITAL | Caps-Lock
184 Qt::Key_unknown, // 21 0x15 VK_KANA / VK_HANGUL | IME Kana or Hangul mode
185 Qt::Key_unknown, // 22 0x16 -- unassigned --
186 Qt::Key_unknown, // 23 0x17 VK_JUNJA | IME Junja mode
187 Qt::Key_unknown, // 24 0x18 VK_FINAL | IME final mode
188 Qt::Key_unknown, // 25 0x19 VK_HANJA / VK_KANJI | IME Hanja or Kanji mode
189 Qt::Key_unknown, // 26 0x1A -- unassigned --
190 Qt::Key_Escape, // 27 0x1B VK_ESCAPE | Esc key
191 Qt::Key_unknown, // 28 0x1C VK_CONVERT | IME convert
192 Qt::Key_unknown, // 29 0x1D VK_NONCONVERT | IME non-convert
193 Qt::Key_unknown, // 30 0x1E VK_ACCEPT | IME accept
194 Qt::Key_Mode_switch,// 31 0x1F VK_MODECHANGE | IME mode change request
195 Qt::Key_Space, // 32 0x20 VK_SPACE | Spacebar
196 Qt::Key_PageUp, // 33 0x21 VK_PRIOR | Page Up key
197 Qt::Key_PageDown, // 34 0x22 VK_NEXT | Page Down key
198 Qt::Key_End, // 35 0x23 VK_END | End key
199 Qt::Key_Home, // 36 0x24 VK_HOME | Home key
200 Qt::Key_Left, // 37 0x25 VK_LEFT | Left arrow key
201 Qt::Key_Up, // 38 0x26 VK_UP | Up arrow key
202 Qt::Key_Right, // 39 0x27 VK_RIGHT | Right arrow key
203 Qt::Key_Down, // 40 0x28 VK_DOWN | Down arrow key
204 Qt::Key_Select, // 41 0x29 VK_SELECT | Select key
205 Qt::Key_Printer, // 42 0x2A VK_PRINT | Print key
206 Qt::Key_Execute, // 43 0x2B VK_EXECUTE | Execute key
207 Qt::Key_Print, // 44 0x2C VK_SNAPSHOT | Print Screen key
208 Qt::Key_Insert, // 45 0x2D VK_INSERT | Ins key
209 Qt::Key_Delete, // 46 0x2E VK_DELETE | Del key
210 Qt::Key_Help, // 47 0x2F VK_HELP | Help key
211 0, // 48 0x30 (VK_0) | 0 key
212 0, // 49 0x31 (VK_1) | 1 key
213 0, // 50 0x32 (VK_2) | 2 key
214 0, // 51 0x33 (VK_3) | 3 key
215 0, // 52 0x34 (VK_4) | 4 key
216 0, // 53 0x35 (VK_5) | 5 key
217 0, // 54 0x36 (VK_6) | 6 key
218 0, // 55 0x37 (VK_7) | 7 key
219 0, // 56 0x38 (VK_8) | 8 key
220 0, // 57 0x39 (VK_9) | 9 key
221 Qt::Key_unknown, // 58 0x3A -- unassigned --
222 Qt::Key_unknown, // 59 0x3B -- unassigned --
223 Qt::Key_unknown, // 60 0x3C -- unassigned --
224 Qt::Key_unknown, // 61 0x3D -- unassigned --
225 Qt::Key_unknown, // 62 0x3E -- unassigned --
226 Qt::Key_unknown, // 63 0x3F -- unassigned --
227 Qt::Key_unknown, // 64 0x40 -- unassigned --
228 0, // 65 0x41 (VK_A) | A key
229 0, // 66 0x42 (VK_B) | B key
230 0, // 67 0x43 (VK_C) | C key
231 0, // 68 0x44 (VK_D) | D key
232 0, // 69 0x45 (VK_E) | E key
233 0, // 70 0x46 (VK_F) | F key
234 0, // 71 0x47 (VK_G) | G key
235 0, // 72 0x48 (VK_H) | H key
236 0, // 73 0x49 (VK_I) | I key
237 0, // 74 0x4A (VK_J) | J key
238 0, // 75 0x4B (VK_K) | K key
239 0, // 76 0x4C (VK_L) | L key
240 0, // 77 0x4D (VK_M) | M key
241 0, // 78 0x4E (VK_N) | N key
242 0, // 79 0x4F (VK_O) | O key
243 0, // 80 0x50 (VK_P) | P key
244 0, // 81 0x51 (VK_Q) | Q key
245 0, // 82 0x52 (VK_R) | R key
246 0, // 83 0x53 (VK_S) | S key
247 0, // 84 0x54 (VK_T) | T key
248 0, // 85 0x55 (VK_U) | U key
249 0, // 86 0x56 (VK_V) | V key
250 0, // 87 0x57 (VK_W) | W key
251 0, // 88 0x58 (VK_X) | X key
252 0, // 89 0x59 (VK_Y) | Y key
253 0, // 90 0x5A (VK_Z) | Z key
254 Qt::Key_Meta, // 91 0x5B VK_LWIN | Left Windows - MS Natural kbd
255 Qt::Key_Meta, // 92 0x5C VK_RWIN | Right Windows - MS Natural kbd
256 Qt::Key_Menu, // 93 0x5D VK_APPS | Application key-MS Natural kbd
257 Qt::Key_unknown, // 94 0x5E -- reserved --
258 Qt::Key_Sleep, // 95 0x5F VK_SLEEP
259 Qt::Key_0, // 96 0x60 VK_NUMPAD0 | Numeric keypad 0 key
260 Qt::Key_1, // 97 0x61 VK_NUMPAD1 | Numeric keypad 1 key
261 Qt::Key_2, // 98 0x62 VK_NUMPAD2 | Numeric keypad 2 key
262 Qt::Key_3, // 99 0x63 VK_NUMPAD3 | Numeric keypad 3 key
263 Qt::Key_4, // 100 0x64 VK_NUMPAD4 | Numeric keypad 4 key
264 Qt::Key_5, // 101 0x65 VK_NUMPAD5 | Numeric keypad 5 key
265 Qt::Key_6, // 102 0x66 VK_NUMPAD6 | Numeric keypad 6 key
266 Qt::Key_7, // 103 0x67 VK_NUMPAD7 | Numeric keypad 7 key
267 Qt::Key_8, // 104 0x68 VK_NUMPAD8 | Numeric keypad 8 key
268 Qt::Key_9, // 105 0x69 VK_NUMPAD9 | Numeric keypad 9 key
269 Qt::Key_Asterisk, // 106 0x6A VK_MULTIPLY | Multiply key
270 Qt::Key_Plus, // 107 0x6B VK_ADD | Add key
271 Qt::Key_Comma, // 108 0x6C VK_SEPARATOR | Separator key
272 Qt::Key_Minus, // 109 0x6D VK_SUBTRACT | Subtract key
273 Qt::Key_Period, // 110 0x6E VK_DECIMAL | Decimal key
274 Qt::Key_Slash, // 111 0x6F VK_DIVIDE | Divide key
275 Qt::Key_F1, // 112 0x70 VK_F1 | F1 key
276 Qt::Key_F2, // 113 0x71 VK_F2 | F2 key
277 Qt::Key_F3, // 114 0x72 VK_F3 | F3 key
278 Qt::Key_F4, // 115 0x73 VK_F4 | F4 key
279 Qt::Key_F5, // 116 0x74 VK_F5 | F5 key
280 Qt::Key_F6, // 117 0x75 VK_F6 | F6 key
281 Qt::Key_F7, // 118 0x76 VK_F7 | F7 key
282 Qt::Key_F8, // 119 0x77 VK_F8 | F8 key
283 Qt::Key_F9, // 120 0x78 VK_F9 | F9 key
284 Qt::Key_F10, // 121 0x79 VK_F10 | F10 key
285 Qt::Key_F11, // 122 0x7A VK_F11 | F11 key
286 Qt::Key_F12, // 123 0x7B VK_F12 | F12 key
287 Qt::Key_F13, // 124 0x7C VK_F13 | F13 key
288 Qt::Key_F14, // 125 0x7D VK_F14 | F14 key
289 Qt::Key_F15, // 126 0x7E VK_F15 | F15 key
290 Qt::Key_F16, // 127 0x7F VK_F16 | F16 key
291 Qt::Key_F17, // 128 0x80 VK_F17 | F17 key
292 Qt::Key_F18, // 129 0x81 VK_F18 | F18 key
293 Qt::Key_F19, // 130 0x82 VK_F19 | F19 key
294 Qt::Key_F20, // 131 0x83 VK_F20 | F20 key
295 Qt::Key_F21, // 132 0x84 VK_F21 | F21 key
296 Qt::Key_F22, // 133 0x85 VK_F22 | F22 key
297 Qt::Key_F23, // 134 0x86 VK_F23 | F23 key
298 Qt::Key_F24, // 135 0x87 VK_F24 | F24 key
299 Qt::Key_unknown, // 136 0x88 -- unassigned --
300 Qt::Key_unknown, // 137 0x89 -- unassigned --
301 Qt::Key_unknown, // 138 0x8A -- unassigned --
302 Qt::Key_unknown, // 139 0x8B -- unassigned --
303 Qt::Key_unknown, // 140 0x8C -- unassigned --
304 Qt::Key_unknown, // 141 0x8D -- unassigned --
305 Qt::Key_unknown, // 142 0x8E -- unassigned --
306 Qt::Key_unknown, // 143 0x8F -- unassigned --
307 Qt::Key_NumLock, // 144 0x90 VK_NUMLOCK | Num Lock key
308 Qt::Key_ScrollLock, // 145 0x91 VK_SCROLL | Scroll Lock key
309 // Fujitsu/OASYS kbd --------------------
310 0, //Qt::Key_Jisho, // 146 0x92 VK_OEM_FJ_JISHO | 'Dictionary' key /
311 // VK_OEM_NEC_EQUAL = key on numpad on NEC PC-9800 kbd
312 Qt::Key_Massyo, // 147 0x93 VK_OEM_FJ_MASSHOU | 'Unregister word' key
313 Qt::Key_Touroku, // 148 0x94 VK_OEM_FJ_TOUROKU | 'Register word' key
314 0, //Qt::Key_Oyayubi_Left,//149 0x95 VK_OEM_FJ_LOYA | 'Left OYAYUBI' key
315 0, //Qt::Key_Oyayubi_Right,//150 0x96 VK_OEM_FJ_ROYA | 'Right OYAYUBI' key
316 Qt::Key_unknown, // 151 0x97 -- unassigned --
317 Qt::Key_unknown, // 152 0x98 -- unassigned --
318 Qt::Key_unknown, // 153 0x99 -- unassigned --
319 Qt::Key_unknown, // 154 0x9A -- unassigned --
320 Qt::Key_unknown, // 155 0x9B -- unassigned --
321 Qt::Key_unknown, // 156 0x9C -- unassigned --
322 Qt::Key_unknown, // 157 0x9D -- unassigned --
323 Qt::Key_unknown, // 158 0x9E -- unassigned --
324 Qt::Key_unknown, // 159 0x9F -- unassigned --
325 Qt::Key_Shift, // 160 0xA0 VK_LSHIFT | Left Shift key
326 Qt::Key_Shift, // 161 0xA1 VK_RSHIFT | Right Shift key
327 Qt::Key_Control, // 162 0xA2 VK_LCONTROL | Left Ctrl key
328 Qt::Key_Control, // 163 0xA3 VK_RCONTROL | Right Ctrl key
329 Qt::Key_Alt, // 164 0xA4 VK_LMENU | Left Menu key
330 Qt::Key_Alt, // 165 0xA5 VK_RMENU | Right Menu key
331 Qt::Key_Back, // 166 0xA6 VK_BROWSER_BACK | Browser Back key
332 Qt::Key_Forward, // 167 0xA7 VK_BROWSER_FORWARD | Browser Forward key
333 Qt::Key_Refresh, // 168 0xA8 VK_BROWSER_REFRESH | Browser Refresh key
334 Qt::Key_Stop, // 169 0xA9 VK_BROWSER_STOP | Browser Stop key
335 Qt::Key_Search, // 170 0xAA VK_BROWSER_SEARCH | Browser Search key
336 Qt::Key_Favorites, // 171 0xAB VK_BROWSER_FAVORITES| Browser Favorites key
337 Qt::Key_HomePage, // 172 0xAC VK_BROWSER_HOME | Browser Start and Home key
338 Qt::Key_VolumeMute, // 173 0xAD VK_VOLUME_MUTE | Volume Mute key
339 Qt::Key_VolumeDown, // 174 0xAE VK_VOLUME_DOWN | Volume Down key
340 Qt::Key_VolumeUp, // 175 0xAF VK_VOLUME_UP | Volume Up key
341 Qt::Key_MediaNext, // 176 0xB0 VK_MEDIA_NEXT_TRACK | Next Track key
342 Qt::Key_MediaPrevious, //177 0xB1 VK_MEDIA_PREV_TRACK | Previous Track key
343 Qt::Key_MediaStop, // 178 0xB2 VK_MEDIA_STOP | Stop Media key
344 Qt::Key_MediaPlay, // 179 0xB3 VK_MEDIA_PLAY_PAUSE | Play/Pause Media key
345 Qt::Key_LaunchMail, // 180 0xB4 VK_LAUNCH_MAIL | Start Mail key
346 Qt::Key_LaunchMedia,// 181 0xB5 VK_LAUNCH_MEDIA_SELECT Select Media key
347 Qt::Key_Launch0, // 182 0xB6 VK_LAUNCH_APP1 | Start Application 1 key
348 Qt::Key_Launch1, // 183 0xB7 VK_LAUNCH_APP2 | Start Application 2 key
349 Qt::Key_unknown, // 184 0xB8 -- reserved --
350 Qt::Key_unknown, // 185 0xB9 -- reserved --
351 0, // 186 0xBA VK_OEM_1 | ';:' for US
352 0, // 187 0xBB VK_OEM_PLUS | '+' any country
353 0, // 188 0xBC VK_OEM_COMMA | ',' any country
354 0, // 189 0xBD VK_OEM_MINUS | '-' any country
355 0, // 190 0xBE VK_OEM_PERIOD | '.' any country
356 0, // 191 0xBF VK_OEM_2 | '/?' for US
357 0, // 192 0xC0 VK_OEM_3 | '`~' for US
358 Qt::Key_unknown, // 193 0xC1 -- reserved --
359 Qt::Key_unknown, // 194 0xC2 -- reserved --
360 Qt::Key_unknown, // 195 0xC3 -- reserved --
361 Qt::Key_unknown, // 196 0xC4 -- reserved --
362 Qt::Key_unknown, // 197 0xC5 -- reserved --
363 Qt::Key_unknown, // 198 0xC6 -- reserved --
364 Qt::Key_unknown, // 199 0xC7 -- reserved --
365 Qt::Key_unknown, // 200 0xC8 -- reserved --
366 Qt::Key_unknown, // 201 0xC9 -- reserved --
367 Qt::Key_unknown, // 202 0xCA -- reserved --
368 Qt::Key_unknown, // 203 0xCB -- reserved --
369 Qt::Key_unknown, // 204 0xCC -- reserved --
370 Qt::Key_unknown, // 205 0xCD -- reserved --
371 Qt::Key_unknown, // 206 0xCE -- reserved --
372 Qt::Key_unknown, // 207 0xCF -- reserved --
373 Qt::Key_unknown, // 208 0xD0 -- reserved --
374 Qt::Key_unknown, // 209 0xD1 -- reserved --
375 Qt::Key_unknown, // 210 0xD2 -- reserved --
376 Qt::Key_unknown, // 211 0xD3 -- reserved --
377 Qt::Key_unknown, // 212 0xD4 -- reserved --
378 Qt::Key_unknown, // 213 0xD5 -- reserved --
379 Qt::Key_unknown, // 214 0xD6 -- reserved --
380 Qt::Key_unknown, // 215 0xD7 -- reserved --
381 Qt::Key_unknown, // 216 0xD8 -- unassigned --
382 Qt::Key_unknown, // 217 0xD9 -- unassigned --
383 Qt::Key_unknown, // 218 0xDA -- unassigned --
384 0, // 219 0xDB VK_OEM_4 | '[{' for US
385 0, // 220 0xDC VK_OEM_5 | '\|' for US
386 0, // 221 0xDD VK_OEM_6 | ']}' for US
387 0, // 222 0xDE VK_OEM_7 | ''"' for US
388 0, // 223 0xDF VK_OEM_8
389 Qt::Key_unknown, // 224 0xE0 -- reserved --
390 Qt::Key_unknown, // 225 0xE1 VK_OEM_AX | 'AX' key on Japanese AX kbd
391 Qt::Key_unknown, // 226 0xE2 VK_OEM_102 | "<>" or "\|" on RT 102-key kbd
392 Qt::Key_unknown, // 227 0xE3 VK_ICO_HELP | Help key on ICO
393 Qt::Key_unknown, // 228 0xE4 VK_ICO_00 | 00 key on ICO
394 Qt::Key_unknown, // 229 0xE5 VK_PROCESSKEY | IME Process key
395 Qt::Key_unknown, // 230 0xE6 VK_ICO_CLEAR |
396 Qt::Key_unknown, // 231 0xE7 VK_PACKET | Unicode char as keystrokes
397 Qt::Key_unknown, // 232 0xE8 -- unassigned --
398 // Nokia/Ericsson definitions ---------------
399 Qt::Key_unknown, // 233 0xE9 VK_OEM_RESET
400 Qt::Key_unknown, // 234 0xEA VK_OEM_JUMP
401 Qt::Key_unknown, // 235 0xEB VK_OEM_PA1
402 Qt::Key_unknown, // 236 0xEC VK_OEM_PA2
403 Qt::Key_unknown, // 237 0xED VK_OEM_PA3
404 Qt::Key_unknown, // 238 0xEE VK_OEM_WSCTRL
405 Qt::Key_unknown, // 239 0xEF VK_OEM_CUSEL
406 Qt::Key_unknown, // 240 0xF0 VK_OEM_ATTN
407 Qt::Key_unknown, // 241 0xF1 VK_OEM_FINISH
408 Qt::Key_unknown, // 242 0xF2 VK_OEM_COPY
409 Qt::Key_unknown, // 243 0xF3 VK_OEM_AUTO
410 Qt::Key_unknown, // 244 0xF4 VK_OEM_ENLW
411 Qt::Key_unknown, // 245 0xF5 VK_OEM_BACKTAB
412 Qt::Key_unknown, // 246 0xF6 VK_ATTN | Attn key
413 Qt::Key_unknown, // 247 0xF7 VK_CRSEL | CrSel key
414 Qt::Key_unknown, // 248 0xF8 VK_EXSEL | ExSel key
415 Qt::Key_unknown, // 249 0xF9 VK_EREOF | Erase EOF key
416 Qt::Key_Play, // 250 0xFA VK_PLAY | Play key
417 Qt::Key_Zoom, // 251 0xFB VK_ZOOM | Zoom key
418 Qt::Key_unknown, // 252 0xFC VK_NONAME | Reserved
419 Qt::Key_unknown, // 253 0xFD VK_PA1 | PA1 key
420 Qt::Key_Clear, // 254 0xFE VK_OEM_CLEAR | Clear key
421 0
422};
423
424// Possible modifier states.
425// NOTE: The order of these states match the order in QKeyMapperPrivate::updatePossibleKeyCodes()!
426static const Qt::KeyboardModifiers ModsTbl[] = {
427 Qt::NoModifier, // 0
428 Qt::ShiftModifier, // 1
429 Qt::ControlModifier, // 2
430 Qt::ControlModifier | Qt::ShiftModifier, // 3
431 Qt::AltModifier, // 4
432 Qt::AltModifier | Qt::ShiftModifier, // 5
433 Qt::AltModifier | Qt::ControlModifier, // 6
434 Qt::AltModifier | Qt::ShiftModifier | Qt::ControlModifier, // 7
435 Qt::NoModifier, // Fall-back to raw Key_*
436};
437
438#if defined(Q_OS_WINCE)
439 // Use the KeyTbl to resolve a Qt::Key out of the virtual keys.
440 // In case it is not resolvable, continue using the virtual key itself.
441
442QT_BEGIN_INCLUDE_NAMESPACE
443
444int ToUnicode(UINT vk, int /*scancode*/, unsigned char* /*kbdBuffer*/, LPWSTR unicodeBuffer, int, int)
445{
446 QT_USE_NAMESPACE
447 QChar* buf = reinterpret_cast< QChar*>(unicodeBuffer);
448 if (KeyTbl[vk] == 0) {
449 buf[0] = vk;
450 return 1;
451 }
452 return 0;
453}
454
455int ToAscii(UINT vk, int scancode, unsigned char *kbdBuffer, LPWORD unicodeBuffer, int flag)
456{
457 return ToUnicode(vk, scancode, kbdBuffer, (LPWSTR) unicodeBuffer, 0, flag);
458
459}
460QT_END_INCLUDE_NAMESPACE
461
462#endif
463
464// Translate a VK into a Qt key code, or unicode character
465static inline int toKeyOrUnicode(int vk, int scancode, unsigned char *kbdBuffer, bool *isDeadkey = 0)
466{
467 Q_ASSERT(vk > 0 && vk < 256);
468 int code = 0;
469 QChar unicodeBuffer[5];
470 int res = 0;
471 if (QSysInfo::WindowsVersion < QSysInfo::WV_NT)
472 res = ToAscii(vk, scancode, kbdBuffer, reinterpret_cast<LPWORD>(unicodeBuffer), 0);
473 else
474 res = ToUnicode(vk, scancode, kbdBuffer, reinterpret_cast<LPWSTR>(unicodeBuffer), 5, 0);
475
476 if (res)
477 code = unicodeBuffer[0].toUpper().unicode();
478
479 // Qt::Key_*'s are not encoded below 0x20, so try again, and DEL keys (0x7f) is encoded with a
480 // proper Qt::Key_ code
481 if (code < 0x20 || code == 0x7f) // Handles res==0 too
482 code = KeyTbl[vk];
483
484 if (isDeadkey)
485 *isDeadkey = (res == -1);
486
487 return code == Qt::Key_unknown ? 0 : code;
488}
489
490Q_GUI_EXPORT int qt_translateKeyCode(int vk)
491{
492 int code = (vk < 0 || vk > 255) ? 0 : KeyTbl[vk];
493 return code == Qt::Key_unknown ? 0 : code;
494}
495
496static inline int asciiToKeycode(char a, int state)
497{
498 if (a >= 'a' && a <= 'z')
499 a = toupper(a);
500 if ((state & Qt::ControlModifier) != 0) {
501 if (a >= 0 && a <= 31) // [email protected]+A..CTRL+Z..Ctrl+_
502 a += '@'; // to @..A..Z.._
503 }
504 return a & 0xff;
505}
506
507static int inputcharset = CP_ACP;
508static inline QChar wmchar_to_unicode(DWORD c)
509{
510 // qt_winMB2QString is the generalization of this function.
511 QT_WA({
512 return QChar((ushort)c);
513 } , {
514 char mb[2];
515 mb[0] = c & 0xff;
516 mb[1] = 0;
517 WCHAR wc[1];
518 MultiByteToWideChar(inputcharset, MB_PRECOMPOSED, mb, -1, wc, 1);
519 return QChar(wc[0]);
520 });
521}
522
523static inline QChar imechar_to_unicode(DWORD c)
524{
525 // qt_winMB2QString is the generalization of this function.
526 QT_WA({
527 return QChar((ushort)c);
528 } , {
529 char mb[3];
530 mb[0] = (c >> 8) & 0xff;
531 mb[1] = c & 0xff;
532 mb[2] = 0;
533 WCHAR wc[1];
534 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mb, -1, wc, 1);
535 return QChar(wc[0]);
536 });
537}
538
539static inline bool isModifierKey(int code)
540{
541 return (code >= Qt::Key_Shift) && (code <= Qt::Key_ScrollLock);
542}
543// Key translation -----------------------------------------------------------------------[ end ]---
544
545
546static void qt_show_system_menu(QWidget* tlw)
547{
548 Q_ASSERT(tlw->testAttribute(Qt::WA_WState_Created));
549 HMENU menu = GetSystemMenu(tlw->internalWinId(), FALSE);
550 if (!menu)
551 return; // no menu for this window
552
553#define enabled (MF_BYCOMMAND | MF_ENABLED)
554#define disabled (MF_BYCOMMAND | MF_GRAYED)
555
556#ifndef Q_OS_WINCE
557 EnableMenuItem(menu, SC_MINIMIZE, (tlw->windowFlags() & Qt::WindowMinimizeButtonHint)?enabled:disabled);
558 bool maximized = IsZoomed(tlw->internalWinId());
559
560 EnableMenuItem(menu, SC_MAXIMIZE, ! (tlw->windowFlags() & Qt::WindowMaximizeButtonHint) || maximized?disabled:enabled);
561 EnableMenuItem(menu, SC_RESTORE, maximized?enabled:disabled);
562
563 // We should _not_ check with the setFixedSize(x,y) case here, since Windows is not able to check
564 // this and our menu here would be out-of-sync with the menu produced by mouse-click on the
565 // System Menu, or right-click on the title bar.
566 EnableMenuItem(menu, SC_SIZE, (tlw->windowFlags() & Qt::MSWindowsFixedSizeDialogHint) || maximized?disabled:enabled);
567 EnableMenuItem(menu, SC_MOVE, maximized?disabled:enabled);
568 EnableMenuItem(menu, SC_CLOSE, enabled);
569 // Set bold on close menu item
570 MENUITEMINFO closeItem;
571 closeItem.cbSize = sizeof(MENUITEMINFO);
572 closeItem.fMask = MIIM_STATE;
573 closeItem.fState = MFS_DEFAULT;
574 SetMenuItemInfo(menu, SC_CLOSE, FALSE, &closeItem);
575#endif
576
577#undef enabled
578#undef disabled
579 int ret = TrackPopupMenuEx(menu,
580 TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
581 tlw->geometry().x(), tlw->geometry().y(),
582 tlw->internalWinId(),
583 0);
584 if (ret)
585 QtWndProc(tlw->internalWinId(), WM_SYSCOMMAND, ret, 0);
586}
587
588
589// QETWidget class is only for accessing the sendSpontaneousEvent function in QApplication
590class QETWidget : public QWidget {
591public:
592 static bool sendSpontaneousEvent(QObject *r, QEvent *e)
593 { return QApplication::sendSpontaneousEvent(r, e); }
594};
595
596
597// Keyboard map private ----------------------------------------------------------------[ start ]---
598
599/*
600 \internal
601 A Windows KeyboardLayoutItem has 8 possible states:
602 1. Unmodified
603 2. Shift
604 3. Control
605 4. Control + Shift
606 5. Alt
607 6. Alt + Shift
608 7. Alt + Control
609 8. Alt + Control + Shift
610*/
611struct KeyboardLayoutItem {
612 bool dirty;
613 quint8 deadkeys;
614 quint32 qtKey[9]; // Can by any Qt::Key_<foo>, or unicode character
615};
616
617QKeyMapperPrivate::QKeyMapperPrivate()
618{
619 memset(keyLayout, 0, sizeof(keyLayout));
620}
621
622QKeyMapperPrivate::~QKeyMapperPrivate()
623{
624 clearMappings();
625}
626
627void QKeyMapperPrivate::clearMappings()
628{
629 for (int i = 0; i < 255; ++i) {
630 if (keyLayout[i]) {
631 delete keyLayout[i];
632 keyLayout[i] = 0;
633 }
634 }
635
636 /* MAKELCID()'s first argument is a WORD, and GetKeyboardLayout()
637 * returns a DWORD. */
638// LCID newLCID = MAKELCID(DWORD(GetKeyboardLayout(0)), SORT_DEFAULT);
639// keyboardInputLocale = qt_localeFromLCID(newLCID);
640 LCID newLCID = MAKELCID(
641 reinterpret_cast<long>(GetKeyboardLayout(0)),
642 SORT_DEFAULT
643 );
644 keyboardInputLocale = qt_localeFromLCID(newLCID);
645 bool bidi = false;
646#ifdef UNICODE
647 if (QSysInfo::WindowsVersion >= QSysInfo::WV_2000) {
648 WCHAR wchLCIDFontSig[16];
649 if (GetLocaleInfoW(newLCID,
650 LOCALE_FONTSIGNATURE,
651 &wchLCIDFontSig[0],
652 (sizeof(wchLCIDFontSig)/sizeof(WCHAR)))
653 && (wchLCIDFontSig[7] & (WCHAR)0x0800))
654 bidi = true;
655 } else
656#endif //UNICODE
657 {
658 if (newLCID == 0x0859 || //Sindhi (Arabic script)
659 newLCID == 0x0460) //Kashmiri (Arabic script)
660 bidi = true;;
661
662 switch (PRIMARYLANGID(newLCID))
663 {
664 case LANG_ARABIC:
665 case LANG_HEBREW:
666 case LANG_URDU:
667 case LANG_FARSI:
668 case LANG_PASHTO:
669 //case LANG_UIGHUR:
670 case LANG_SYRIAC:
671 case LANG_DIVEHI:
672 bidi = true;
673 }
674 }
675
676 keyboardInputDirection = bidi ? Qt::RightToLeft : Qt::LeftToRight;
677}
678
679void QKeyMapperPrivate::clearRecordedKeys()
680{
681 key_recorder.clearKeys();
682}
683
684
685inline void setKbdState(unsigned char *kbd, bool shift, bool ctrl, bool alt)
686{
687 kbd[VK_LSHIFT ] = (shift ? 0x80 : 0);
688 kbd[VK_SHIFT ] = (shift ? 0x80 : 0);
689 kbd[VK_LCONTROL] = (ctrl ? 0x80 : 0);
690 kbd[VK_CONTROL ] = (ctrl ? 0x80 : 0);
691 kbd[VK_RMENU ] = (alt ? 0x80 : 0);
692 kbd[VK_MENU ] = (alt ? 0x80 : 0);
693}
694
695void QKeyMapperPrivate::updateKeyMap(const MSG &msg)
696{
697 unsigned char kbdBuffer[256]; // Will hold the complete keyboard state
698 GetKeyboardState(kbdBuffer);
699 quint32 scancode = (msg.lParam >> 16) & 0xfff;
700 updatePossibleKeyCodes(kbdBuffer, scancode, msg.wParam);