2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include "PlatformKeyboardEvent.h"
31 #include "KeyboardCodes.h"
32 #include "NotImplemented.h"
38 static String keyIdentifierForQtKeyCode(int keyCode)
53 #if QT_VERSION >= 0x040200
113 case Qt::Key_PageDown:
120 return "PrintScreen";
127 // Standard says that DEL becomes U+007F.
132 case Qt::Key_Backtab:
135 return String::format("U+%04X", toupper(keyCode));
139 static int windowsKeyCodeForKeyEvent(unsigned int keycode)
142 /* FIXME: Need to supply a bool in this func, to determine wheter the event comes from the keypad
144 return VK_NUMPAD0;// (60) Numeric keypad 0 key
146 return VK_NUMPAD1;// (61) Numeric keypad 1 key
148 return VK_NUMPAD2; // (62) Numeric keypad 2 key
150 return VK_NUMPAD3; // (63) Numeric keypad 3 key
152 return VK_NUMPAD4; // (64) Numeric keypad 4 key
154 return VK_NUMPAD5; //(65) Numeric keypad 5 key
156 return VK_NUMPAD6; // (66) Numeric keypad 6 key
158 return VK_NUMPAD7; // (67) Numeric keypad 7 key
160 return VK_NUMPAD8; // (68) Numeric keypad 8 key
162 return VK_NUMPAD9; // (69) Numeric keypad 9 key
163 case Qt::Key_Asterisk:
164 return VK_MULTIPLY; // (6A) Multiply key
166 return VK_ADD; // (6B) Add key
168 return VK_SUBTRACT; // (6D) Subtract key
170 return VK_DECIMAL; // (6E) Decimal key
172 return VK_DIVIDE; // (6F) Divide key
175 case Qt::Key_Backspace:
176 return VK_BACK; // (08) BACKSPACE key
177 case Qt::Key_Backtab:
179 return VK_TAB; // (09) TAB key
181 return VK_CLEAR; // (0C) CLEAR key
184 return VK_RETURN; //(0D) Return key
186 return VK_SHIFT; // (10) SHIFT key
187 case Qt::Key_Control:
188 return VK_CONTROL; // (11) CTRL key
191 return VK_MENU; // (12) ALT key
194 return VK_PAUSE; // (13) PAUSE key
195 case Qt::Key_CapsLock:
196 return VK_CAPITAL; // (14) CAPS LOCK key
197 case Qt::Key_Kana_Lock:
198 case Qt::Key_Kana_Shift:
199 return VK_KANA; // (15) Input Method Editor (IME) Kana mode
201 return VK_HANGUL; // VK_HANGUL (15) IME Hangul mode
202 // VK_JUNJA (17) IME Junja mode
203 // VK_FINAL (18) IME final mode
204 case Qt::Key_Hangul_Hanja:
205 return VK_HANJA; // (19) IME Hanja mode
207 return VK_KANJI; // (19) IME Kanji mode
209 return VK_ESCAPE; // (1B) ESC key
210 // VK_CONVERT (1C) IME convert
211 // VK_NONCONVERT (1D) IME nonconvert
212 // VK_ACCEPT (1E) IME accept
213 // VK_MODECHANGE (1F) IME mode change request
215 return VK_SPACE; // (20) SPACEBAR
217 return VK_PRIOR; // (21) PAGE UP key
218 case Qt::Key_PageDown:
219 return VK_NEXT; // (22) PAGE DOWN key
221 return VK_END; // (23) END key
223 return VK_HOME; // (24) HOME key
225 return VK_LEFT; // (25) LEFT ARROW key
227 return VK_UP; // (26) UP ARROW key
229 return VK_RIGHT; // (27) RIGHT ARROW key
231 return VK_DOWN; // (28) DOWN ARROW key
233 return VK_SELECT; // (29) SELECT key
235 return VK_PRINT; // (2A) PRINT key
236 #if QT_VERSION >= 0x040200
237 case Qt::Key_Execute:
238 return VK_EXECUTE;// (2B) EXECUTE key
241 //case Qt::Key_PrintScreen:
242 // return VK_SNAPSHOT; // (2C) PRINT SCREEN key
244 return VK_INSERT; // (2D) INS key
246 return VK_DELETE; // (2E) DEL key
248 return VK_HELP; // (2F) HELP key
250 case Qt::Key_ParenLeft:
251 return VK_0; // (30) 0) key
253 return VK_1; // (31) 1 ! key
256 return VK_2; // (32) 2 & key
258 case Qt::Key_NumberSign:
259 return VK_3; //case '3': case '#';
261 case Qt::Key_Dollar: // (34) 4 key '$';
264 case Qt::Key_Percent:
265 return VK_5; // (35) 5 key '%'
267 case Qt::Key_AsciiCircum:
268 return VK_6; // (36) 6 key '^'
270 case Qt::Key_Ampersand:
271 return VK_7; // (37) 7 key case '&'
273 case Qt::Key_Asterisk:
274 return VK_8; // (38) 8 key '*'
276 case Qt::Key_ParenRight:
277 return VK_9; // (39) 9 key '('
279 return VK_A; // (41) A key case 'a': case 'A': return 0x41;
281 return VK_B; // (42) B key case 'b': case 'B': return 0x42;
283 return VK_C; // (43) C key case 'c': case 'C': return 0x43;
285 return VK_D; // (44) D key case 'd': case 'D': return 0x44;
287 return VK_E; // (45) E key case 'e': case 'E': return 0x45;
289 return VK_F; // (46) F key case 'f': case 'F': return 0x46;
291 return VK_G; // (47) G key case 'g': case 'G': return 0x47;
293 return VK_H; // (48) H key case 'h': case 'H': return 0x48;
295 return VK_I; // (49) I key case 'i': case 'I': return 0x49;
297 return VK_J; // (4A) J key case 'j': case 'J': return 0x4A;
299 return VK_K; // (4B) K key case 'k': case 'K': return 0x4B;
301 return VK_L; // (4C) L key case 'l': case 'L': return 0x4C;
303 return VK_M; // (4D) M key case 'm': case 'M': return 0x4D;
305 return VK_N; // (4E) N key case 'n': case 'N': return 0x4E;
307 return VK_O; // (4F) O key case 'o': case 'O': return 0x4F;
309 return VK_P; // (50) P key case 'p': case 'P': return 0x50;
311 return VK_Q; // (51) Q key case 'q': case 'Q': return 0x51;
313 return VK_R; // (52) R key case 'r': case 'R': return 0x52;
315 return VK_S; // (53) S key case 's': case 'S': return 0x53;
317 return VK_T; // (54) T key case 't': case 'T': return 0x54;
319 return VK_U; // (55) U key case 'u': case 'U': return 0x55;
321 return VK_V; // (56) V key case 'v': case 'V': return 0x56;
323 return VK_W; // (57) W key case 'w': case 'W': return 0x57;
325 return VK_X; // (58) X key case 'x': case 'X': return 0x58;
327 return VK_Y; // (59) Y key case 'y': case 'Y': return 0x59;
329 return VK_Z; // (5A) Z key case 'z': case 'Z': return 0x5A;
331 return VK_LWIN; // (5B) Left Windows key (Microsoft Natural keyboard)
332 //case Qt::Key_Meta_R: FIXME: What to do here?
333 // return VK_RWIN; // (5C) Right Windows key (Natural keyboard)
334 // VK_APPS (5D) Applications key (Natural keyboard)
335 // VK_SLEEP (5F) Computer Sleep key
336 // VK_SEPARATOR (6C) Separator key
337 // VK_SUBTRACT (6D) Subtract key
338 // VK_DECIMAL (6E) Decimal key
339 // VK_DIVIDE (6F) Divide key
340 // handled by key code above
342 case Qt::Key_NumLock:
343 return VK_NUMLOCK; // (90) NUM LOCK key
345 case Qt::Key_ScrollLock:
346 return VK_SCROLL; // (91) SCROLL LOCK key
348 // VK_LSHIFT (A0) Left SHIFT key
349 // VK_RSHIFT (A1) Right SHIFT key
350 // VK_LCONTROL (A2) Left CONTROL key
351 // VK_RCONTROL (A3) Right CONTROL key
352 // VK_LMENU (A4) Left MENU key
353 // VK_RMENU (A5) Right MENU key
354 // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
355 // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
356 // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
357 // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
358 // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
359 // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
360 // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
361 // VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key
362 // VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key
363 // VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key
364 // VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key
365 // VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key
366 // VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key
367 // VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key
368 // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
369 // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
370 // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
371 // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
373 // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
374 case Qt::Key_Semicolon:
376 return VK_OEM_1; //case ';': case ':': return 0xBA;
377 // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
380 return VK_OEM_PLUS; //case '=': case '+': return 0xBB;
381 // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
384 return VK_OEM_COMMA; //case ',': case '<': return 0xBC;
385 // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
387 case Qt::Key_Underscore:
388 return VK_OEM_MINUS; //case '-': case '_': return 0xBD;
389 // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
391 case Qt::Key_Greater:
392 return VK_OEM_PERIOD; //case '.': case '>': return 0xBE;
393 // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
395 case Qt::Key_Question:
396 return VK_OEM_2; //case '/': case '?': return 0xBF;
397 // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
398 case Qt::Key_AsciiTilde:
399 case Qt::Key_QuoteLeft:
400 return VK_OEM_3; //case '`': case '~': return 0xC0;
401 // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
402 case Qt::Key_BracketLeft:
403 case Qt::Key_BraceLeft:
404 return VK_OEM_4; //case '[': case '{': return 0xDB;
405 // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
406 case Qt::Key_Backslash:
408 return VK_OEM_5; //case '\\': case '|': return 0xDC;
409 // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
410 case Qt::Key_BracketRight:
411 case Qt::Key_BraceRight:
412 return VK_OEM_6; // case ']': case '}': return 0xDD;
413 // 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
414 case Qt::Key_QuoteDbl:
415 return VK_OEM_7; // case '\'': case '"': return 0xDE;
416 // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
417 // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
418 // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
419 // 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
420 // VK_ATTN (F6) Attn key
421 // VK_CRSEL (F7) CrSel key
422 // VK_EXSEL (F8) ExSel key
423 // VK_EREOF (F9) Erase EOF key
424 // VK_PLAY (FA) Play key
425 // VK_ZOOM (FB) Zoom key
426 // VK_NONAME (FC) Reserved for future use
427 // VK_PA1 (FD) PA1 key
428 // VK_OEM_CLEAR (FE) Clear key
435 PlatformKeyboardEvent::PlatformKeyboardEvent(QKeyEvent* event)
437 const int state = event->modifiers();
438 m_type = (event->type() == QEvent::KeyRelease) ? KeyUp : KeyDown;
439 m_text = event->text();
440 m_unmodifiedText = event->text(); // FIXME: not correct
441 m_keyIdentifier = keyIdentifierForQtKeyCode(event->key());
442 m_autoRepeat = event->isAutoRepeat();
443 m_ctrlKey = (state & Qt::ControlModifier) != 0;
444 m_altKey = (state & Qt::AltModifier) != 0;
445 m_metaKey = (state & Qt::MetaModifier) != 0;
446 m_windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(event->key());
447 m_isKeypad = (state & Qt::KeypadModifier) != 0;
448 m_shiftKey = (state & Qt::ShiftModifier) != 0 || event->key() == Qt::Key_Backtab; // Simulate Shift+Tab with Key_Backtab
451 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool)
453 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
454 ASSERT(m_type == KeyDown);
457 if (type == RawKeyDown) {
459 m_unmodifiedText = String();
461 m_keyIdentifier = String();
462 m_windowsVirtualKeyCode = 0;
466 bool PlatformKeyboardEvent::currentCapsLockState()