source: trunk/src/gui/kernel/qkeymapper_win.cpp@ 352

Last change on this file since 352 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);
701}
702
703void QKeyMapperPrivate::updatePossibleKeyCodes(unsigned char *kbdBuffer, quint32 scancode,
704 quint32 vk_key)
705{
706 if (!vk_key || (keyLayout[vk_key] && !keyLayout[vk_key]->dirty))
707 return;
708
709 if (!keyLayout[vk_key])
710 keyLayout[vk_key] = new KeyboardLayoutItem;
711
712 // Copy keyboard state, so we can modify and query output for each possible permutation
713 unsigned char buffer[256];
714 memcpy(buffer, kbdBuffer, sizeof(buffer));
715 // Always 0, as Windows doesn't treat these as modifiers;
716 buffer[VK_LWIN ] = 0;
717 buffer[VK_RWIN ] = 0;
718 buffer[VK_CAPITAL ] = 0;
719 buffer[VK_NUMLOCK ] = 0;
720 buffer[VK_SCROLL ] = 0;
721 // Always 0, since we'll only change the other versions
722 buffer[VK_RSHIFT ] = 0;
723 buffer[VK_RCONTROL] = 0;
724 buffer[VK_LMENU ] = 0; // Use right Alt, since left Ctrl + right Alt is considered AltGraph
725
726 bool isDeadKey = false;
727 keyLayout[vk_key]->deadkeys = 0;
728 keyLayout[vk_key]->dirty = false;
729 setKbdState(buffer, false, false, false);
730 keyLayout[vk_key]->qtKey[0] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
731 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x01 : 0;
732 setKbdState(buffer, true, false, false);
733 keyLayout[vk_key]->qtKey[1] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
734 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x02 : 0;
735 setKbdState(buffer, false, true, false);
736 keyLayout[vk_key]->qtKey[2] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
737 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x04 : 0;
738 setKbdState(buffer, true, true, false);
739 keyLayout[vk_key]->qtKey[3] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
740 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x08 : 0;
741 setKbdState(buffer, false, false, true);
742 keyLayout[vk_key]->qtKey[4] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
743 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x10 : 0;
744 setKbdState(buffer, true, false, true);
745 keyLayout[vk_key]->qtKey[5] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
746 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x20 : 0;
747 setKbdState(buffer, false, true, true);
748 keyLayout[vk_key]->qtKey[6] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
749 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x40 : 0;
750 setKbdState(buffer, true, true, true);
751 keyLayout[vk_key]->qtKey[7] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
752 keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x80 : 0;
753 // Add a fall back key for layouts which don't do composition and show non-latin1 characters
754 int fallbackKey = KeyTbl[vk_key];
755 if (!fallbackKey || fallbackKey == Qt::Key_unknown) {
756 fallbackKey = 0;
757 if (vk_key != keyLayout[vk_key]->qtKey[0] && vk_key < 0x5B && vk_key > 0x2F)
758 fallbackKey = vk_key;
759 }
760 keyLayout[vk_key]->qtKey[8] = fallbackKey;
761
762 // If this vk_key a Dead Key
763 if (MapVirtualKey(vk_key, 2) & 0x80008000) { // (High-order dead key on Win 95 is 0x8000)
764 // Push a Space, then the original key through the low-level ToAscii functions.
765 // We do this because these functions (ToAscii / ToUnicode) will alter the internal state of
766 // the keyboard driver By doing the following, we set the keyboard driver state back to what
767 // it was before we wrecked it with the code above.
768 // We need to push the space with an empty keystate map, since the driver checks the map for
769 // transitions in modifiers, so this helps us capture all possible deadkeys.
770 unsigned char emptyBuffer[256];
771 memset(emptyBuffer, 0, sizeof(emptyBuffer));
772 ::ToAscii(VK_SPACE, 0, emptyBuffer, reinterpret_cast<LPWORD>(&buffer), 0);
773 ::ToAscii(vk_key, scancode, kbdBuffer, reinterpret_cast<LPWORD>(&buffer), 0);
774 }
775
776#ifdef DEBUG_KEYMAPPER
777 qDebug("updatePossibleKeyCodes for virtual key = 0x%02x!", vk_key);
778 for (int i = 0; i < 9; ++i) {
779 qDebug(" [%d] (%d,0x%02x,'%c') %s", i,
780 keyLayout[vk_key]->qtKey[i],
781 keyLayout[vk_key]->qtKey[i],
782 keyLayout[vk_key]->qtKey[i] ? keyLayout[vk_key]->qtKey[i] : 0x03,
783 keyLayout[vk_key]->deadkeys & (1<<i) ? "deadkey" : "");
784 }
785#endif // DEBUG_KEYMAPPER
786}
787
788bool QKeyMapperPrivate::isADeadKey(unsigned int vk_key, unsigned int modifiers)
789{
790 if (keyLayout && (vk_key < 256) && keyLayout[vk_key]) {
791 for(register int i = 0; i < 9; ++i) {
792 if (uint(ModsTbl[i]) == modifiers)
793 return bool(keyLayout[vk_key]->deadkeys & 1<<i);
794 }
795 }
796 return false;
797}
798
799extern bool qt_use_rtl_extensions;
800
801QList<int> QKeyMapperPrivate::possibleKeys(QKeyEvent *e)
802{
803 QList<int> result;
804
805 KeyboardLayoutItem *kbItem = keyLayout[e->nativeVirtualKey()];
806 if(!kbItem)
807 return result;
808
809 quint32 baseKey = kbItem->qtKey[0];
810 Qt::KeyboardModifiers keyMods = e->modifiers();
811 if (baseKey == Qt::Key_Return && (e->nativeModifiers() & ExtendedKey)) {
812 result << int(Qt::Key_Enter + keyMods);
813 return result;
814 }
815 result << int(baseKey + keyMods); // The base key is _always_ valid, of course
816
817 for(int i = 1; i < 9; ++i) {
818 Qt::KeyboardModifiers neededMods = ModsTbl[i];
819 quint32 key = kbItem->qtKey[i];
820 if (key && key != baseKey && ((keyMods & neededMods) == neededMods))
821 result << int(key + (keyMods & ~neededMods));
822 }
823
824 return result;
825}
826
827bool QKeyMapperPrivate::translateKeyEvent(QWidget *widget, const MSG &msg, bool grab)
828{
829 Q_Q(QKeyMapper);
830 Q_UNUSED(q); // Strange, but the compiler complains on q not being referenced, even if it is..
831 bool k0 = false;
832 bool k1 = false;
833 int msgType = msg.message;
834
835 quint32 scancode = (msg.lParam >> 16) & 0xfff;
836 quint32 vk_key = MapVirtualKey(scancode, 1);
837 bool isNumpad = (msg.wParam >= VK_NUMPAD0 && msg.wParam <= VK_NUMPAD9);
838 quint32 nModifiers = 0;
839
840 if (QSysInfo::WindowsVersion < QSysInfo::WV_NT || QSysInfo::WindowsVersion & QSysInfo::WV_CE_based) {
841 nModifiers |= (GetKeyState(VK_SHIFT ) < 0 ? ShiftAny : 0);
842 nModifiers |= (GetKeyState(VK_CONTROL) < 0 ? ControlAny : 0);
843 nModifiers |= (GetKeyState(VK_MENU ) < 0 ? AltAny : 0);
844 nModifiers |= (GetKeyState(VK_LWIN ) < 0 ? MetaLeft : 0);
845 nModifiers |= (GetKeyState(VK_RWIN ) < 0 ? MetaRight : 0);
846 } else {
847 // Map native modifiers to some bit representation
848 nModifiers |= (GetKeyState(VK_LSHIFT ) & 0x80 ? ShiftLeft : 0);
849 nModifiers |= (GetKeyState(VK_RSHIFT ) & 0x80 ? ShiftRight : 0);
850 nModifiers |= (GetKeyState(VK_LCONTROL) & 0x80 ? ControlLeft : 0);
851 nModifiers |= (GetKeyState(VK_RCONTROL) & 0x80 ? ControlRight : 0);
852 nModifiers |= (GetKeyState(VK_LMENU ) & 0x80 ? AltLeft : 0);
853 nModifiers |= (GetKeyState(VK_RMENU ) & 0x80 ? AltRight : 0);
854 nModifiers |= (GetKeyState(VK_LWIN ) & 0x80 ? MetaLeft : 0);
855 nModifiers |= (GetKeyState(VK_RWIN ) & 0x80 ? MetaRight : 0);
856 // Add Lock keys to the same bits
857 nModifiers |= (GetKeyState(VK_CAPITAL ) & 0x01 ? CapsLock : 0);
858 nModifiers |= (GetKeyState(VK_NUMLOCK ) & 0x01 ? NumLock : 0);
859 nModifiers |= (GetKeyState(VK_SCROLL ) & 0x01 ? ScrollLock : 0);
860 }
861 if (msg.lParam & ExtendedKey)
862 nModifiers |= msg.lParam & ExtendedKey;
863
864 // Get the modifier states (may be altered later, depending on key code)
865 int state = 0;
866 state |= (nModifiers & ShiftAny ? Qt::ShiftModifier : 0);
867 state |= (nModifiers & ControlAny ? Qt::ControlModifier : 0);
868 state |= (nModifiers & AltAny ? Qt::AltModifier : 0);
869 state |= (nModifiers & MetaAny ? Qt::MetaModifier : 0);
870
871 // Now we know enough to either have MapVirtualKey or our own keymap tell us if it's a deadkey
872 bool isDeadKey = isADeadKey(msg.wParam, state)
873 || MapVirtualKey(msg.wParam, 2) & 0x80008000; // High-order on 95 is 0x8000
874
875 // A multi-character key not found by our look-ahead
876 if (msgType == WM_CHAR) {
877 QString s;
878 QChar ch = wmchar_to_unicode(msg.wParam);
879 if (!ch.isNull())
880 s += ch;
881
882 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
883 k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
884 }
885
886 // Input method characters not found by our look-ahead
887 else if (msgType == WM_IME_CHAR) {
888 QString s;
889 QChar ch = imechar_to_unicode(msg.wParam);
890 if (!ch.isNull())
891 s += ch;
892
893 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
894 k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
895 }
896
897 else {
898 // handle Directionality changes (BiDi) with RTL extensions
899 if (qt_use_rtl_extensions) {
900 static int dirStatus = 0;
901 if (!dirStatus && state == Qt::ControlModifier
902 && msg.wParam == VK_CONTROL
903 && msgType == WM_KEYDOWN) {
904 if (GetKeyState(VK_LCONTROL) < 0)
905 dirStatus = VK_LCONTROL;
906 else if (GetKeyState(VK_RCONTROL) < 0)
907 dirStatus = VK_RCONTROL;
908 } else if (dirStatus) {
909 if (msgType == WM_KEYDOWN) {
910 if (msg.wParam == VK_SHIFT) {
911 if (dirStatus == VK_LCONTROL && GetKeyState(VK_LSHIFT) < 0)
912 dirStatus = VK_LSHIFT;
913 else if (dirStatus == VK_RCONTROL && GetKeyState(VK_RSHIFT) < 0)
914 dirStatus = VK_RSHIFT;
915 } else {
916 dirStatus = 0;
917 }
918 } else if (msgType == WM_KEYUP) {
919 if (dirStatus == VK_LSHIFT
920 && (msg.wParam == VK_SHIFT && GetKeyState(VK_LCONTROL)
921 || msg.wParam == VK_CONTROL && GetKeyState(VK_LSHIFT))) {
922 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, Qt::Key_Direction_L, 0,
923 QString(), false, 0,
924 scancode, msg.wParam, nModifiers);
925 k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, Qt::Key_Direction_L, 0,
926 QString(), false, 0,
927 scancode, msg.wParam, nModifiers);
928 dirStatus = 0;
929 } else if (dirStatus == VK_RSHIFT
930 && (msg.wParam == VK_SHIFT && GetKeyState(VK_RCONTROL)
931 || msg.wParam == VK_CONTROL && GetKeyState(VK_RSHIFT))) {
932 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, Qt::Key_Direction_R,
933 0, QString(), false, 0,
934 scancode, msg.wParam, nModifiers);
935 k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, Qt::Key_Direction_R,
936 0, QString(), false, 0,
937 scancode, msg.wParam, nModifiers);
938 dirStatus = 0;
939 } else {
940 dirStatus = 0;
941 }
942 } else {
943 dirStatus = 0;
944 }
945 }
946 }
947
948 // IME will process these keys, so simply return
949 if(msg.wParam == VK_PROCESSKEY)
950 return true;
951
952 // Ignore invalid virtual keycode (see bug 127424)
953 if (msg.wParam == 0xFF)
954 return true;
955
956 // Translate VK_* (native) -> Key_* (Qt) keys
957 // If it's a dead key, we cannot use the toKeyOrUnicode() function, since that will change
958 // the internal state of the keyboard driver, resulting in that dead keys no longer works.
959 // ..also if we're typing numbers on the keypad, while holding down the Alt modifier.
960 int code = 0;
961 if (isNumpad && (nModifiers & AltAny)) {
962 code = KeyTbl[msg.wParam];
963 } else if (!isDeadKey) {
964 unsigned char kbdBuffer[256]; // Will hold the complete keyboard state
965 GetKeyboardState(kbdBuffer);
966 code = toKeyOrUnicode(msg.wParam, scancode, kbdBuffer);
967 }
968
969 // Invert state logic:
970 // If the key actually pressed is a modifier key, then we remove its modifier key from the
971 // state, since a modifier-key can't have itself as a modifier
972 if (code == Qt::Key_Control)
973 state = state ^ Qt::ControlModifier;
974 else if (code == Qt::Key_Shift)
975 state = state ^ Qt::ShiftModifier;
976 else if (code == Qt::Key_Alt)
977 state = state ^ Qt::AltModifier;
978
979 // If the bit 24 of lParm is set you received a enter,
980 // otherwise a Return. (This is the extended key bit)
981 if ((code == Qt::Key_Return) && (msg.lParam & 0x1000000))
982 code = Qt::Key_Enter;
983
984 // All cursor keys without extended bit
985 if (!(msg.lParam & 0x1000000)) {
986 switch (code) {
987 case Qt::Key_Left:
988 case Qt::Key_Right:
989 case Qt::Key_Up:
990 case Qt::Key_Down:
991 case Qt::Key_PageUp:
992 case Qt::Key_PageDown:
993 case Qt::Key_Home:
994 case Qt::Key_End:
995 case Qt::Key_Insert:
996 case Qt::Key_Delete:
997 case Qt::Key_Asterisk:
998 case Qt::Key_Plus:
999 case Qt::Key_Minus:
1000 case Qt::Key_Period:
1001 case Qt::Key_0:
1002 case Qt::Key_1:
1003 case Qt::Key_2:
1004 case Qt::Key_3:
1005 case Qt::Key_4:
1006 case Qt::Key_5:
1007 case Qt::Key_6:
1008 case Qt::Key_7:
1009 case Qt::Key_8:
1010 case Qt::Key_9:
1011 state |= ((msg.wParam >= '0' && msg.wParam <= '9')
1012 || (msg.wParam >= VK_OEM_PLUS && msg.wParam <= VK_OEM_3))
1013 ? 0 : Qt::KeypadModifier;
1014 default:
1015 if ((uint)msg.lParam == 0x004c0001 || (uint)msg.lParam == 0xc04c0001)
1016 state |= Qt::KeypadModifier;
1017 break;
1018 }
1019 }
1020 // Other keys with with extended bit
1021 else {
1022 switch (code) {
1023 case Qt::Key_Enter:
1024 case Qt::Key_Slash:
1025 case Qt::Key_NumLock:
1026 state |= Qt::KeypadModifier;
1027 default:
1028 break;
1029 }
1030 }
1031
1032 // KEYDOWN ---------------------------------------------------------------------------------
1033 if (msgType == WM_KEYDOWN || msgType == WM_IME_KEYDOWN || msgType == WM_SYSKEYDOWN) {
1034 // Get the last record of this key press, so we can validate the current state
1035 // The record is not removed from the list
1036 KeyRecord *rec = key_recorder.findKey(msg.wParam, false);
1037
1038 // If rec's state doesn't match the current state, something has changed behind our back
1039 // (Consumed by modal widget is one possibility) So, remove the record from the list
1040 // This will stop the auto-repeat of the key, should a modifier change, for example
1041 if (rec && rec->state != state) {
1042 key_recorder.findKey(msg.wParam, true);
1043 rec = 0;
1044 }
1045
1046 // Find unicode character from Windows Message Queue
1047 MSG wm_char;
1048 UINT charType = (msgType == WM_KEYDOWN
1049 ? WM_CHAR
1050 : msgType == WM_IME_KEYDOWN ? WM_IME_CHAR : WM_SYSCHAR);
1051
1052 QChar uch;
1053 if (winPeekMessage(&wm_char, 0, charType, charType, PM_REMOVE)) {
1054 // Found a ?_CHAR
1055 uch = charType == WM_IME_CHAR
1056 ? imechar_to_unicode(wm_char.wParam)
1057 : wmchar_to_unicode(wm_char.wParam);
1058 if (msgType == WM_SYSKEYDOWN && uch.isLetter() && (msg.lParam & KF_ALTDOWN))
1059 uch = uch.toLower(); // (See doc of WM_SYSCHAR) Alt-letter
1060 if (!code && !uch.row())
1061 code = asciiToKeycode(uch.cell(), state);
1062 }
1063
1064 // Special handling for the WM_IME_KEYDOWN message. Microsoft IME (Korean) will not
1065 // generate a WM_IME_CHAR message corresponding to this message. We might get wrong
1066 // results, if we map this virtual key-code directly (for eg '?' US layouts). So try
1067 // to find the correct key using the current message parameters & keyboard state.
1068 if (uch.isNull() && msgType == WM_IME_KEYDOWN) {
1069 BYTE keyState[256];
1070 WCHAR newKey[3] = {0};
1071 GetKeyboardState(keyState);
1072 int val = ToUnicode(vk_key, scancode, keyState, newKey, 2, 0);
1073 if (val == 1) {
1074 uch = QChar(newKey[0]);
1075 } else {
1076 // If we are still not able to find a unicode key, pass the WM_IME_KEYDOWN
1077 // message to DefWindowProc() for generating a proper WM_KEYDOWN.
1078 return false;
1079 }
1080 }
1081
1082 // If no ?_CHAR was found in the queue; deduct character from the ?_KEYDOWN parameters
1083 if (uch.isNull()) {
1084 if (msg.wParam == VK_DELETE) {
1085 uch = QChar(QLatin1Char(0x7f)); // Windows doesn't know this one.
1086 } else {
1087 if (msgType != WM_SYSKEYDOWN || !code) {
1088 UINT map;
1089 QT_WA({
1090 map = MapVirtualKey(msg.wParam, 2);
1091 } , {
1092 map = MapVirtualKeyA(msg.wParam, 2);
1093 // High-order bit is 0x8000 on '95
1094 if (map & 0x8000)
1095 map = (map^0x8000)|0x80000000;
1096 });
1097 // If the high bit of the return value is set, it's a deadkey
1098 if (!(map & 0x80000000))
1099 uch = wmchar_to_unicode((DWORD)map);
1100 }
1101 }
1102 if (!code && !uch.row())
1103 code = asciiToKeycode(uch.cell(), state);
1104 }
1105
1106 // Special handling of global Windows hotkeys
1107 if (state == Qt::AltModifier) {
1108 switch (code) {
1109 case Qt::Key_Escape:
1110 case Qt::Key_Tab:
1111 case Qt::Key_Enter:
1112 case Qt::Key_F4:
1113 return false; // Send the event on to Windows
1114 case Qt::Key_Space:
1115 // do not pass this key to windows, we will process it ourselves
1116 qt_show_system_menu(widget->window());
1117 return true;
1118 default:
1119 break;
1120 }
1121 }
1122
1123 // Map SHIFT + Tab to SHIFT + BackTab, QShortcutMap knows about this translation
1124 if (code == Qt::Key_Tab && (state & Qt::ShiftModifier) == Qt::ShiftModifier)
1125 code = Qt::Key_Backtab;
1126
1127 // If we have a record, it means that the key is already pressed, the state is the same
1128 // so, we have an auto-repeating key
1129 if (rec) {
1130 if (code < Qt::Key_Shift || code > Qt::Key_ScrollLock) {
1131 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, code,
1132 Qt::KeyboardModifier(state), rec->text, true, 0,
1133 scancode, msg.wParam, nModifiers);
1134 k1 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, code,
1135 Qt::KeyboardModifier(state), rec->text, true, 0,
1136 scancode, msg.wParam, nModifiers);
1137 }
1138 }
1139 // No record of the key being previous pressed, so we now send a QEvent::KeyPress event,
1140 // and store the key data into our records.
1141 else {
1142 QString text;
1143 if (!uch.isNull())
1144 text += uch;
1145 char a = uch.row() ? 0 : uch.cell();
1146 key_recorder.storeKey(msg.wParam, a, state, text);
1147 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, code, Qt::KeyboardModifier(state),
1148 text, false, 0, scancode, msg.wParam, nModifiers);
1149
1150 bool store = true;
1151 // Alt+<alphanumerical> go to the Win32 menu system if unhandled by Qt
1152#if !defined(Q_OS_WINCE)
1153 if (msgType == WM_SYSKEYDOWN && !k0 && a) {
1154 HWND parent = GetParent(widget->internalWinId());
1155 while (parent) {
1156 if (GetMenu(parent)) {
1157 SendMessage(parent, WM_SYSCOMMAND, SC_KEYMENU, a);
1158 store = false;
1159 k0 = true;
1160 break;
1161 }
1162 parent = GetParent(parent);
1163 }
1164 }
1165#endif
1166 if (!store)
1167 key_recorder.findKey(msg.wParam, true);
1168 }
1169 }
1170
1171 // KEYUP -----------------------------------------------------------------------------------
1172 else {
1173 // Try to locate the key in our records, and remove it if it exists.
1174 // The key may not be in our records if, for example, the down event was handled by
1175 // win32 natively, or our window gets focus while a key is already press, but now gets
1176 // the key release event.
1177 KeyRecord* rec = key_recorder.findKey(msg.wParam, true);
1178 if (!rec && !(code == Qt::Key_Shift
1179 || code == Qt::Key_Control
1180 || code == Qt::Key_Meta
1181 || code == Qt::Key_Alt)) {
1182 // Someone ate the key down event
1183 } else {
1184 if (!code)
1185 code = asciiToKeycode(rec->ascii ? rec->ascii : msg.wParam, state);
1186
1187 // Map SHIFT + Tab to SHIFT + BackTab, QShortcutMap knows about this translation
1188 if (code == Qt::Key_Tab && (state & Qt::ShiftModifier) == Qt::ShiftModifier)
1189 code = Qt::Key_Backtab;
1190
1191 k0 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, code, Qt::KeyboardModifier(state),
1192 (rec ? rec->text : QString()), false, 0, scancode, msg.wParam, nModifiers);
1193
1194 // don't pass Alt to Windows unless we are embedded in a non-Qt window
1195#if !defined(Q_OS_WINCE)
1196 if (code == Qt::Key_Alt) {
1197 k0 = true;
1198 HWND parent = GetParent(widget->internalWinId());
1199 while (parent) {
1200 if (!QWidget::find(parent) && GetMenu(parent)) {
1201 k0 = false;
1202 break;
1203 }
1204 parent = GetParent(parent);
1205 }
1206 }
1207#endif
1208 }
1209 }
1210 }
1211
1212 // Return true, if a QKeyEvent was sent to a widget
1213 return k0 || k1;
1214}
1215
1216
1217// QKeyMapper (Windows) implementation -------------------------------------------------[ start ]---
1218
1219bool QKeyMapper::sendKeyEvent(QWidget *widget, bool grab,
1220 QEvent::Type type, int code, Qt::KeyboardModifiers modifiers,
1221 const QString &text, bool autorepeat, int count,
1222 quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers,
1223 bool *)
1224{
1225#if defined(Q_OS_WINCE)
1226 Q_UNUSED(grab);
1227#endif
1228 Q_UNUSED(count);
1229#if defined QT3_SUPPORT && !defined(QT_NO_SHORTCUT)
1230 if (type == QEvent::KeyPress
1231 && !grab
1232 && QApplicationPrivate::instance()->use_compat()) {
1233 // send accel events if the keyboard is not grabbed
1234 QKeyEventEx a(type, code, modifiers,
1235 text, autorepeat, qMax(1, int(text.length())),
1236 nativeScanCode, nativeVirtualKey, nativeModifiers);
1237 if (QApplicationPrivate::instance()->qt_tryAccelEvent(widget, &a))
1238 return true;
1239 }
1240#endif
1241 if (!widget->isEnabled())
1242 return false;
1243
1244 QKeyEventEx e(type, code, modifiers,
1245 text, autorepeat, qMax(1, int(text.length())),
1246 nativeScanCode, nativeVirtualKey, nativeModifiers);
1247 QETWidget::sendSpontaneousEvent(widget, &e);
1248
1249 if (!isModifierKey(code)
1250 && modifiers == Qt::AltModifier
1251 && ((code >= Qt::Key_A && code <= Qt::Key_Z) || (code >= Qt::Key_0 && code <= Qt::Key_9))
1252 && type == QEvent::KeyPress
1253 && !e.isAccepted())
1254 QApplication::beep(); // Emulate windows behavior
1255
1256 return e.isAccepted();
1257}
1258
1259QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.