Qt 4.8
qkeymapper_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qkeymapper_p.h"
43 
44 #include <qt_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 
54 
55 // Uncommend, to show debugging information for the keymapper
56 //#define DEBUG_KEYMAPPER
57 
58 // Implemented elsewhere
59 extern "C" LRESULT QT_WIN_CALLBACK QtWndProc(HWND, UINT, WPARAM, LPARAM);
60 
62 #ifndef LANG_PASHTO
63 #define LANG_PASHTO 0x63
64 #endif
65 #ifndef LANG_SYRIAC
66 #define LANG_SYRIAC 0x5a
67 #endif
68 #ifndef LANG_DIVEHI
69 #define LANG_DIVEHI 0x65
70 #endif
71 #ifndef VK_OEM_PLUS
72 #define VK_OEM_PLUS 0xBB
73 #endif
74 #ifndef VK_OEM_3
75 #define VK_OEM_3 0xC0
76 #endif
77 #ifndef MAPVK_VK_TO_CHAR
78 #define MAPVK_VK_TO_CHAR (2)
79 #endif
80 
81 #if defined(Q_OS_WINCE)
82 bool GetKeyboardState(unsigned char* kbuffer)
83 {
84  for (int i=0; i< 256; ++i)
85  kbuffer[i] = GetAsyncKeyState(i);
86  return true;
87 }
88 #endif
89 
90 // We not only need the scancode itself but also the extended bit of key messages. Thus we need
91 // the additional bit when masking the scancode.
92 enum { scancodeBitmask = 0x1ff };
93 
94 
95 // Key recorder ------------------------------------------------------------------------[ start ] --
96 struct KeyRecord {
97  KeyRecord(int c, int a, int s, const QString &t) : code(c), ascii(a), state(s), text(t) {}
98  KeyRecord() {}
99 
100  int code;
101  int ascii;
102  int state;
104 };
105 
106 static const int QT_MAX_KEY_RECORDINGS = 64; // User has LOTS of fingers...
108 {
109  KeyRecorder() : nrecs(0) {}
110 
111  inline KeyRecord *findKey(int code, bool remove);
112  inline void storeKey(int code, int ascii, int state, const QString& text);
113  inline void clearKeys();
114 
115  int nrecs;
116  KeyRecord deleted_record; // A copy of last entry removed from records[]
118 };
120 
122 {
123  KeyRecord *result = 0;
124  for (int i = 0; i < nrecs; ++i) {
125  if (records[i].code == code) {
126  if (remove) {
127  deleted_record = records[i];
128  // Move rest down, and decrease count
129  while (i + 1 < nrecs) {
130  records[i] = records[i + 1];
131  ++i;
132  }
133  --nrecs;
134  result = &deleted_record;
135  } else {
136  result = &records[i];
137  }
138  break;
139  }
140  }
141  return result;
142 }
143 
144 void KeyRecorder::storeKey(int code, int ascii, int state, const QString& text)
145 {
147  "Internal KeyRecorder",
148  "Keyboard recorder buffer overflow, consider increasing QT_MAX_KEY_RECORDINGS");
149 
150  if (nrecs == QT_MAX_KEY_RECORDINGS) {
151  qWarning("Qt: Internal keyboard buffer overflow");
152  return;
153  }
154  records[nrecs++] = KeyRecord(code,ascii,state,text);
155 }
156 
158 {
159  nrecs = 0;
160 }
161 // Key recorder --------------------------------------------------------------------------[ end ] --
162 
163 
164 // Key translation ---------------------------------------------------------------------[ start ] --
165 // Meaning of values:
166 // 0 = Character output key, needs keyboard driver mapping
167 // Key_unknown = Unknown Virtual Key, no translation possible, ignore
168 static const uint KeyTbl[] = { // Keyboard mapping table
169  // Dec | Hex | Windows Virtual key
170  Qt::Key_unknown, // 0 0x00
171  Qt::Key_unknown, // 1 0x01 VK_LBUTTON | Left mouse button
172  Qt::Key_unknown, // 2 0x02 VK_RBUTTON | Right mouse button
173  Qt::Key_Cancel, // 3 0x03 VK_CANCEL | Control-Break processing
174  Qt::Key_unknown, // 4 0x04 VK_MBUTTON | Middle mouse button
175  Qt::Key_unknown, // 5 0x05 VK_XBUTTON1 | X1 mouse button
176  Qt::Key_unknown, // 6 0x06 VK_XBUTTON2 | X2 mouse button
177  Qt::Key_unknown, // 7 0x07 -- unassigned --
178  Qt::Key_Backspace, // 8 0x08 VK_BACK | BackSpace key
179  Qt::Key_Tab, // 9 0x09 VK_TAB | Tab key
180  Qt::Key_unknown, // 10 0x0A -- reserved --
181  Qt::Key_unknown, // 11 0x0B -- reserved --
182  Qt::Key_Clear, // 12 0x0C VK_CLEAR | Clear key
183  Qt::Key_Return, // 13 0x0D VK_RETURN | Enter key
184  Qt::Key_unknown, // 14 0x0E -- unassigned --
185  Qt::Key_unknown, // 15 0x0F -- unassigned --
186  Qt::Key_Shift, // 16 0x10 VK_SHIFT | Shift key
187  Qt::Key_Control, // 17 0x11 VK_CONTROL | Ctrl key
188  Qt::Key_Alt, // 18 0x12 VK_MENU | Alt key
189  Qt::Key_Pause, // 19 0x13 VK_PAUSE | Pause key
190  Qt::Key_CapsLock, // 20 0x14 VK_CAPITAL | Caps-Lock
191  Qt::Key_unknown, // 21 0x15 VK_KANA / VK_HANGUL | IME Kana or Hangul mode
192  Qt::Key_unknown, // 22 0x16 -- unassigned --
193  Qt::Key_unknown, // 23 0x17 VK_JUNJA | IME Junja mode
194  Qt::Key_unknown, // 24 0x18 VK_FINAL | IME final mode
195  Qt::Key_unknown, // 25 0x19 VK_HANJA / VK_KANJI | IME Hanja or Kanji mode
196  Qt::Key_unknown, // 26 0x1A -- unassigned --
197  Qt::Key_Escape, // 27 0x1B VK_ESCAPE | Esc key
198  Qt::Key_unknown, // 28 0x1C VK_CONVERT | IME convert
199  Qt::Key_unknown, // 29 0x1D VK_NONCONVERT | IME non-convert
200  Qt::Key_unknown, // 30 0x1E VK_ACCEPT | IME accept
201  Qt::Key_Mode_switch,// 31 0x1F VK_MODECHANGE | IME mode change request
202  Qt::Key_Space, // 32 0x20 VK_SPACE | Spacebar
203  Qt::Key_PageUp, // 33 0x21 VK_PRIOR | Page Up key
204  Qt::Key_PageDown, // 34 0x22 VK_NEXT | Page Down key
205  Qt::Key_End, // 35 0x23 VK_END | End key
206  Qt::Key_Home, // 36 0x24 VK_HOME | Home key
207  Qt::Key_Left, // 37 0x25 VK_LEFT | Left arrow key
208  Qt::Key_Up, // 38 0x26 VK_UP | Up arrow key
209  Qt::Key_Right, // 39 0x27 VK_RIGHT | Right arrow key
210  Qt::Key_Down, // 40 0x28 VK_DOWN | Down arrow key
211  Qt::Key_Select, // 41 0x29 VK_SELECT | Select key
212  Qt::Key_Printer, // 42 0x2A VK_PRINT | Print key
213  Qt::Key_Execute, // 43 0x2B VK_EXECUTE | Execute key
214  Qt::Key_Print, // 44 0x2C VK_SNAPSHOT | Print Screen key
215  Qt::Key_Insert, // 45 0x2D VK_INSERT | Ins key
216  Qt::Key_Delete, // 46 0x2E VK_DELETE | Del key
217  Qt::Key_Help, // 47 0x2F VK_HELP | Help key
218  0, // 48 0x30 (VK_0) | 0 key
219  0, // 49 0x31 (VK_1) | 1 key
220  0, // 50 0x32 (VK_2) | 2 key
221  0, // 51 0x33 (VK_3) | 3 key
222  0, // 52 0x34 (VK_4) | 4 key
223  0, // 53 0x35 (VK_5) | 5 key
224  0, // 54 0x36 (VK_6) | 6 key
225  0, // 55 0x37 (VK_7) | 7 key
226  0, // 56 0x38 (VK_8) | 8 key
227  0, // 57 0x39 (VK_9) | 9 key
228  Qt::Key_unknown, // 58 0x3A -- unassigned --
229  Qt::Key_unknown, // 59 0x3B -- unassigned --
230  Qt::Key_unknown, // 60 0x3C -- unassigned --
231  Qt::Key_unknown, // 61 0x3D -- unassigned --
232  Qt::Key_unknown, // 62 0x3E -- unassigned --
233  Qt::Key_unknown, // 63 0x3F -- unassigned --
234  Qt::Key_unknown, // 64 0x40 -- unassigned --
235  0, // 65 0x41 (VK_A) | A key
236  0, // 66 0x42 (VK_B) | B key
237  0, // 67 0x43 (VK_C) | C key
238  0, // 68 0x44 (VK_D) | D key
239  0, // 69 0x45 (VK_E) | E key
240  0, // 70 0x46 (VK_F) | F key
241  0, // 71 0x47 (VK_G) | G key
242  0, // 72 0x48 (VK_H) | H key
243  0, // 73 0x49 (VK_I) | I key
244  0, // 74 0x4A (VK_J) | J key
245  0, // 75 0x4B (VK_K) | K key
246  0, // 76 0x4C (VK_L) | L key
247  0, // 77 0x4D (VK_M) | M key
248  0, // 78 0x4E (VK_N) | N key
249  0, // 79 0x4F (VK_O) | O key
250  0, // 80 0x50 (VK_P) | P key
251  0, // 81 0x51 (VK_Q) | Q key
252  0, // 82 0x52 (VK_R) | R key
253  0, // 83 0x53 (VK_S) | S key
254  0, // 84 0x54 (VK_T) | T key
255  0, // 85 0x55 (VK_U) | U key
256  0, // 86 0x56 (VK_V) | V key
257  0, // 87 0x57 (VK_W) | W key
258  0, // 88 0x58 (VK_X) | X key
259  0, // 89 0x59 (VK_Y) | Y key
260  0, // 90 0x5A (VK_Z) | Z key
261  Qt::Key_Meta, // 91 0x5B VK_LWIN | Left Windows - MS Natural kbd
262  Qt::Key_Meta, // 92 0x5C VK_RWIN | Right Windows - MS Natural kbd
263  Qt::Key_Menu, // 93 0x5D VK_APPS | Application key-MS Natural kbd
264  Qt::Key_unknown, // 94 0x5E -- reserved --
265  Qt::Key_Sleep, // 95 0x5F VK_SLEEP
266  Qt::Key_0, // 96 0x60 VK_NUMPAD0 | Numeric keypad 0 key
267  Qt::Key_1, // 97 0x61 VK_NUMPAD1 | Numeric keypad 1 key
268  Qt::Key_2, // 98 0x62 VK_NUMPAD2 | Numeric keypad 2 key
269  Qt::Key_3, // 99 0x63 VK_NUMPAD3 | Numeric keypad 3 key
270  Qt::Key_4, // 100 0x64 VK_NUMPAD4 | Numeric keypad 4 key
271  Qt::Key_5, // 101 0x65 VK_NUMPAD5 | Numeric keypad 5 key
272  Qt::Key_6, // 102 0x66 VK_NUMPAD6 | Numeric keypad 6 key
273  Qt::Key_7, // 103 0x67 VK_NUMPAD7 | Numeric keypad 7 key
274  Qt::Key_8, // 104 0x68 VK_NUMPAD8 | Numeric keypad 8 key
275  Qt::Key_9, // 105 0x69 VK_NUMPAD9 | Numeric keypad 9 key
276  Qt::Key_Asterisk, // 106 0x6A VK_MULTIPLY | Multiply key
277  Qt::Key_Plus, // 107 0x6B VK_ADD | Add key
278  Qt::Key_Comma, // 108 0x6C VK_SEPARATOR | Separator key
279  Qt::Key_Minus, // 109 0x6D VK_SUBTRACT | Subtract key
280  Qt::Key_Period, // 110 0x6E VK_DECIMAL | Decimal key
281  Qt::Key_Slash, // 111 0x6F VK_DIVIDE | Divide key
282  Qt::Key_F1, // 112 0x70 VK_F1 | F1 key
283  Qt::Key_F2, // 113 0x71 VK_F2 | F2 key
284  Qt::Key_F3, // 114 0x72 VK_F3 | F3 key
285  Qt::Key_F4, // 115 0x73 VK_F4 | F4 key
286  Qt::Key_F5, // 116 0x74 VK_F5 | F5 key
287  Qt::Key_F6, // 117 0x75 VK_F6 | F6 key
288  Qt::Key_F7, // 118 0x76 VK_F7 | F7 key
289  Qt::Key_F8, // 119 0x77 VK_F8 | F8 key
290  Qt::Key_F9, // 120 0x78 VK_F9 | F9 key
291  Qt::Key_F10, // 121 0x79 VK_F10 | F10 key
292  Qt::Key_F11, // 122 0x7A VK_F11 | F11 key
293  Qt::Key_F12, // 123 0x7B VK_F12 | F12 key
294  Qt::Key_F13, // 124 0x7C VK_F13 | F13 key
295  Qt::Key_F14, // 125 0x7D VK_F14 | F14 key
296  Qt::Key_F15, // 126 0x7E VK_F15 | F15 key
297  Qt::Key_F16, // 127 0x7F VK_F16 | F16 key
298  Qt::Key_F17, // 128 0x80 VK_F17 | F17 key
299  Qt::Key_F18, // 129 0x81 VK_F18 | F18 key
300  Qt::Key_F19, // 130 0x82 VK_F19 | F19 key
301  Qt::Key_F20, // 131 0x83 VK_F20 | F20 key
302  Qt::Key_F21, // 132 0x84 VK_F21 | F21 key
303  Qt::Key_F22, // 133 0x85 VK_F22 | F22 key
304  Qt::Key_F23, // 134 0x86 VK_F23 | F23 key
305  Qt::Key_F24, // 135 0x87 VK_F24 | F24 key
306  Qt::Key_unknown, // 136 0x88 -- unassigned --
307  Qt::Key_unknown, // 137 0x89 -- unassigned --
308  Qt::Key_unknown, // 138 0x8A -- unassigned --
309  Qt::Key_unknown, // 139 0x8B -- unassigned --
310  Qt::Key_unknown, // 140 0x8C -- unassigned --
311  Qt::Key_unknown, // 141 0x8D -- unassigned --
312  Qt::Key_unknown, // 142 0x8E -- unassigned --
313  Qt::Key_unknown, // 143 0x8F -- unassigned --
314  Qt::Key_NumLock, // 144 0x90 VK_NUMLOCK | Num Lock key
315  Qt::Key_ScrollLock, // 145 0x91 VK_SCROLL | Scroll Lock key
316  // Fujitsu/OASYS kbd --------------------
317  0, //Qt::Key_Jisho, // 146 0x92 VK_OEM_FJ_JISHO | 'Dictionary' key /
318  // VK_OEM_NEC_EQUAL = key on numpad on NEC PC-9800 kbd
319  Qt::Key_Massyo, // 147 0x93 VK_OEM_FJ_MASSHOU | 'Unregister word' key
320  Qt::Key_Touroku, // 148 0x94 VK_OEM_FJ_TOUROKU | 'Register word' key
321  0, //Qt::Key_Oyayubi_Left,//149 0x95 VK_OEM_FJ_LOYA | 'Left OYAYUBI' key
322  0, //Qt::Key_Oyayubi_Right,//150 0x96 VK_OEM_FJ_ROYA | 'Right OYAYUBI' key
323  Qt::Key_unknown, // 151 0x97 -- unassigned --
324  Qt::Key_unknown, // 152 0x98 -- unassigned --
325  Qt::Key_unknown, // 153 0x99 -- unassigned --
326  Qt::Key_unknown, // 154 0x9A -- unassigned --
327  Qt::Key_unknown, // 155 0x9B -- unassigned --
328  Qt::Key_unknown, // 156 0x9C -- unassigned --
329  Qt::Key_unknown, // 157 0x9D -- unassigned --
330  Qt::Key_unknown, // 158 0x9E -- unassigned --
331  Qt::Key_unknown, // 159 0x9F -- unassigned --
332  Qt::Key_Shift, // 160 0xA0 VK_LSHIFT | Left Shift key
333  Qt::Key_Shift, // 161 0xA1 VK_RSHIFT | Right Shift key
334  Qt::Key_Control, // 162 0xA2 VK_LCONTROL | Left Ctrl key
335  Qt::Key_Control, // 163 0xA3 VK_RCONTROL | Right Ctrl key
336  Qt::Key_Alt, // 164 0xA4 VK_LMENU | Left Menu key
337  Qt::Key_Alt, // 165 0xA5 VK_RMENU | Right Menu key
338  Qt::Key_Back, // 166 0xA6 VK_BROWSER_BACK | Browser Back key
339  Qt::Key_Forward, // 167 0xA7 VK_BROWSER_FORWARD | Browser Forward key
340  Qt::Key_Refresh, // 168 0xA8 VK_BROWSER_REFRESH | Browser Refresh key
341  Qt::Key_Stop, // 169 0xA9 VK_BROWSER_STOP | Browser Stop key
342  Qt::Key_Search, // 170 0xAA VK_BROWSER_SEARCH | Browser Search key
343  Qt::Key_Favorites, // 171 0xAB VK_BROWSER_FAVORITES| Browser Favorites key
344  Qt::Key_HomePage, // 172 0xAC VK_BROWSER_HOME | Browser Start and Home key
345  Qt::Key_VolumeMute, // 173 0xAD VK_VOLUME_MUTE | Volume Mute key
346  Qt::Key_VolumeDown, // 174 0xAE VK_VOLUME_DOWN | Volume Down key
347  Qt::Key_VolumeUp, // 175 0xAF VK_VOLUME_UP | Volume Up key
348  Qt::Key_MediaNext, // 176 0xB0 VK_MEDIA_NEXT_TRACK | Next Track key
349  Qt::Key_MediaPrevious, //177 0xB1 VK_MEDIA_PREV_TRACK | Previous Track key
350  Qt::Key_MediaStop, // 178 0xB2 VK_MEDIA_STOP | Stop Media key
351  Qt::Key_MediaPlay, // 179 0xB3 VK_MEDIA_PLAY_PAUSE | Play/Pause Media key
352  Qt::Key_LaunchMail, // 180 0xB4 VK_LAUNCH_MAIL | Start Mail key
353  Qt::Key_LaunchMedia,// 181 0xB5 VK_LAUNCH_MEDIA_SELECT Select Media key
354  Qt::Key_Launch0, // 182 0xB6 VK_LAUNCH_APP1 | Start Application 1 key
355  Qt::Key_Launch1, // 183 0xB7 VK_LAUNCH_APP2 | Start Application 2 key
356  Qt::Key_unknown, // 184 0xB8 -- reserved --
357  Qt::Key_unknown, // 185 0xB9 -- reserved --
358  0, // 186 0xBA VK_OEM_1 | ';:' for US
359  0, // 187 0xBB VK_OEM_PLUS | '+' any country
360  0, // 188 0xBC VK_OEM_COMMA | ',' any country
361  0, // 189 0xBD VK_OEM_MINUS | '-' any country
362  0, // 190 0xBE VK_OEM_PERIOD | '.' any country
363  0, // 191 0xBF VK_OEM_2 | '/?' for US
364  0, // 192 0xC0 VK_OEM_3 | '`~' for US
365  Qt::Key_unknown, // 193 0xC1 -- reserved --
366  Qt::Key_unknown, // 194 0xC2 -- reserved --
367  Qt::Key_unknown, // 195 0xC3 -- reserved --
368  Qt::Key_unknown, // 196 0xC4 -- reserved --
369  Qt::Key_unknown, // 197 0xC5 -- reserved --
370  Qt::Key_unknown, // 198 0xC6 -- reserved --
371  Qt::Key_unknown, // 199 0xC7 -- reserved --
372  Qt::Key_unknown, // 200 0xC8 -- reserved --
373  Qt::Key_unknown, // 201 0xC9 -- reserved --
374  Qt::Key_unknown, // 202 0xCA -- reserved --
375  Qt::Key_unknown, // 203 0xCB -- reserved --
376  Qt::Key_unknown, // 204 0xCC -- reserved --
377  Qt::Key_unknown, // 205 0xCD -- reserved --
378  Qt::Key_unknown, // 206 0xCE -- reserved --
379  Qt::Key_unknown, // 207 0xCF -- reserved --
380  Qt::Key_unknown, // 208 0xD0 -- reserved --
381  Qt::Key_unknown, // 209 0xD1 -- reserved --
382  Qt::Key_unknown, // 210 0xD2 -- reserved --
383  Qt::Key_unknown, // 211 0xD3 -- reserved --
384  Qt::Key_unknown, // 212 0xD4 -- reserved --
385  Qt::Key_unknown, // 213 0xD5 -- reserved --
386  Qt::Key_unknown, // 214 0xD6 -- reserved --
387  Qt::Key_unknown, // 215 0xD7 -- reserved --
388  Qt::Key_unknown, // 216 0xD8 -- unassigned --
389  Qt::Key_unknown, // 217 0xD9 -- unassigned --
390  Qt::Key_unknown, // 218 0xDA -- unassigned --
391  0, // 219 0xDB VK_OEM_4 | '[{' for US
392  0, // 220 0xDC VK_OEM_5 | '\|' for US
393  0, // 221 0xDD VK_OEM_6 | ']}' for US
394  0, // 222 0xDE VK_OEM_7 | ''"' for US
395  0, // 223 0xDF VK_OEM_8
396  Qt::Key_unknown, // 224 0xE0 -- reserved --
397  Qt::Key_unknown, // 225 0xE1 VK_OEM_AX | 'AX' key on Japanese AX kbd
398  Qt::Key_unknown, // 226 0xE2 VK_OEM_102 | "<>" or "\|" on RT 102-key kbd
399  Qt::Key_unknown, // 227 0xE3 VK_ICO_HELP | Help key on ICO
400  Qt::Key_unknown, // 228 0xE4 VK_ICO_00 | 00 key on ICO
401  Qt::Key_unknown, // 229 0xE5 VK_PROCESSKEY | IME Process key
402  Qt::Key_unknown, // 230 0xE6 VK_ICO_CLEAR |
403  Qt::Key_unknown, // 231 0xE7 VK_PACKET | Unicode char as keystrokes
404  Qt::Key_unknown, // 232 0xE8 -- unassigned --
405  // Nokia/Ericsson definitions ---------------
406  Qt::Key_unknown, // 233 0xE9 VK_OEM_RESET
407  Qt::Key_unknown, // 234 0xEA VK_OEM_JUMP
408  Qt::Key_unknown, // 235 0xEB VK_OEM_PA1
409  Qt::Key_unknown, // 236 0xEC VK_OEM_PA2
410  Qt::Key_unknown, // 237 0xED VK_OEM_PA3
411  Qt::Key_unknown, // 238 0xEE VK_OEM_WSCTRL
412  Qt::Key_unknown, // 239 0xEF VK_OEM_CUSEL
413  Qt::Key_unknown, // 240 0xF0 VK_OEM_ATTN
414  Qt::Key_unknown, // 241 0xF1 VK_OEM_FINISH
415  Qt::Key_unknown, // 242 0xF2 VK_OEM_COPY
416  Qt::Key_unknown, // 243 0xF3 VK_OEM_AUTO
417  Qt::Key_unknown, // 244 0xF4 VK_OEM_ENLW
418  Qt::Key_unknown, // 245 0xF5 VK_OEM_BACKTAB
419  Qt::Key_unknown, // 246 0xF6 VK_ATTN | Attn key
420  Qt::Key_unknown, // 247 0xF7 VK_CRSEL | CrSel key
421  Qt::Key_unknown, // 248 0xF8 VK_EXSEL | ExSel key
422  Qt::Key_unknown, // 249 0xF9 VK_EREOF | Erase EOF key
423  Qt::Key_Play, // 250 0xFA VK_PLAY | Play key
424  Qt::Key_Zoom, // 251 0xFB VK_ZOOM | Zoom key
425  Qt::Key_unknown, // 252 0xFC VK_NONAME | Reserved
426  Qt::Key_unknown, // 253 0xFD VK_PA1 | PA1 key
427  Qt::Key_Clear, // 254 0xFE VK_OEM_CLEAR | Clear key
428  0
429 };
430 
431 // Possible modifier states.
432 // NOTE: The order of these states match the order in QKeyMapperPrivate::updatePossibleKeyCodes()!
433 static const Qt::KeyboardModifiers ModsTbl[] = {
434  Qt::NoModifier, // 0
435  Qt::ShiftModifier, // 1
436  Qt::ControlModifier, // 2
437  Qt::ControlModifier | Qt::ShiftModifier, // 3
438  Qt::AltModifier, // 4
439  Qt::AltModifier | Qt::ShiftModifier, // 5
440  Qt::AltModifier | Qt::ControlModifier, // 6
441  Qt::AltModifier | Qt::ShiftModifier | Qt::ControlModifier, // 7
442  Qt::NoModifier, // Fall-back to raw Key_*
443 };
444 
448 inline int winceKeyBend(int keyCode)
449 {
450 #if defined(Q_OS_WINCE_WM) && defined(QT_KEYPAD_NAVIGATION)
451  // remap return or action key to select key for windows mobile.
452  // will be changed to a table remapping function in the next version (4.6/7).
453  if (keyCode == VK_RETURN && QApplication::keypadNavigationEnabled())
454  return Qt::Key_Select;
455  else
456  return KeyTbl[keyCode];
457 #else
458  return KeyTbl[keyCode];
459 #endif
460 }
461 
462 #if defined(Q_OS_WINCE)
463  // Use the KeyTbl to resolve a Qt::Key out of the virtual keys.
464  // In case it is not resolvable, continue using the virtual key itself.
465 
467 
468 int ToUnicode(UINT vk, int /*scancode*/, unsigned char* /*kbdBuffer*/, LPWSTR unicodeBuffer, int, int)
469 {
471  QChar* buf = reinterpret_cast< QChar*>(unicodeBuffer);
472  if (KeyTbl[vk] == 0) {
473  buf[0] = vk;
474  return 1;
475  }
476  return 0;
477 }
478 
479 int ToAscii(UINT vk, int scancode, unsigned char *kbdBuffer, LPWORD unicodeBuffer, int flag)
480 {
481  return ToUnicode(vk, scancode, kbdBuffer, (LPWSTR) unicodeBuffer, 0, flag);
482 
483 }
485 
486 #endif
487 
488 // Translate a VK into a Qt key code, or unicode character
489 static inline int toKeyOrUnicode(int vk, int scancode, unsigned char *kbdBuffer, bool *isDeadkey = 0)
490 {
491  Q_ASSERT(vk > 0 && vk < 256);
492  int code = 0;
493  QChar unicodeBuffer[5];
494  int res = ToUnicode(vk, scancode, kbdBuffer, reinterpret_cast<LPWSTR>(unicodeBuffer), 5, 0);
495  // When Ctrl modifier is used ToUnicode does not return correct values. In order to assign the
496  // right key the control modifier is removed for just that function if the previous call failed.
497  if (res == 0 && kbdBuffer[VK_CONTROL]) {
498  const unsigned char controlState = kbdBuffer[VK_CONTROL];
499  kbdBuffer[VK_CONTROL] = 0;
500  res = ToUnicode(vk, scancode, kbdBuffer, reinterpret_cast<LPWSTR>(unicodeBuffer), 5, 0);
501  kbdBuffer[VK_CONTROL] = controlState;
502  }
503  if (res)
504  code = unicodeBuffer[0].toUpper().unicode();
505 
506  // Qt::Key_*'s are not encoded below 0x20, so try again, and DEL keys (0x7f) is encoded with a
507  // proper Qt::Key_ code
508  if (code < 0x20 || code == 0x7f) // Handles res==0 too
509  code = winceKeyBend(vk);
510 
511  if (isDeadkey)
512  *isDeadkey = (res == -1);
513 
514  return code == Qt::Key_unknown ? 0 : code;
515 }
516 
518 {
519  int code = winceKeyBend((vk < 0 || vk > 255) ? 0 : vk);
520  return code == Qt::Key_unknown ? 0 : code;
521 }
522 
523 static inline int asciiToKeycode(char a, int state)
524 {
525  if (a >= 'a' && a <= 'z')
526  a = toupper(a);
527  if ((state & Qt::ControlModifier) != 0) {
528  if (a >= 0 && a <= 31) // Ctrl+@..Ctrl+A..CTRL+Z..Ctrl+_
529  a += '@'; // to @..A..Z.._
530  }
531  return a & 0xff;
532 }
533 
534 static inline bool isModifierKey(int code)
535 {
536  return (code >= Qt::Key_Shift) && (code <= Qt::Key_ScrollLock);
537 }
538 // Key translation -----------------------------------------------------------------------[ end ]---
539 
540 
541 static void qt_show_system_menu(QWidget* tlw)
542 {
544  HMENU menu = GetSystemMenu(tlw->internalWinId(), FALSE);
545  if (!menu)
546  return; // no menu for this window
547 
548 #define enabled (MF_BYCOMMAND | MF_ENABLED)
549 #define disabled (MF_BYCOMMAND | MF_GRAYED)
550 
551 #ifndef Q_OS_WINCE
552  EnableMenuItem(menu, SC_MINIMIZE, (tlw->windowFlags() & Qt::WindowMinimizeButtonHint)?enabled:disabled);
553  bool maximized = IsZoomed(tlw->internalWinId());
554 
555  EnableMenuItem(menu, SC_MAXIMIZE, ! (tlw->windowFlags() & Qt::WindowMaximizeButtonHint) || maximized?disabled:enabled);
556  EnableMenuItem(menu, SC_RESTORE, maximized?enabled:disabled);
557 
558  // We should _not_ check with the setFixedSize(x,y) case here, since Windows is not able to check
559  // this and our menu here would be out-of-sync with the menu produced by mouse-click on the
560  // System Menu, or right-click on the title bar.
561  EnableMenuItem(menu, SC_SIZE, (tlw->windowFlags() & Qt::MSWindowsFixedSizeDialogHint) || maximized?disabled:enabled);
562  EnableMenuItem(menu, SC_MOVE, maximized?disabled:enabled);
563  EnableMenuItem(menu, SC_CLOSE, enabled);
564  // Set bold on close menu item
565  MENUITEMINFO closeItem;
566  closeItem.cbSize = sizeof(MENUITEMINFO);
567  closeItem.fMask = MIIM_STATE;
568  closeItem.fState = MFS_DEFAULT;
569  SetMenuItemInfo(menu, SC_CLOSE, FALSE, &closeItem);
570 #endif
571 
572 #undef enabled
573 #undef disabled
574  int ret = TrackPopupMenuEx(menu,
575  TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
576  tlw->geometry().x(), tlw->geometry().y(),
577  tlw->internalWinId(),
578  0);
579  if (ret)
580  QtWndProc(tlw->internalWinId(), WM_SYSCOMMAND, ret, 0);
581 }
582 
583 
584 // QETWidget class is only for accessing the sendSpontaneousEvent function in QApplication
585 class QETWidget : public QWidget {
586 public:
587  static bool sendSpontaneousEvent(QObject *r, QEvent *e)
588  { return QApplication::sendSpontaneousEvent(r, e); }
589 };
590 
591 
592 // Keyboard map private ----------------------------------------------------------------[ start ]---
593 
594 /*
595  \internal
596  A Windows KeyboardLayoutItem has 8 possible states:
597  1. Unmodified
598  2. Shift
599  3. Control
600  4. Control + Shift
601  5. Alt
602  6. Alt + Shift
603  7. Alt + Control
604  8. Alt + Control + Shift
605 */
606 struct KeyboardLayoutItem {
607  bool dirty;
609  quint32 qtKey[9]; // Can by any Qt::Key_<foo>, or unicode character
610 };
611 
613 {
614  memset(keyLayout, 0, sizeof(keyLayout));
615 }
616 
618 {
619  deleteLayouts();
620 }
621 
623 {
624  for (int i = 0; i < 255; ++i) {
625  if (keyLayout[i]) {
626  delete keyLayout[i];
627  keyLayout[i] = 0;
628  }
629  }
630 }
631 
633 {
634  deleteLayouts();
635 
636  /* MAKELCID()'s first argument is a WORD, and GetKeyboardLayout()
637  * returns a DWORD. */
638 
639  LCID newLCID = MAKELCID((quintptr)GetKeyboardLayout(0), SORT_DEFAULT);
640 // keyboardInputLocale = qt_localeFromLCID(newLCID);
641 
642  bool bidi = false;
643  wchar_t LCIDFontSig[16];
644  if (GetLocaleInfo(newLCID, LOCALE_FONTSIGNATURE, LCIDFontSig, sizeof(LCIDFontSig) / sizeof(wchar_t))
645  && (LCIDFontSig[7] & (wchar_t)0x0800))
646  bidi = true;
647 
648  keyboardInputDirection = bidi ? Qt::RightToLeft : Qt::LeftToRight;
649 }
650 
652 {
653  key_recorder.clearKeys();
654 }
655 
656 
657 inline void setKbdState(unsigned char *kbd, bool shift, bool ctrl, bool alt)
658 {
659  kbd[VK_LSHIFT ] = (shift ? 0x80 : 0);
660  kbd[VK_SHIFT ] = (shift ? 0x80 : 0);
661  kbd[VK_LCONTROL] = (ctrl ? 0x80 : 0);
662  kbd[VK_CONTROL ] = (ctrl ? 0x80 : 0);
663  kbd[VK_RMENU ] = (alt ? 0x80 : 0);
664  kbd[VK_MENU ] = (alt ? 0x80 : 0);
665 }
666 
667 void QKeyMapperPrivate::updateKeyMap(const MSG &msg)
668 {
669  unsigned char kbdBuffer[256]; // Will hold the complete keyboard state
670  GetKeyboardState(kbdBuffer);
671  const quint32 scancode = (msg.lParam >> 16) & scancodeBitmask;
672  updatePossibleKeyCodes(kbdBuffer, scancode, msg.wParam);
673 }
674 
675 void QKeyMapperPrivate::updatePossibleKeyCodes(unsigned char *kbdBuffer, quint32 scancode,
676  quint32 vk_key)
677 {
678  if (!vk_key || (keyLayout[vk_key] && !keyLayout[vk_key]->dirty))
679  return;
680 
681  if (!keyLayout[vk_key])
682  keyLayout[vk_key] = new KeyboardLayoutItem;
683 
684  // Copy keyboard state, so we can modify and query output for each possible permutation
685  unsigned char buffer[256];
686  memcpy(buffer, kbdBuffer, sizeof(buffer));
687  // Always 0, as Windows doesn't treat these as modifiers;
688  buffer[VK_LWIN ] = 0;
689  buffer[VK_RWIN ] = 0;
690  buffer[VK_CAPITAL ] = 0;
691  buffer[VK_NUMLOCK ] = 0;
692  buffer[VK_SCROLL ] = 0;
693  // Always 0, since we'll only change the other versions
694  buffer[VK_RSHIFT ] = 0;
695  buffer[VK_RCONTROL] = 0;
696  buffer[VK_LMENU ] = 0; // Use right Alt, since left Ctrl + right Alt is considered AltGraph
697 
698  bool isDeadKey = false;
699  keyLayout[vk_key]->deadkeys = 0;
700  keyLayout[vk_key]->dirty = false;
701  setKbdState(buffer, false, false, false);
702  keyLayout[vk_key]->qtKey[0] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
703  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x01 : 0;
704  setKbdState(buffer, true, false, false);
705  keyLayout[vk_key]->qtKey[1] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
706  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x02 : 0;
707  setKbdState(buffer, false, true, false);
708  keyLayout[vk_key]->qtKey[2] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
709  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x04 : 0;
710  setKbdState(buffer, true, true, false);
711  keyLayout[vk_key]->qtKey[3] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
712  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x08 : 0;
713  setKbdState(buffer, false, false, true);
714  keyLayout[vk_key]->qtKey[4] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
715  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x10 : 0;
716  setKbdState(buffer, true, false, true);
717  keyLayout[vk_key]->qtKey[5] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
718  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x20 : 0;
719  setKbdState(buffer, false, true, true);
720  keyLayout[vk_key]->qtKey[6] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
721  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x40 : 0;
722  setKbdState(buffer, true, true, true);
723  keyLayout[vk_key]->qtKey[7] = toKeyOrUnicode(vk_key, scancode, buffer, &isDeadKey);
724  keyLayout[vk_key]->deadkeys |= isDeadKey ? 0x80 : 0;
725  // Add a fall back key for layouts which don't do composition and show non-latin1 characters
726  int fallbackKey = winceKeyBend(vk_key);
727  if (!fallbackKey || fallbackKey == Qt::Key_unknown) {
728  fallbackKey = 0;
729  if (vk_key != keyLayout[vk_key]->qtKey[0] && vk_key < 0x5B && vk_key > 0x2F)
730  fallbackKey = vk_key;
731  }
732  keyLayout[vk_key]->qtKey[8] = fallbackKey;
733 
734  // If this vk_key makes a dead key with any combination of modifiers
735  if (keyLayout[vk_key]->deadkeys) {
736  // Push a Space, then the original key through the low-level ToAscii functions.
737  // We do this because these functions (ToAscii / ToUnicode) will alter the internal state of
738  // the keyboard driver By doing the following, we set the keyboard driver state back to what
739  // it was before we wrecked it with the code above.
740  // We need to push the space with an empty keystate map, since the driver checks the map for
741  // transitions in modifiers, so this helps us capture all possible deadkeys.
742  unsigned char emptyBuffer[256];
743  memset(emptyBuffer, 0, sizeof(emptyBuffer));
744  ::ToAscii(VK_SPACE, 0, emptyBuffer, reinterpret_cast<LPWORD>(&buffer), 0);
745  ::ToAscii(vk_key, scancode, kbdBuffer, reinterpret_cast<LPWORD>(&buffer), 0);
746  }
747 
748 #ifdef DEBUG_KEYMAPPER
749  qDebug("updatePossibleKeyCodes for virtual key = 0x%02x!", vk_key);
750  for (int i = 0; i < 9; ++i) {
751  qDebug(" [%d] (%d,0x%02x,'%c') %s", i,
752  keyLayout[vk_key]->qtKey[i],
753  keyLayout[vk_key]->qtKey[i],
754  keyLayout[vk_key]->qtKey[i] ? keyLayout[vk_key]->qtKey[i] : 0x03,
755  keyLayout[vk_key]->deadkeys & (1<<i) ? "deadkey" : "");
756  }
757 #endif // DEBUG_KEYMAPPER
758 }
759 
760 bool QKeyMapperPrivate::isADeadKey(unsigned int vk_key, unsigned int modifiers)
761 {
762  if (keyLayout && (vk_key < 256) && keyLayout[vk_key]) {
763  for(register int i = 0; i < 9; ++i) {
764  if (uint(ModsTbl[i]) == modifiers)
765  return bool(keyLayout[vk_key]->deadkeys & 1<<i);
766  }
767  }
768  return false;
769 }
770 
771 extern bool qt_use_rtl_extensions;
772 
774 {
775  QList<int> result;
776  const quint32 nativeVirtualKey = e->nativeVirtualKey();
777  if (nativeVirtualKey > 255)
778  return result;
779 
780  KeyboardLayoutItem *kbItem = keyLayout[nativeVirtualKey];
781  if(!kbItem)
782  return result;
783 
784  quint32 baseKey = kbItem->qtKey[0];
785  Qt::KeyboardModifiers keyMods = e->modifiers();
786  if (baseKey == Qt::Key_Return && (e->nativeModifiers() & ExtendedKey)) {
787  result << int(Qt::Key_Enter + keyMods);
788  return result;
789  }
790  result << int(baseKey + keyMods); // The base key is _always_ valid, of course
791 
792  for(int i = 1; i < 9; ++i) {
793  Qt::KeyboardModifiers neededMods = ModsTbl[i];
794  quint32 key = kbItem->qtKey[i];
795  if (key && key != baseKey && ((keyMods & neededMods) == neededMods))
796  result << int(key + (keyMods & ~neededMods));
797  }
798 
799  return result;
800 }
801 
802 bool QKeyMapperPrivate::translateKeyEvent(QWidget *widget, const MSG &msg, bool grab)
803 {
804  Q_Q(QKeyMapper);
805  Q_UNUSED(q); // Strange, but the compiler complains on q not being referenced, even if it is..
806  bool k0 = false;
807  bool k1 = false;
808  int msgType = msg.message;
809  // WM_(IME_)CHAR messages already contain the character in question so there is
810  // no need to fiddle with our key map. In any other case add this key to the
811  // keymap if it is not present yet.
812  if (msg.message != WM_CHAR && msg.message != WM_IME_CHAR)
813  updateKeyMap(msg);
814 
815  const quint32 scancode = (msg.lParam >> 16) & scancodeBitmask;
816  const quint32 vk_key = msg.wParam;
817 
818  quint32 nModifiers = 0;
819 
820 #if defined(Q_OS_WINCE)
821  nModifiers |= (GetKeyState(VK_SHIFT ) < 0 ? ShiftAny : 0);
822  nModifiers |= (GetKeyState(VK_CONTROL) < 0 ? ControlAny : 0);
823  nModifiers |= (GetKeyState(VK_MENU ) < 0 ? AltAny : 0);
824  nModifiers |= (GetKeyState(VK_LWIN ) < 0 ? MetaLeft : 0);
825  nModifiers |= (GetKeyState(VK_RWIN ) < 0 ? MetaRight : 0);
826 #else
827  // Map native modifiers to some bit representation
828  nModifiers |= (GetKeyState(VK_LSHIFT ) & 0x80 ? ShiftLeft : 0);
829  nModifiers |= (GetKeyState(VK_RSHIFT ) & 0x80 ? ShiftRight : 0);
830  nModifiers |= (GetKeyState(VK_LCONTROL) & 0x80 ? ControlLeft : 0);
831  nModifiers |= (GetKeyState(VK_RCONTROL) & 0x80 ? ControlRight : 0);
832  nModifiers |= (GetKeyState(VK_LMENU ) & 0x80 ? AltLeft : 0);
833  nModifiers |= (GetKeyState(VK_RMENU ) & 0x80 ? AltRight : 0);
834  nModifiers |= (GetKeyState(VK_LWIN ) & 0x80 ? MetaLeft : 0);
835  nModifiers |= (GetKeyState(VK_RWIN ) & 0x80 ? MetaRight : 0);
836  // Add Lock keys to the same bits
837  nModifiers |= (GetKeyState(VK_CAPITAL ) & 0x01 ? CapsLock : 0);
838  nModifiers |= (GetKeyState(VK_NUMLOCK ) & 0x01 ? NumLock : 0);
839  nModifiers |= (GetKeyState(VK_SCROLL ) & 0x01 ? ScrollLock : 0);
840 #endif // Q_OS_WINCE
841 
842  if (msg.lParam & ExtendedKey)
843  nModifiers |= msg.lParam & ExtendedKey;
844 
845  // Get the modifier states (may be altered later, depending on key code)
846  int state = 0;
847  state |= (nModifiers & ShiftAny ? int(Qt::ShiftModifier) : 0);
848  state |= (nModifiers & ControlAny ? int(Qt::ControlModifier) : 0);
849  state |= (nModifiers & AltAny ? int(Qt::AltModifier) : 0);
850  state |= (nModifiers & MetaAny ? int(Qt::MetaModifier) : 0);
851 
852  // A multi-character key not found by our look-ahead
853  if (msgType == WM_CHAR) {
854  QString s;
855  QChar ch = QChar((ushort)msg.wParam);
856  if (!ch.isNull())
857  s += ch;
858 
859  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
860  k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
861  }
862 
863  // Input method characters not found by our look-ahead
864  else if (msgType == WM_IME_CHAR) {
865  QString s;
866  QChar ch = QChar((ushort)msg.wParam);
867  if (!ch.isNull())
868  s += ch;
869 
870  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
871  k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, 0, Qt::KeyboardModifier(state), s, false, 0, scancode, vk_key, nModifiers);
872  }
873 
874  else {
875  // handle Directionality changes (BiDi) with RTL extensions
876  if (qt_use_rtl_extensions) {
877  static int dirStatus = 0;
878  if (!dirStatus && state == Qt::ControlModifier
879  && msg.wParam == VK_CONTROL
880  && msgType == WM_KEYDOWN) {
881  if (GetKeyState(VK_LCONTROL) < 0)
882  dirStatus = VK_LCONTROL;
883  else if (GetKeyState(VK_RCONTROL) < 0)
884  dirStatus = VK_RCONTROL;
885  } else if (dirStatus) {
886  if (msgType == WM_KEYDOWN) {
887  if (msg.wParam == VK_SHIFT) {
888  if (dirStatus == VK_LCONTROL && GetKeyState(VK_LSHIFT) < 0)
889  dirStatus = VK_LSHIFT;
890  else if (dirStatus == VK_RCONTROL && GetKeyState(VK_RSHIFT) < 0)
891  dirStatus = VK_RSHIFT;
892  } else {
893  dirStatus = 0;
894  }
895  } else if (msgType == WM_KEYUP) {
896  if (dirStatus == VK_LSHIFT
897  && ((msg.wParam == VK_SHIFT && GetKeyState(VK_LCONTROL))
898  || (msg.wParam == VK_CONTROL && GetKeyState(VK_LSHIFT)))) {
899  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, Qt::Key_Direction_L, 0,
900  QString(), false, 0,
901  scancode, msg.wParam, nModifiers);
902  k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, Qt::Key_Direction_L, 0,
903  QString(), false, 0,
904  scancode, msg.wParam, nModifiers);
905  dirStatus = 0;
906  } else if (dirStatus == VK_RSHIFT
907  && ( (msg.wParam == VK_SHIFT && GetKeyState(VK_RCONTROL))
908  || (msg.wParam == VK_CONTROL && GetKeyState(VK_RSHIFT)))) {
909  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, Qt::Key_Direction_R,
910  0, QString(), false, 0,
911  scancode, msg.wParam, nModifiers);
912  k1 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, Qt::Key_Direction_R,
913  0, QString(), false, 0,
914  scancode, msg.wParam, nModifiers);
915  dirStatus = 0;
916  } else {
917  dirStatus = 0;
918  }
919  } else {
920  dirStatus = 0;
921  }
922  }
923  }
924 
925  // IME will process these keys, so simply return
926  if(msg.wParam == VK_PROCESSKEY)
927  return true;
928 
929  // Ignore invalid virtual keycodes (see bugs 127424, QTBUG-3630)
930  if (msg.wParam == 0 || msg.wParam == 0xFF)
931  return true;
932 
933  // Translate VK_* (native) -> Key_* (Qt) keys
934  int modifiersIndex = 0;
935  modifiersIndex |= (nModifiers & ShiftAny ? 0x1 : 0);
936  modifiersIndex |= (nModifiers & ControlAny ? 0x2 : 0);
937  modifiersIndex |= (nModifiers & AltAny ? 0x4 : 0);
938 
939  int code = keyLayout[vk_key]->qtKey[modifiersIndex];
940 
941  // Invert state logic:
942  // If the key actually pressed is a modifier key, then we remove its modifier key from the
943  // state, since a modifier-key can't have itself as a modifier
944  if (code == Qt::Key_Control)
945  state = state ^ Qt::ControlModifier;
946  else if (code == Qt::Key_Shift)
947  state = state ^ Qt::ShiftModifier;
948  else if (code == Qt::Key_Alt)
949  state = state ^ Qt::AltModifier;
950 
951  // If the bit 24 of lParm is set you received a enter,
952  // otherwise a Return. (This is the extended key bit)
953  if ((code == Qt::Key_Return) && (msg.lParam & 0x1000000))
954  code = Qt::Key_Enter;
955 
956  // All cursor keys without extended bit
957  if (!(msg.lParam & 0x1000000)) {
958  switch (code) {
959  case Qt::Key_Left:
960  case Qt::Key_Right:
961  case Qt::Key_Up:
962  case Qt::Key_Down:
963  case Qt::Key_PageUp:
964  case Qt::Key_PageDown:
965  case Qt::Key_Home:
966  case Qt::Key_End:
967  case Qt::Key_Insert:
968  case Qt::Key_Delete:
969  case Qt::Key_Asterisk:
970  case Qt::Key_Plus:
971  case Qt::Key_Minus:
972  case Qt::Key_Period:
973  case Qt::Key_0:
974  case Qt::Key_1:
975  case Qt::Key_2:
976  case Qt::Key_3:
977  case Qt::Key_4:
978  case Qt::Key_5:
979  case Qt::Key_6:
980  case Qt::Key_7:
981  case Qt::Key_8:
982  case Qt::Key_9:
983  state |= ((msg.wParam >= '0' && msg.wParam <= '9')
984  || (msg.wParam >= VK_OEM_PLUS && msg.wParam <= VK_OEM_3))
985  ? 0 : int(Qt::KeypadModifier);
986  default:
987  if ((uint)msg.lParam == 0x004c0001 || (uint)msg.lParam == 0xc04c0001)
988  state |= Qt::KeypadModifier;
989  break;
990  }
991  }
992  // Other keys with with extended bit
993  else {
994  switch (code) {
995  case Qt::Key_Enter:
996  case Qt::Key_Slash:
997  case Qt::Key_NumLock:
998  state |= Qt::KeypadModifier;
999  default:
1000  break;
1001  }
1002  }
1003 
1004  // KEYDOWN ---------------------------------------------------------------------------------
1005  if (msgType == WM_KEYDOWN || msgType == WM_IME_KEYDOWN || msgType == WM_SYSKEYDOWN) {
1006  // Get the last record of this key press, so we can validate the current state
1007  // The record is not removed from the list
1008  KeyRecord *rec = key_recorder.findKey(msg.wParam, false);
1009 
1010  // If rec's state doesn't match the current state, something has changed behind our back
1011  // (Consumed by modal widget is one possibility) So, remove the record from the list
1012  // This will stop the auto-repeat of the key, should a modifier change, for example
1013  if (rec && rec->state != state) {
1014  key_recorder.findKey(msg.wParam, true);
1015  rec = 0;
1016  }
1017 
1018  // Find unicode character from Windows Message Queue
1019  MSG wm_char;
1020  UINT charType = (msgType == WM_KEYDOWN
1021  ? WM_CHAR
1022  : msgType == WM_IME_KEYDOWN ? WM_IME_CHAR : WM_SYSCHAR);
1023 
1024  QChar uch;
1025  if (PeekMessage(&wm_char, 0, charType, charType, PM_REMOVE)) {
1026  // Found a ?_CHAR
1027  uch = QChar((ushort)wm_char.wParam);
1028  if (msgType == WM_SYSKEYDOWN && uch.isLetter() && (msg.lParam & KF_ALTDOWN))
1029  uch = uch.toLower(); // (See doc of WM_SYSCHAR) Alt-letter
1030  if (!code && !uch.row())
1031  code = asciiToKeycode(uch.cell(), state);
1032  }
1033 
1034  // Special handling for the WM_IME_KEYDOWN message. Microsoft IME (Korean) will not
1035  // generate a WM_IME_CHAR message corresponding to this message. We might get wrong
1036  // results, if we map this virtual key-code directly (for eg '?' US layouts). So try
1037  // to find the correct key using the current message parameters & keyboard state.
1038  if (uch.isNull() && msgType == WM_IME_KEYDOWN) {
1039  BYTE keyState[256];
1040  wchar_t newKey[3] = {0};
1041  GetKeyboardState(keyState);
1042  int val = ToUnicode(vk_key, scancode, keyState, newKey, 2, 0);
1043  if (val == 1) {
1044  uch = QChar(newKey[0]);
1045  } else {
1046  // If we are still not able to find a unicode key, pass the WM_IME_KEYDOWN
1047  // message to DefWindowProc() for generating a proper WM_KEYDOWN.
1048  return false;
1049  }
1050  }
1051 
1052  // If no ?_CHAR was found in the queue; deduct character from the ?_KEYDOWN parameters
1053  if (uch.isNull()) {
1054  if (msg.wParam == VK_DELETE) {
1055  uch = QChar(QLatin1Char(0x7f)); // Windows doesn't know this one.
1056  } else {
1057  if (msgType != WM_SYSKEYDOWN || !code) {
1058  UINT map = MapVirtualKey(msg.wParam, 2);
1059  // If the high bit of the return value is set, it's a deadkey
1060  if (!(map & 0x80000000))
1061  uch = QChar((ushort)map);
1062  }
1063  }
1064  if (!code && !uch.row())
1065  code = asciiToKeycode(uch.cell(), state);
1066  }
1067 
1068  // Special handling of global Windows hotkeys
1069  if (state == Qt::AltModifier) {
1070  switch (code) {
1071  case Qt::Key_Escape:
1072  case Qt::Key_Tab:
1073  case Qt::Key_Enter:
1074  case Qt::Key_F4:
1075  return false; // Send the event on to Windows
1076  case Qt::Key_Space:
1077  // do not pass this key to windows, we will process it ourselves
1078  qt_show_system_menu(widget->window());
1079  return true;
1080  default:
1081  break;
1082  }
1083  }
1084 
1085  // Map SHIFT + Tab to SHIFT + BackTab, QShortcutMap knows about this translation
1086  if (code == Qt::Key_Tab && (state & Qt::ShiftModifier) == Qt::ShiftModifier)
1087  code = Qt::Key_Backtab;
1088 
1089  // If we have a record, it means that the key is already pressed, the state is the same
1090  // so, we have an auto-repeating key
1091  if (rec) {
1092  if (code < Qt::Key_Shift || code > Qt::Key_ScrollLock) {
1093  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, code,
1094  Qt::KeyboardModifier(state), rec->text, true, 0,
1095  scancode, msg.wParam, nModifiers);
1096  k1 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, code,
1097  Qt::KeyboardModifier(state), rec->text, true, 0,
1098  scancode, msg.wParam, nModifiers);
1099  }
1100  }
1101  // No record of the key being previous pressed, so we now send a QEvent::KeyPress event,
1102  // and store the key data into our records.
1103  else {
1104  QString text;
1105  if (!uch.isNull())
1106  text += uch;
1107  char a = uch.row() ? 0 : uch.cell();
1108  key_recorder.storeKey(msg.wParam, a, state, text);
1109  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, code, Qt::KeyboardModifier(state),
1110  text, false, 0, scancode, msg.wParam, nModifiers);
1111 
1112  bool store = true;
1113  // Alt+<alphanumerical> go to the Win32 menu system if unhandled by Qt
1114 #if !defined(Q_OS_WINCE)
1115  if (msgType == WM_SYSKEYDOWN && !k0 && a) {
1116  HWND parent = GetParent(widget->internalWinId());
1117  while (parent) {
1118  if (GetMenu(parent)) {
1119  SendMessage(parent, WM_SYSCOMMAND, SC_KEYMENU, a);
1120  store = false;
1121  k0 = true;
1122  break;
1123  }
1124  parent = GetParent(parent);
1125  }
1126  }
1127 #endif
1128  if (!store)
1129  key_recorder.findKey(msg.wParam, true);
1130  }
1131  }
1132 
1133  // KEYUP -----------------------------------------------------------------------------------
1134  else {
1135  // Try to locate the key in our records, and remove it if it exists.
1136  // The key may not be in our records if, for example, the down event was handled by
1137  // win32 natively, or our window gets focus while a key is already press, but now gets
1138  // the key release event.
1139  KeyRecord* rec = key_recorder.findKey(msg.wParam, true);
1140  if (!rec && !(code == Qt::Key_Shift
1141  || code == Qt::Key_Control
1142  || code == Qt::Key_Meta
1143  || code == Qt::Key_Alt)) {
1144  // Someone ate the key down event
1145  } else {
1146  if (!code)
1147  code = asciiToKeycode(rec->ascii ? rec->ascii : msg.wParam, state);
1148 
1149  // Map SHIFT + Tab to SHIFT + BackTab, QShortcutMap knows about this translation
1150  if (code == Qt::Key_Tab && (state & Qt::ShiftModifier) == Qt::ShiftModifier)
1151  code = Qt::Key_Backtab;
1152 
1153  k0 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, code, Qt::KeyboardModifier(state),
1154  (rec ? rec->text : QString()), false, 0, scancode, msg.wParam, nModifiers);
1155 
1156  // don't pass Alt to Windows unless we are embedded in a non-Qt window
1157 #if !defined(Q_OS_WINCE)
1158  if (code == Qt::Key_Alt) {
1159  k0 = true;
1160  HWND parent = GetParent(widget->internalWinId());
1161  while (parent) {
1162  if (!QWidget::find(parent) && GetMenu(parent)) {
1163  k0 = false;
1164  break;
1165  }
1166  parent = GetParent(parent);
1167  }
1168  }
1169 #endif
1170  }
1171  }
1172  }
1173 
1174  // Return true, if a QKeyEvent was sent to a widget
1175  return k0 || k1;
1176 }
1177 
1178 
1179 // QKeyMapper (Windows) implementation -------------------------------------------------[ start ]---
1180 
1181 bool QKeyMapper::sendKeyEvent(QWidget *widget, bool grab,
1182  QEvent::Type type, int code, Qt::KeyboardModifiers modifiers,
1183  const QString &text, bool autorepeat, int count,
1184  quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers,
1185  bool *)
1186 {
1187 #if defined(Q_OS_WINCE)
1188  Q_UNUSED(grab);
1189 #endif
1190  Q_UNUSED(count);
1191 #if defined QT3_SUPPORT && !defined(QT_NO_SHORTCUT)
1192  if (type == QEvent::KeyPress
1193  && !grab
1194  && QApplicationPrivate::instance()->use_compat()) {
1195  // send accel events if the keyboard is not grabbed
1196  QKeyEventEx a(type, code, modifiers,
1197  text, autorepeat, qMax(1, int(text.length())),
1198  nativeScanCode, nativeVirtualKey, nativeModifiers);
1199  if (QApplicationPrivate::instance()->qt_tryAccelEvent(widget, &a))
1200  return true;
1201  }
1202 #else
1203  Q_UNUSED(grab);
1204 #endif
1205  if (!widget->isEnabled())
1206  return false;
1207 
1208  QKeyEventEx e(type, code, modifiers,
1209  text, autorepeat, qMax(1, int(text.length())),
1210  nativeScanCode, nativeVirtualKey, nativeModifiers);
1211  QETWidget::sendSpontaneousEvent(widget, &e);
1212 
1213  if (!isModifierKey(code)
1214  && modifiers == Qt::AltModifier
1215  && ((code >= Qt::Key_A && code <= Qt::Key_Z) || (code >= Qt::Key_0 && code <= Qt::Key_9))
1216  && type == QEvent::KeyPress
1217  && !e.isAccepted())
1218  QApplication::beep(); // Emulate windows behavior
1219 
1220  return e.isAccepted();
1221 }
1222 
static const int QT_MAX_KEY_RECORDINGS
static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
Definition: qbezier.cpp:289
The QKeyEvent class describes a key event.
Definition: qevent.h:224
int type
Definition: qmetatype.cpp:239
static void beep()
Sounds the bell, using the default volume and sound.
bool isLetter() const
Returns true if the character is a letter (Letter_* categories); otherwise returns false...
Definition: qchar.cpp:653
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
#define disabled
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
#define BYTE
void updatePossibleKeyCodes(unsigned char *kbdBuffer, quint32 scancode, quint32 vk_key)
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
bool qt_use_rtl_extensions
KeyRecord * findKey(int code, bool remove)
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
KeyRecord(int c, int a, int s, const QString &t)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static bool sendSpontaneousEvent(QObject *r, QEvent *e)
#define VK_OEM_3
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
quint32 nativeModifiers() const
Returns the native modifiers of a key event.
Definition: qevent.cpp:906
Q_GUI_EXPORT int qt_translateKeyCode(int vk)
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
LRESULT QT_WIN_CALLBACK QtWndProc(HWND, UINT, WPARAM, LPARAM)
static QApplicationPrivate * instance()
long ASN1_INTEGER_get ASN1_INTEGER * a
unsigned char quint8
Definition: qglobal.h:934
KeyboardModifier
Definition: qnamespace.h:127
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isADeadKey(unsigned int vk_key, unsigned int modifiers)
void updateKeyMap(const MSG &msg)
void setKbdState(unsigned char *kbd, bool shift, bool ctrl, bool alt)
int ToUnicode(UINT vk, int, unsigned char *, LPWSTR unicodeBuffer, int, int)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static void qt_show_system_menu(QWidget *tlw)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define Q_Q(Class)
Definition: qglobal.h:2483
Q_CORE_EXPORT void qDebug(const char *,...)
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
Definition: qevent.cpp:999
QList< int > possibleKeys(QKeyEvent *e)
#define QT_WIN_CALLBACK
Definition: qglobal.h:1178
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static const uint KeyTbl[]
bool isAccepted() const
Definition: qcoreevent.h:307
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
static QWidget * find(WId)
Returns a pointer to the widget with window identifer/handle id.
Definition: qwidget.cpp:2517
int winceKeyBend(int keyCode)
static bool sendSpontaneousEvent(QObject *receiver, QEvent *event)
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
bool translateKeyEvent(QWidget *receiver, const MSG &msg, bool grab)
QChar toUpper() const
Returns the uppercase equivalent if the character is lowercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1287
static int asciiToKeycode(char a, int state)
void storeKey(int code, int ascii, int state, const QString &text)
bool isEnabled() const
Definition: qwidget.h:948
struct tagMSG MSG
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define VK_OEM_PLUS
KeyRecord deleted_record
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
Type
This enum type defines the valid event types in Qt.
Definition: qcoreevent.h:62
int ToAscii(UINT vk, int scancode, unsigned char *kbdBuffer, LPWORD unicodeBuffer, int flag)
static bool isModifierKey(int code)
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
unsigned short ushort
Definition: qglobal.h:995
#define store(x)
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
int key
unsigned int quint32
Definition: qglobal.h:938
uchar cell() const
Returns the cell (least significant byte) of the Unicode character.
Definition: qchar.h:283
#define KF_ALTDOWN
WId internalWinId() const
Returns the window system identifier of the widget, or 0 if the widget is not created yet...
Definition: qwidget.h:244
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
static bool sendKeyEvent(QWidget *widget, bool grab, QEvent::Type type, int code, Qt::KeyboardModifiers modifiers, const QString &text, bool autorepeat, int count, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, bool *unusedExceptForCocoa=0)
static const Qt::KeyboardModifiers ModsTbl[]
static int toKeyOrUnicode(int vk, int scancode, unsigned char *kbdBuffer, bool *isDeadkey=0)
static KeyRecorder key_recorder
Qt::WindowFlags windowFlags() const
Window flags are a combination of a type (e.
Definition: qwidget.h:939
static QString qtKey(CFStringRef cfkey)
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition: qwidget.h:158
QChar toLower() const
Returns the lowercase equivalent if the character is uppercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1239
uchar row() const
Returns the row (most significant byte) of the Unicode character.
Definition: qchar.h:284
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
quint32 nativeVirtualKey() const
Returns the native virtual key, or key sym of the key event.
Definition: qevent.cpp:889
#define enabled
bool GetKeyboardState(unsigned char *kbuffer)
Q_CORE_EXPORT QLocale qt_localeFromLCID(LCID id)