Bumping manifests a=b2g-bump
[gecko.git] / widget / qt / nsQtKeyUtils.cpp
blobd4d24f95fdbd2d47ee89f96cc52ae4e3494b2e7b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <qnamespace.h>
8 #include "mozilla/ArrayUtils.h"
9 #include "mozilla/TextEvents.h"
11 #include "nsWindow.h"
12 #include "nsQtKeyUtils.h"
14 using namespace mozilla;
15 using namespace mozilla::widget;
17 struct nsKeyConverter
19 int vkCode; // Platform independent key code
20 int keysym; // Qt key code
23 static struct nsKeyConverter nsKeycodes[] =
25 // { NS_VK_CANCEL, Qt::Key_Cancel },
26 { NS_VK_BACK, Qt::Key_Backspace },
27 { NS_VK_TAB, Qt::Key_Tab },
28 { NS_VK_TAB, Qt::Key_Backtab },
29 // { NS_VK_CLEAR, Qt::Key_Clear },
30 { NS_VK_RETURN, Qt::Key_Return },
31 { NS_VK_RETURN, Qt::Key_Enter },
32 { NS_VK_SHIFT, Qt::Key_Shift },
33 { NS_VK_CONTROL, Qt::Key_Control },
34 { NS_VK_ALT, Qt::Key_Alt },
35 { NS_VK_PAUSE, Qt::Key_Pause },
36 { NS_VK_CAPS_LOCK, Qt::Key_CapsLock },
37 { NS_VK_ESCAPE, Qt::Key_Escape },
38 { NS_VK_SPACE, Qt::Key_Space },
39 { NS_VK_PAGE_UP, Qt::Key_PageUp },
40 { NS_VK_PAGE_DOWN, Qt::Key_PageDown },
41 { NS_VK_END, Qt::Key_End },
42 { NS_VK_HOME, Qt::Key_Home },
43 { NS_VK_LEFT, Qt::Key_Left },
44 { NS_VK_UP, Qt::Key_Up },
45 { NS_VK_RIGHT, Qt::Key_Right },
46 { NS_VK_DOWN, Qt::Key_Down },
47 { NS_VK_PRINTSCREEN, Qt::Key_Print },
48 { NS_VK_INSERT, Qt::Key_Insert },
49 { NS_VK_DELETE, Qt::Key_Delete },
50 { NS_VK_HELP, Qt::Key_Help },
52 { NS_VK_0, Qt::Key_0 },
53 { NS_VK_1, Qt::Key_1 },
54 { NS_VK_2, Qt::Key_2 },
55 { NS_VK_3, Qt::Key_3 },
56 { NS_VK_4, Qt::Key_4 },
57 { NS_VK_5, Qt::Key_5 },
58 { NS_VK_6, Qt::Key_6 },
59 { NS_VK_7, Qt::Key_7 },
60 { NS_VK_8, Qt::Key_8 },
61 { NS_VK_9, Qt::Key_9 },
63 { NS_VK_SEMICOLON, Qt::Key_Semicolon },
64 { NS_VK_EQUALS, Qt::Key_Equal },
66 { NS_VK_A, Qt::Key_A },
67 { NS_VK_B, Qt::Key_B },
68 { NS_VK_C, Qt::Key_C },
69 { NS_VK_D, Qt::Key_D },
70 { NS_VK_E, Qt::Key_E },
71 { NS_VK_F, Qt::Key_F },
72 { NS_VK_G, Qt::Key_G },
73 { NS_VK_H, Qt::Key_H },
74 { NS_VK_I, Qt::Key_I },
75 { NS_VK_J, Qt::Key_J },
76 { NS_VK_K, Qt::Key_K },
77 { NS_VK_L, Qt::Key_L },
78 { NS_VK_M, Qt::Key_M },
79 { NS_VK_N, Qt::Key_N },
80 { NS_VK_O, Qt::Key_O },
81 { NS_VK_P, Qt::Key_P },
82 { NS_VK_Q, Qt::Key_Q },
83 { NS_VK_R, Qt::Key_R },
84 { NS_VK_S, Qt::Key_S },
85 { NS_VK_T, Qt::Key_T },
86 { NS_VK_U, Qt::Key_U },
87 { NS_VK_V, Qt::Key_V },
88 { NS_VK_W, Qt::Key_W },
89 { NS_VK_X, Qt::Key_X },
90 { NS_VK_Y, Qt::Key_Y },
91 { NS_VK_Z, Qt::Key_Z },
93 { NS_VK_NUMPAD0, Qt::Key_0 },
94 { NS_VK_NUMPAD1, Qt::Key_1 },
95 { NS_VK_NUMPAD2, Qt::Key_2 },
96 { NS_VK_NUMPAD3, Qt::Key_3 },
97 { NS_VK_NUMPAD4, Qt::Key_4 },
98 { NS_VK_NUMPAD5, Qt::Key_5 },
99 { NS_VK_NUMPAD6, Qt::Key_6 },
100 { NS_VK_NUMPAD7, Qt::Key_7 },
101 { NS_VK_NUMPAD8, Qt::Key_8 },
102 { NS_VK_NUMPAD9, Qt::Key_9 },
103 { NS_VK_MULTIPLY, Qt::Key_Asterisk },
104 { NS_VK_ADD, Qt::Key_Plus },
105 // { NS_VK_SEPARATOR, Qt::Key_Separator },
106 { NS_VK_SUBTRACT, Qt::Key_Minus },
107 { NS_VK_DECIMAL, Qt::Key_Period },
108 { NS_VK_DIVIDE, Qt::Key_Slash },
109 { NS_VK_F1, Qt::Key_F1 },
110 { NS_VK_F2, Qt::Key_F2 },
111 { NS_VK_F3, Qt::Key_F3 },
112 { NS_VK_F4, Qt::Key_F4 },
113 { NS_VK_F5, Qt::Key_F5 },
114 { NS_VK_F6, Qt::Key_F6 },
115 { NS_VK_F7, Qt::Key_F7 },
116 { NS_VK_F8, Qt::Key_F8 },
117 { NS_VK_F9, Qt::Key_F9 },
118 { NS_VK_F10, Qt::Key_F10 },
119 { NS_VK_F11, Qt::Key_F11 },
120 { NS_VK_F12, Qt::Key_F12 },
121 { NS_VK_F13, Qt::Key_F13 },
122 { NS_VK_F14, Qt::Key_F14 },
123 { NS_VK_F15, Qt::Key_F15 },
124 { NS_VK_F16, Qt::Key_F16 },
125 { NS_VK_F17, Qt::Key_F17 },
126 { NS_VK_F18, Qt::Key_F18 },
127 { NS_VK_F19, Qt::Key_F19 },
128 { NS_VK_F20, Qt::Key_F20 },
129 { NS_VK_F21, Qt::Key_F21 },
130 { NS_VK_F22, Qt::Key_F22 },
131 { NS_VK_F23, Qt::Key_F23 },
132 { NS_VK_F24, Qt::Key_F24 },
134 { NS_VK_NUM_LOCK, Qt::Key_NumLock },
135 { NS_VK_SCROLL_LOCK, Qt::Key_ScrollLock },
137 { NS_VK_COMMA, Qt::Key_Comma },
138 { NS_VK_PERIOD, Qt::Key_Period },
139 { NS_VK_SLASH, Qt::Key_Slash },
140 { NS_VK_BACK_QUOTE, Qt::Key_QuoteLeft },
141 { NS_VK_OPEN_BRACKET, Qt::Key_ParenLeft },
142 { NS_VK_CLOSE_BRACKET, Qt::Key_ParenRight },
143 { NS_VK_QUOTE, Qt::Key_QuoteDbl },
145 { NS_VK_META, Qt::Key_Meta }
149 QtKeyCodeToDOMKeyCode(int aKeysym)
151 unsigned int i;
153 // First, try to handle alphanumeric input, not listed in nsKeycodes:
154 // most likely, more letters will be getting typed in than things in
155 // the key list, so we will look through these first.
157 // since X has different key symbols for upper and lowercase letters and
158 // mozilla does not, convert gdk's to mozilla's
159 if (aKeysym >= Qt::Key_A && aKeysym <= Qt::Key_Z)
160 return aKeysym - Qt::Key_A + NS_VK_A;
162 // numbers
163 if (aKeysym >= Qt::Key_0 && aKeysym <= Qt::Key_9)
164 return aKeysym - Qt::Key_0 + NS_VK_0;
166 // keypad numbers
167 // if (aKeysym >= Qt::Key_KP_0 && aKeysym <= Qt::Key_KP_9)
168 // return aKeysym - Qt::Key_KP_0 + NS_VK_NUMPAD0;
170 // misc other things
171 for (i = 0; i < ArrayLength(nsKeycodes); i++) {
172 if (nsKeycodes[i].keysym == aKeysym)
173 return(nsKeycodes[i].vkCode);
176 // function keys
177 if (aKeysym >= Qt::Key_F1 && aKeysym <= Qt::Key_F24)
178 return aKeysym - Qt::Key_F1 + NS_VK_F1;
180 return((int)0);
184 DOMKeyCodeToQtKeyCode(int aKeysym)
186 unsigned int i;
188 // First, try to handle alphanumeric input, not listed in nsKeycodes:
189 // most likely, more letters will be getting typed in than things in
190 // the key list, so we will look through these first.
192 if (aKeysym >= NS_VK_A && aKeysym <= NS_VK_Z)
193 // gdk and DOM both use the ASCII codes for these keys.
194 return aKeysym;
196 // numbers
197 if (aKeysym >= NS_VK_0 && aKeysym <= NS_VK_9)
198 // gdk and DOM both use the ASCII codes for these keys.
199 return aKeysym - Qt::Key_0 + NS_VK_0;
201 // keypad numbers
202 if (aKeysym >= NS_VK_NUMPAD0 && aKeysym <= NS_VK_NUMPAD9) {
203 NS_ERROR("keypad numbers conversion not implemented");
204 //return aKeysym - NS_VK_NUMPAD0 + Qt::Key_KP_0;
205 return 0;
208 // misc other things
209 for (i = 0; i < ArrayLength(nsKeycodes); ++i) {
210 if (nsKeycodes[i].vkCode == aKeysym) {
211 return nsKeycodes[i].keysym;
215 // function keys
216 if (aKeysym >= NS_VK_F1 && aKeysym <= NS_VK_F9)
217 return aKeysym - NS_VK_F1 + Qt::Key_F1;
219 return 0;
222 KeyNameIndex
223 QtKeyCodeToDOMKeyNameIndex(int aKeysym)
225 switch (aKeysym) {
227 #define NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX(aNativeKey, aKeyNameIndex) \
228 case aNativeKey: return aKeyNameIndex;
230 #include "NativeKeyToDOMKeyName.h"
232 #undef NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX
234 case Qt::Key_Exclam:
235 case Qt::Key_QuoteDbl:
236 case Qt::Key_NumberSign:
237 case Qt::Key_Dollar:
238 case Qt::Key_Percent:
239 case Qt::Key_Ampersand:
240 case Qt::Key_Apostrophe:
241 case Qt::Key_ParenLeft:
242 case Qt::Key_ParenRight:
243 case Qt::Key_Asterisk:
244 case Qt::Key_Plus:
245 case Qt::Key_Comma:
246 case Qt::Key_Minus:
247 case Qt::Key_Period:
248 case Qt::Key_Slash:
249 case Qt::Key_0:
250 case Qt::Key_1:
251 case Qt::Key_2:
252 case Qt::Key_3:
253 case Qt::Key_4:
254 case Qt::Key_5:
255 case Qt::Key_6:
256 case Qt::Key_7:
257 case Qt::Key_8:
258 case Qt::Key_9:
259 case Qt::Key_Colon:
260 case Qt::Key_Semicolon:
261 case Qt::Key_Less:
262 case Qt::Key_Equal:
263 case Qt::Key_Greater:
264 case Qt::Key_Question:
265 case Qt::Key_At:
266 case Qt::Key_A:
267 case Qt::Key_B:
268 case Qt::Key_C:
269 case Qt::Key_D:
270 case Qt::Key_E:
271 case Qt::Key_F:
272 case Qt::Key_G:
273 case Qt::Key_H:
274 case Qt::Key_I:
275 case Qt::Key_J:
276 case Qt::Key_K:
277 case Qt::Key_L:
278 case Qt::Key_M:
279 case Qt::Key_N:
280 case Qt::Key_O:
281 case Qt::Key_P:
282 case Qt::Key_Q:
283 case Qt::Key_R:
284 case Qt::Key_S:
285 case Qt::Key_T:
286 case Qt::Key_U:
287 case Qt::Key_V:
288 case Qt::Key_W:
289 case Qt::Key_X:
290 case Qt::Key_Y:
291 case Qt::Key_Z:
292 case Qt::Key_BracketLeft:
293 case Qt::Key_Backslash:
294 case Qt::Key_BracketRight:
295 case Qt::Key_AsciiCircum:
296 case Qt::Key_Underscore:
297 case Qt::Key_QuoteLeft:
298 case Qt::Key_BraceLeft:
299 case Qt::Key_Bar:
300 case Qt::Key_BraceRight:
301 case Qt::Key_AsciiTilde:
302 case Qt::Key_exclamdown:
303 case Qt::Key_cent:
304 case Qt::Key_sterling:
305 case Qt::Key_currency:
306 case Qt::Key_yen:
307 case Qt::Key_brokenbar:
308 case Qt::Key_section:
309 case Qt::Key_diaeresis:
310 case Qt::Key_copyright:
311 case Qt::Key_ordfeminine:
312 case Qt::Key_guillemotleft:
313 case Qt::Key_notsign:
314 case Qt::Key_hyphen:
315 case Qt::Key_registered:
316 case Qt::Key_macron:
317 case Qt::Key_degree:
318 case Qt::Key_plusminus:
319 case Qt::Key_twosuperior:
320 case Qt::Key_threesuperior:
321 case Qt::Key_acute:
322 case Qt::Key_mu:
323 case Qt::Key_paragraph:
324 case Qt::Key_periodcentered:
325 case Qt::Key_cedilla:
326 case Qt::Key_onesuperior:
327 case Qt::Key_masculine:
328 case Qt::Key_guillemotright:
329 case Qt::Key_onequarter:
330 case Qt::Key_onehalf:
331 case Qt::Key_threequarters:
332 case Qt::Key_questiondown:
333 case Qt::Key_Agrave:
334 case Qt::Key_Aacute:
335 case Qt::Key_Acircumflex:
336 case Qt::Key_Atilde:
337 case Qt::Key_Adiaeresis:
338 case Qt::Key_Aring:
339 case Qt::Key_AE:
340 case Qt::Key_Ccedilla:
341 case Qt::Key_Egrave:
342 case Qt::Key_Eacute:
343 case Qt::Key_Ecircumflex:
344 case Qt::Key_Ediaeresis:
345 case Qt::Key_Igrave:
346 case Qt::Key_Iacute:
347 case Qt::Key_Icircumflex:
348 case Qt::Key_Idiaeresis:
349 case Qt::Key_ETH:
350 case Qt::Key_Ntilde:
351 case Qt::Key_Ograve:
352 case Qt::Key_Oacute:
353 case Qt::Key_Ocircumflex:
354 case Qt::Key_Otilde:
355 case Qt::Key_Odiaeresis:
356 case Qt::Key_multiply:
357 case Qt::Key_Ooblique:
358 case Qt::Key_Ugrave:
359 case Qt::Key_Uacute:
360 case Qt::Key_Ucircumflex:
361 case Qt::Key_Udiaeresis:
362 case Qt::Key_Yacute:
363 case Qt::Key_THORN:
364 case Qt::Key_ssharp:
365 case Qt::Key_division:
366 case Qt::Key_ydiaeresis:
367 return KEY_NAME_INDEX_PrintableKey;
369 case Qt::Key_Backtab:
370 case Qt::Key_Direction_L:
371 case Qt::Key_Direction_R:
372 case Qt::Key_SingleCandidate:
373 case Qt::Key_Hiragana_Katakana:
374 case Qt::Key_Zenkaku_Hankaku:
375 case Qt::Key_Touroku:
376 case Qt::Key_Massyo:
377 case Qt::Key_Hangul:
378 case Qt::Key_Hangul_Start:
379 case Qt::Key_Hangul_End:
380 case Qt::Key_Hangul_Hanja:
381 case Qt::Key_Hangul_Jamo:
382 case Qt::Key_Hangul_Romaja:
383 case Qt::Key_Hangul_Jeonja:
384 case Qt::Key_Hangul_Banja:
385 case Qt::Key_Hangul_PreHanja:
386 case Qt::Key_Hangul_PostHanja:
387 case Qt::Key_Hangul_Special:
388 case Qt::Key_Dead_Belowdot:
389 case Qt::Key_Dead_Hook:
390 case Qt::Key_Dead_Horn:
391 case Qt::Key_TrebleUp:
392 case Qt::Key_TrebleDown:
393 case Qt::Key_Standby:
394 case Qt::Key_OpenUrl:
395 case Qt::Key_LaunchMedia:
396 case Qt::Key_KeyboardLightOnOff:
397 case Qt::Key_KeyboardBrightnessUp:
398 case Qt::Key_KeyboardBrightnessDown:
399 case Qt::Key_WakeUp:
400 case Qt::Key_ScreenSaver:
401 case Qt::Key_WWW:
402 case Qt::Key_Memo:
403 case Qt::Key_LightBulb:
404 case Qt::Key_Shop:
405 case Qt::Key_History:
406 case Qt::Key_AddFavorite:
407 case Qt::Key_HotLinks:
408 case Qt::Key_Finance:
409 case Qt::Key_Community:
410 case Qt::Key_BackForward:
411 case Qt::Key_ApplicationLeft:
412 case Qt::Key_ApplicationRight:
413 case Qt::Key_Book:
414 case Qt::Key_CD:
415 case Qt::Key_Calculator:
416 case Qt::Key_ToDoList:
417 case Qt::Key_ClearGrab:
418 case Qt::Key_Close:
419 case Qt::Key_Display:
420 case Qt::Key_DOS:
421 case Qt::Key_Documents:
422 case Qt::Key_Excel:
423 case Qt::Key_Explorer:
424 case Qt::Key_Game:
425 case Qt::Key_Go:
426 case Qt::Key_iTouch:
427 case Qt::Key_LogOff:
428 case Qt::Key_Market:
429 case Qt::Key_Meeting:
430 case Qt::Key_MenuKB:
431 case Qt::Key_MenuPB:
432 case Qt::Key_MySites:
433 case Qt::Key_News:
434 case Qt::Key_OfficeHome:
435 case Qt::Key_Option:
436 case Qt::Key_Phone:
437 case Qt::Key_Calendar:
438 case Qt::Key_Reply:
439 case Qt::Key_RotateWindows:
440 case Qt::Key_RotationPB:
441 case Qt::Key_RotationKB:
442 case Qt::Key_Save:
443 case Qt::Key_Send:
444 case Qt::Key_Spell:
445 case Qt::Key_SplitScreen:
446 case Qt::Key_Support:
447 case Qt::Key_TaskPane:
448 case Qt::Key_Terminal:
449 case Qt::Key_Tools:
450 case Qt::Key_Travel:
451 case Qt::Key_Video:
452 case Qt::Key_Word:
453 case Qt::Key_Xfer:
454 case Qt::Key_ZoomIn:
455 case Qt::Key_ZoomOut:
456 case Qt::Key_Away:
457 case Qt::Key_Messenger:
458 case Qt::Key_WebCam:
459 case Qt::Key_MailForward:
460 case Qt::Key_Pictures:
461 case Qt::Key_Music:
462 case Qt::Key_Battery:
463 case Qt::Key_Bluetooth:
464 case Qt::Key_WLAN:
465 case Qt::Key_UWB:
466 case Qt::Key_AudioRepeat:
467 case Qt::Key_AudioCycleTrack:
468 case Qt::Key_Time:
469 case Qt::Key_Hibernate:
470 case Qt::Key_View:
471 case Qt::Key_TopMenu:
472 case Qt::Key_PowerDown:
473 case Qt::Key_Suspend:
474 case Qt::Key_ContrastAdjust:
475 case Qt::Key_TouchpadToggle:
476 case Qt::Key_TouchpadOn:
477 case Qt::Key_TouchpadOff:
478 case Qt::Key_unknown:
479 case Qt::Key_Call:
480 case Qt::Key_CameraFocus:
481 case Qt::Key_Context1:
482 case Qt::Key_Context2:
483 case Qt::Key_Context3:
484 case Qt::Key_Context4:
485 case Qt::Key_Flip:
486 case Qt::Key_Hangup:
487 case Qt::Key_No:
488 case Qt::Key_Select:
489 case Qt::Key_Yes:
490 case Qt::Key_ToggleCallHangup:
491 case Qt::Key_VoiceDial:
492 case Qt::Key_LastNumberRedial:
493 case Qt::Key_Printer:
494 case Qt::Key_Sleep:
495 default:
496 return KEY_NAME_INDEX_Unidentified;
500 CodeNameIndex
501 ScanCodeToDOMCodeNameIndex(int32_t aScanCode)
503 switch (aScanCode) {
505 #define NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX(aNativeKey, aCodeNameIndex) \
506 case aNativeKey: return aCodeNameIndex;
508 #include "NativeKeyToDOMCodeName.h"
510 #undef NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX
512 default:
513 return CODE_NAME_INDEX_UNKNOWN;