1/*
2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "PlatformKeyboardEvent.h"
30
31#include "NotImplemented.h"
32#include "WindowsKeyboardCodes.h"
33
34#include <QKeyEvent>
35#include <ctype.h>
36#include <wtf/CurrentTime.h>
37
38namespace WebCore {
39
40String keyIdentifierForQtKeyCode(int keyCode)
41{
42    switch (keyCode) {
43    case Qt::Key_Menu:
44    case Qt::Key_Alt:
45        return ASCIILiteral("Alt");
46    case Qt::Key_Clear:
47        return ASCIILiteral("Clear");
48    case Qt::Key_Down:
49        return ASCIILiteral("Down");
50    case Qt::Key_End:
51        return ASCIILiteral("End");
52    case Qt::Key_Return:
53    case Qt::Key_Enter:
54        return ASCIILiteral("Enter");
55    case Qt::Key_Execute:
56        return ASCIILiteral("Execute");
57    case Qt::Key_F1:
58        return ASCIILiteral("F1");
59    case Qt::Key_F2:
60        return ASCIILiteral("F2");
61    case Qt::Key_F3:
62        return ASCIILiteral("F3");
63    case Qt::Key_F4:
64        return ASCIILiteral("F4");
65    case Qt::Key_F5:
66        return ASCIILiteral("F5");
67    case Qt::Key_F6:
68        return ASCIILiteral("F6");
69    case Qt::Key_F7:
70        return ASCIILiteral("F7");
71    case Qt::Key_F8:
72        return ASCIILiteral("F8");
73    case Qt::Key_F9:
74        return ASCIILiteral("F9");
75    case Qt::Key_F10:
76        return ASCIILiteral("F10");
77    case Qt::Key_F11:
78        return ASCIILiteral("F11");
79    case Qt::Key_F12:
80        return ASCIILiteral("F12");
81    case Qt::Key_F13:
82        return ASCIILiteral("F13");
83    case Qt::Key_F14:
84        return ASCIILiteral("F14");
85    case Qt::Key_F15:
86        return ASCIILiteral("F15");
87    case Qt::Key_F16:
88        return ASCIILiteral("F16");
89    case Qt::Key_F17:
90        return ASCIILiteral("F17");
91    case Qt::Key_F18:
92        return ASCIILiteral("F18");
93    case Qt::Key_F19:
94        return ASCIILiteral("F19");
95    case Qt::Key_F20:
96        return ASCIILiteral("F20");
97    case Qt::Key_F21:
98        return ASCIILiteral("F21");
99    case Qt::Key_F22:
100        return ASCIILiteral("F22");
101    case Qt::Key_F23:
102        return ASCIILiteral("F23");
103    case Qt::Key_F24:
104        return ASCIILiteral("F24");
105    case Qt::Key_Help:
106        return ASCIILiteral("Help");
107    case Qt::Key_Home:
108        return ASCIILiteral("Home");
109    case Qt::Key_Insert:
110        return ASCIILiteral("Insert");
111    case Qt::Key_Left:
112        return ASCIILiteral("Left");
113    case Qt::Key_PageDown:
114        return ASCIILiteral("PageDown");
115    case Qt::Key_PageUp:
116        return ASCIILiteral("PageUp");
117    case Qt::Key_Pause:
118        return ASCIILiteral("Pause");
119    case Qt::Key_Print:
120        return ASCIILiteral("PrintScreen");
121    case Qt::Key_Right:
122        return ASCIILiteral("Right");
123    case Qt::Key_Select:
124        return ASCIILiteral("Select");
125    case Qt::Key_Up:
126        return ASCIILiteral("Up");
127        // Standard says that DEL becomes U+007F.
128    case Qt::Key_Delete:
129        return ASCIILiteral("U+007F");
130    case Qt::Key_Backspace:
131        return ASCIILiteral("U+0008");
132    case Qt::Key_Tab:
133        return ASCIILiteral("U+0009");
134    case Qt::Key_Backtab:
135        return ASCIILiteral("U+0009");
136    default:
137        return String::format("U+%04X", toupper(keyCode));
138    }
139}
140
141int windowsKeyCodeForKeyEvent(unsigned int keycode, bool isKeypad)
142{
143    // Determine wheter the event comes from the keypad
144    if (isKeypad) {
145        switch (keycode) {
146        case Qt::Key_0:
147            return VK_NUMPAD0; // (60) Numeric keypad 0 key
148        case Qt::Key_1:
149            return VK_NUMPAD1; // (61) Numeric keypad 1 key
150        case Qt::Key_2:
151            return  VK_NUMPAD2; // (62) Numeric keypad 2 key
152        case Qt::Key_3:
153            return VK_NUMPAD3; // (63) Numeric keypad 3 key
154        case Qt::Key_4:
155            return VK_NUMPAD4; // (64) Numeric keypad 4 key
156        case Qt::Key_5:
157            return VK_NUMPAD5; // (65) Numeric keypad 5 key
158        case Qt::Key_6:
159            return VK_NUMPAD6; // (66) Numeric keypad 6 key
160        case Qt::Key_7:
161            return VK_NUMPAD7; // (67) Numeric keypad 7 key
162        case Qt::Key_8:
163            return VK_NUMPAD8; // (68) Numeric keypad 8 key
164        case Qt::Key_9:
165            return VK_NUMPAD9; // (69) Numeric keypad 9 key
166        case Qt::Key_Asterisk:
167            return VK_MULTIPLY; // (6A) Multiply key
168        case Qt::Key_Plus:
169            return VK_ADD; // (6B) Add key
170        case Qt::Key_Minus:
171            return VK_SUBTRACT; // (6D) Subtract key
172        case Qt::Key_Period:
173            return VK_DECIMAL; // (6E) Decimal key
174        case Qt::Key_Slash:
175            return VK_DIVIDE; // (6F) Divide key
176        case Qt::Key_PageUp:
177            return VK_PRIOR; // (21) PAGE UP key
178        case Qt::Key_PageDown:
179            return VK_NEXT; // (22) PAGE DOWN key
180        case Qt::Key_End:
181            return VK_END; // (23) END key
182        case Qt::Key_Home:
183            return VK_HOME; // (24) HOME key
184        case Qt::Key_Left:
185            return VK_LEFT; // (25) LEFT ARROW key
186        case Qt::Key_Up:
187            return VK_UP; // (26) UP ARROW key
188        case Qt::Key_Right:
189            return VK_RIGHT; // (27) RIGHT ARROW key
190        case Qt::Key_Down:
191            return VK_DOWN; // (28) DOWN ARROW key
192        case Qt::Key_Enter:
193        case Qt::Key_Return:
194            return VK_RETURN; // (0D) Return key
195        case Qt::Key_Insert:
196            return VK_INSERT; // (2D) INS key
197        case Qt::Key_Delete:
198            return VK_DELETE; // (2E) DEL key
199        default:
200            return 0;
201        }
202
203    } else
204
205    switch (keycode) {
206    case Qt::Key_Backspace:
207        return VK_BACK; // (08) BACKSPACE key
208    case Qt::Key_Backtab:
209    case Qt::Key_Tab:
210        return VK_TAB; // (09) TAB key
211    case Qt::Key_Clear:
212        return VK_CLEAR; // (0C) CLEAR key
213    case Qt::Key_Enter:
214    case Qt::Key_Return:
215        return VK_RETURN; // (0D) Return key
216    case Qt::Key_Shift:
217        return VK_SHIFT; // (10) SHIFT key
218    case Qt::Key_Control:
219        return VK_CONTROL; // (11) CTRL key
220    case Qt::Key_Menu:
221    case Qt::Key_Alt:
222        return VK_MENU; // (12) ALT key
223
224    case Qt::Key_F1:
225        return VK_F1;
226    case Qt::Key_F2:
227        return VK_F2;
228    case Qt::Key_F3:
229        return VK_F3;
230    case Qt::Key_F4:
231        return VK_F4;
232    case Qt::Key_F5:
233        return VK_F5;
234    case Qt::Key_F6:
235        return VK_F6;
236    case Qt::Key_F7:
237        return VK_F7;
238    case Qt::Key_F8:
239        return VK_F8;
240    case Qt::Key_F9:
241        return VK_F9;
242    case Qt::Key_F10:
243        return VK_F10;
244    case Qt::Key_F11:
245        return VK_F11;
246    case Qt::Key_F12:
247        return VK_F12;
248    case Qt::Key_F13:
249        return VK_F13;
250    case Qt::Key_F14:
251        return VK_F14;
252    case Qt::Key_F15:
253        return VK_F15;
254    case Qt::Key_F16:
255        return VK_F16;
256    case Qt::Key_F17:
257        return VK_F17;
258    case Qt::Key_F18:
259        return VK_F18;
260    case Qt::Key_F19:
261        return VK_F19;
262    case Qt::Key_F20:
263        return VK_F20;
264    case Qt::Key_F21:
265        return VK_F21;
266    case Qt::Key_F22:
267        return VK_F22;
268    case Qt::Key_F23:
269        return VK_F23;
270    case Qt::Key_F24:
271        return VK_F24;
272
273    case Qt::Key_Pause:
274        return VK_PAUSE; // (13) PAUSE key
275    case Qt::Key_CapsLock:
276        return VK_CAPITAL; // (14) CAPS LOCK key
277    case Qt::Key_Kana_Lock:
278    case Qt::Key_Kana_Shift:
279        return VK_KANA; // (15) Input Method Editor (IME) Kana mode
280    case Qt::Key_Hangul:
281        return VK_HANGUL; // VK_HANGUL (15) IME Hangul mode
282        // VK_JUNJA (17) IME Junja mode
283        // VK_FINAL (18) IME final mode
284    case Qt::Key_Hangul_Hanja:
285        return VK_HANJA; // (19) IME Hanja mode
286    case Qt::Key_Kanji:
287        return VK_KANJI; // (19) IME Kanji mode
288    case Qt::Key_Escape:
289        return VK_ESCAPE; // (1B) ESC key
290        // VK_CONVERT (1C) IME convert
291        // VK_NONCONVERT (1D) IME nonconvert
292        // VK_ACCEPT (1E) IME accept
293        // VK_MODECHANGE (1F) IME mode change request
294    case Qt::Key_Space:
295        return VK_SPACE; // (20) SPACEBAR
296    case Qt::Key_PageUp:
297        return VK_PRIOR; // (21) PAGE UP key
298    case Qt::Key_PageDown:
299        return VK_NEXT; // (22) PAGE DOWN key
300    case Qt::Key_End:
301        return VK_END; // (23) END key
302    case Qt::Key_Home:
303        return VK_HOME; // (24) HOME key
304    case Qt::Key_Left:
305        return VK_LEFT; // (25) LEFT ARROW key
306    case Qt::Key_Up:
307        return VK_UP; // (26) UP ARROW key
308    case Qt::Key_Right:
309        return VK_RIGHT; // (27) RIGHT ARROW key
310    case Qt::Key_Down:
311        return VK_DOWN; // (28) DOWN ARROW key
312    case Qt::Key_Select:
313        return VK_SELECT; // (29) SELECT key
314    case Qt::Key_Print:
315        return VK_SNAPSHOT; // (2A) PRINT key
316    case Qt::Key_Execute:
317        return VK_EXECUTE; // (2B) EXECUTE key
318    case Qt::Key_Insert:
319        return VK_INSERT; // (2D) INS key
320    case Qt::Key_Delete:
321        return VK_DELETE; // (2E) DEL key
322    case Qt::Key_Help:
323        return VK_HELP; // (2F) HELP key
324    case Qt::Key_0:
325    case Qt::Key_ParenLeft:
326        return VK_0; // (30) 0) key
327    case Qt::Key_1:
328        return VK_1; // (31) 1 ! key
329    case Qt::Key_2:
330    case Qt::Key_At:
331        return VK_2; // (32) 2 & key
332    case Qt::Key_3:
333    case Qt::Key_NumberSign:
334        return VK_3; // case '3': case '#';
335    case Qt::Key_4:
336    case Qt::Key_Dollar: // (34) 4 key '$';
337        return VK_4;
338    case Qt::Key_5:
339    case Qt::Key_Percent:
340        return VK_5; // (35) 5 key  '%'
341    case Qt::Key_6:
342    case Qt::Key_AsciiCircum:
343        return VK_6; // (36) 6 key  '^'
344    case Qt::Key_7:
345    case Qt::Key_Ampersand:
346        return VK_7; // (37) 7 key  case '&'
347    case Qt::Key_8:
348    case Qt::Key_Asterisk:
349        return VK_8; // (38) 8 key  '*'
350    case Qt::Key_9:
351    case Qt::Key_ParenRight:
352        return VK_9; // (39) 9 key '('
353    case Qt::Key_A:
354        return VK_A; // (41) A key case 'a': case 'A': return 0x41;
355    case Qt::Key_B:
356        return VK_B; // (42) B key case 'b': case 'B': return 0x42;
357    case Qt::Key_C:
358        return VK_C; // (43) C key case 'c': case 'C': return 0x43;
359    case Qt::Key_D:
360        return VK_D; // (44) D key case 'd': case 'D': return 0x44;
361    case Qt::Key_E:
362        return VK_E; // (45) E key case 'e': case 'E': return 0x45;
363    case Qt::Key_F:
364        return VK_F; // (46) F key case 'f': case 'F': return 0x46;
365    case Qt::Key_G:
366        return VK_G; // (47) G key case 'g': case 'G': return 0x47;
367    case Qt::Key_H:
368        return VK_H; // (48) H key case 'h': case 'H': return 0x48;
369    case Qt::Key_I:
370        return VK_I; // (49) I key case 'i': case 'I': return 0x49;
371    case Qt::Key_J:
372        return VK_J; // (4A) J key case 'j': case 'J': return 0x4A;
373    case Qt::Key_K:
374        return VK_K; // (4B) K key case 'k': case 'K': return 0x4B;
375    case Qt::Key_L:
376        return VK_L; // (4C) L key case 'l': case 'L': return 0x4C;
377    case Qt::Key_M:
378        return VK_M; // (4D) M key case 'm': case 'M': return 0x4D;
379    case Qt::Key_N:
380        return VK_N; // (4E) N key case 'n': case 'N': return 0x4E;
381    case Qt::Key_O:
382        return VK_O; // (4F) O key case 'o': case 'O': return 0x4F;
383    case Qt::Key_P:
384        return VK_P; // (50) P key case 'p': case 'P': return 0x50;
385    case Qt::Key_Q:
386        return VK_Q; // (51) Q key case 'q': case 'Q': return 0x51;
387    case Qt::Key_R:
388        return VK_R; // (52) R key case 'r': case 'R': return 0x52;
389    case Qt::Key_S:
390        return VK_S; // (53) S key case 's': case 'S': return 0x53;
391    case Qt::Key_T:
392        return VK_T; // (54) T key case 't': case 'T': return 0x54;
393    case Qt::Key_U:
394        return VK_U; // (55) U key case 'u': case 'U': return 0x55;
395    case Qt::Key_V:
396        return VK_V; // (56) V key case 'v': case 'V': return 0x56;
397    case Qt::Key_W:
398        return VK_W; // (57) W key case 'w': case 'W': return 0x57;
399    case Qt::Key_X:
400        return VK_X; // (58) X key case 'x': case 'X': return 0x58;
401    case Qt::Key_Y:
402        return VK_Y; // (59) Y key case 'y': case 'Y': return 0x59;
403    case Qt::Key_Z:
404        return VK_Z; // (5A) Z key case 'z': case 'Z': return 0x5A;
405    case Qt::Key_Meta:
406        return VK_LWIN; // (5B) Left Windows key (Microsoft Natural keyboard)
407        // case Qt::Key_Meta_R: FIXME: What to do here?
408        //    return VK_RWIN; // (5C) Right Windows key (Natural keyboard)
409        // VK_APPS (5D) Applications key (Natural keyboard)
410        // VK_SLEEP (5F) Computer Sleep key
411        // VK_SEPARATOR (6C) Separator key
412        // VK_SUBTRACT (6D) Subtract key
413        // VK_DECIMAL (6E) Decimal key
414        // VK_DIVIDE (6F) Divide key
415        // handled by key code above
416
417    case Qt::Key_NumLock:
418        return VK_NUMLOCK; // (90) NUM LOCK key
419
420    case Qt::Key_ScrollLock:
421        return VK_SCROLL; // (91) SCROLL LOCK key
422
423        // VK_LSHIFT (A0) Left SHIFT key
424        // VK_RSHIFT (A1) Right SHIFT key
425        // VK_LCONTROL (A2) Left CONTROL key
426        // VK_RCONTROL (A3) Right CONTROL key
427        // VK_LMENU (A4) Left MENU key
428        // VK_RMENU (A5) Right MENU key
429        // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
430        // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
431        // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
432        // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
433        // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
434        // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
435        // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
436
437    case Qt::Key_VolumeMute:
438        return VK_VOLUME_MUTE; // (AD) Windows 2000/XP: Volume Mute key
439    case Qt::Key_VolumeDown:
440        return VK_VOLUME_DOWN; // (AE) Windows 2000/XP: Volume Down key
441    case Qt::Key_VolumeUp:
442        return VK_VOLUME_UP; // (AF) Windows 2000/XP: Volume Up key
443    case Qt::Key_MediaNext:
444        return VK_MEDIA_NEXT_TRACK; // (B0) Windows 2000/XP: Next Track key
445    case Qt::Key_MediaPrevious:
446        return VK_MEDIA_PREV_TRACK; // (B1) Windows 2000/XP: Previous Track key
447    case Qt::Key_MediaStop:
448        return VK_MEDIA_STOP; // (B2) Windows 2000/XP: Stop Media key
449    case Qt::Key_MediaTogglePlayPause:
450        return VK_MEDIA_PLAY_PAUSE; // (B3) Windows 2000/XP: Play/Pause Media key
451
452        // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
453        // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
454        // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
455        // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
456
457        // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
458    case Qt::Key_Semicolon:
459    case Qt::Key_Colon:
460        return VK_OEM_1; // case ';': case ':': return 0xBA;
461        // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
462    case Qt::Key_Plus:
463    case Qt::Key_Equal:
464        return VK_OEM_PLUS; // case '=': case '+': return 0xBB;
465        // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
466    case Qt::Key_Comma:
467    case Qt::Key_Less:
468        return VK_OEM_COMMA; // case ',': case '<': return 0xBC;
469        // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
470    case Qt::Key_Minus:
471    case Qt::Key_Underscore:
472        return VK_OEM_MINUS; // case '-': case '_': return 0xBD;
473        // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
474    case Qt::Key_Period:
475    case Qt::Key_Greater:
476        return VK_OEM_PERIOD; // case '.': case '>': return 0xBE;
477        // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
478    case Qt::Key_Slash:
479    case Qt::Key_Question:
480        return VK_OEM_2; // case '/': case '?': return 0xBF;
481        // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
482    case Qt::Key_AsciiTilde:
483    case Qt::Key_QuoteLeft:
484        return VK_OEM_3; // case '`': case '~': return 0xC0;
485        // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
486    case Qt::Key_BracketLeft:
487    case Qt::Key_BraceLeft:
488        return VK_OEM_4; // case '[': case '{': return 0xDB;
489        // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
490    case Qt::Key_Backslash:
491    case Qt::Key_Bar:
492        return VK_OEM_5; // case '\\': case '|': return 0xDC;
493        // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
494    case Qt::Key_BracketRight:
495    case Qt::Key_BraceRight:
496        return VK_OEM_6; // case ']': case '}': return 0xDD;
497        // VK_OEM_7 (DE) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key
498    case Qt::Key_QuoteDbl:
499        return VK_OEM_7; // case '\'': case '"': return 0xDE;
500        // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
501        // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
502
503    case Qt::Key_AudioRewind:
504        return 0xE3; // (E3) Android/GoogleTV: Rewind media key (Windows: VK_ICO_HELP Help key on 1984 Olivetti M24 deluxe keyboard)
505    case Qt::Key_AudioForward:
506        return 0xE4; // (E4) Android/GoogleTV: Fast forward media key  (Windows: VK_ICO_00 '00' key on 1984 Olivetti M24 deluxe keyboard)
507
508        // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
509        // VK_PACKET (E7) Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT,SendInput, WM_KEYDOWN, and WM_KEYUP
510        // VK_ATTN (F6) Attn key
511        // VK_CRSEL (F7) CrSel key
512        // VK_EXSEL (F8) ExSel key
513        // VK_EREOF (F9) Erase EOF key
514        // VK_PLAY (FA) Play key
515        // VK_ZOOM (FB) Zoom key
516        // VK_NONAME (FC) Reserved for future use
517        // VK_PA1 (FD) PA1 key
518        // VK_OEM_CLEAR (FE) Clear key
519    default:
520        return 0;
521    }
522}
523
524static bool isVirtualKeyCodeRepresentingCharacter(int code)
525{
526    switch (code) {
527    case VK_SPACE:
528    case VK_0:
529    case VK_1:
530    case VK_2:
531    case VK_3:
532    case VK_4:
533    case VK_5:
534    case VK_6:
535    case VK_7:
536    case VK_8:
537    case VK_9:
538    case VK_A:
539    case VK_B:
540    case VK_C:
541    case VK_D:
542    case VK_E:
543    case VK_F:
544    case VK_G:
545    case VK_H:
546    case VK_I:
547    case VK_J:
548    case VK_K:
549    case VK_L:
550    case VK_M:
551    case VK_N:
552    case VK_O:
553    case VK_P:
554    case VK_Q:
555    case VK_R:
556    case VK_S:
557    case VK_T:
558    case VK_U:
559    case VK_V:
560    case VK_W:
561    case VK_X:
562    case VK_Y:
563    case VK_Z:
564    case VK_NUMPAD0:
565    case VK_NUMPAD1:
566    case VK_NUMPAD2:
567    case VK_NUMPAD3:
568    case VK_NUMPAD4:
569    case VK_NUMPAD5:
570    case VK_NUMPAD6:
571    case VK_NUMPAD7:
572    case VK_NUMPAD8:
573    case VK_NUMPAD9:
574    case VK_MULTIPLY:
575    case VK_ADD:
576    case VK_SEPARATOR:
577    case VK_SUBTRACT:
578    case VK_DECIMAL:
579    case VK_DIVIDE:
580    case VK_OEM_1:
581    case VK_OEM_PLUS:
582    case VK_OEM_COMMA:
583    case VK_OEM_MINUS:
584    case VK_OEM_PERIOD:
585    case VK_OEM_2:
586    case VK_OEM_3:
587    case VK_OEM_4:
588    case VK_OEM_5:
589    case VK_OEM_6:
590    case VK_OEM_7:
591        return true;
592    default:
593        return false;
594    }
595}
596
597static String keyTextForKeyEvent(const QKeyEvent* event)
598{
599    switch (event->key()) {
600    case Qt::Key_Tab:
601    case Qt::Key_Backtab:
602        if (event->text().isNull())
603            return ASCIILiteral("\t");
604        break;
605    case Qt::Key_Return:
606    case Qt::Key_Enter:
607        if (event->text().isNull())
608            return ASCIILiteral("\r");
609    }
610    return event->text();
611}
612
613PlatformKeyboardEvent::PlatformKeyboardEvent(QKeyEvent* event)
614{
615    const int state = event->modifiers();
616    m_type = (event->type() == QEvent::KeyRelease) ? PlatformEvent::KeyUp : PlatformEvent::KeyDown;
617
618    m_modifiers = 0;
619    if ((state & Qt::ShiftModifier) || event->key() == Qt::Key_Backtab) // Simulate Shift+Tab with Key_Backtab
620        m_modifiers |= ShiftKey;
621    if (state & Qt::ControlModifier)
622        m_modifiers |= CtrlKey;
623    if (state & Qt::AltModifier)
624        m_modifiers |= AltKey;
625    if (state & Qt::MetaModifier)
626        m_modifiers |= MetaKey;
627
628    m_text = keyTextForKeyEvent(event);
629    m_unmodifiedText = m_text; // FIXME: not correct
630    m_keyIdentifier = keyIdentifierForQtKeyCode(event->key());
631    m_autoRepeat = event->isAutoRepeat();
632    m_isKeypad = (state & Qt::KeypadModifier);
633    m_isSystemKey = false;
634    m_windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(event->key(), m_isKeypad);
635    m_nativeVirtualKeyCode = event->nativeVirtualKey();
636    m_macCharCode = 0;
637    m_qtEvent = event;
638    m_timestamp = WTF::currentTime();
639}
640
641void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool)
642{
643    // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
644    ASSERT(m_type == PlatformEvent::KeyDown);
645    m_type = type;
646
647    if (type == PlatformEvent::RawKeyDown) {
648        m_text = String();
649        m_unmodifiedText = String();
650    } else {
651        /*
652            When we receive shortcut events like Ctrl+V then the text in the QKeyEvent is
653            empty. If we're asked to disambiguate the event into a Char keyboard event,
654            we try to detect this situation and still set the text, to ensure that the
655            general event handling sends a key press event after this disambiguation.
656        */
657        if (m_text.isEmpty() && m_windowsVirtualKeyCode && isVirtualKeyCodeRepresentingCharacter(m_windowsVirtualKeyCode))
658            m_text.append(UChar(m_windowsVirtualKeyCode));
659
660        m_keyIdentifier = String();
661        m_windowsVirtualKeyCode = 0;
662    }
663}
664
665bool PlatformKeyboardEvent::currentCapsLockState()
666{
667    notImplemented();
668    return false;
669}
670
671void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
672{
673    notImplemented();
674    shiftKey = false;
675    ctrlKey = false;
676    altKey = false;
677    metaKey = false;
678}
679
680uint32_t PlatformKeyboardEvent::nativeModifiers() const
681{
682    ASSERT(m_qtEvent);
683    return m_qtEvent->nativeModifiers();
684}
685
686uint32_t PlatformKeyboardEvent::nativeScanCode() const
687{
688    ASSERT(m_qtEvent);
689    return m_qtEvent->nativeScanCode();
690}
691
692}
693
694// vim: ts=4 sw=4 et
695