keybindings.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* keybindings.cpp --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include "keybindings.h"
  32. /* InputEvent key modifiers */
  33. #define SHIFT_MASK 1
  34. #define CTRL_MASK 2
  35. #define META_MASK 4
  36. #define ALT_MASK 8
  37. #define ALT_GRAPH_MASK 0x20
  38. #define BUTTON1_MASK 0x10
  39. #define BUTTON2_MASK 8
  40. #define BUTTON3_MASK 4
  41. #define SHIFT_DOWN_MASK 0x0040
  42. #define CTRL_DOWN_MASK 0x0080
  43. #define META_DOWN_MASK 0x0100
  44. #define ALT_DOWN_MASK 0x0200
  45. #define BUTTON1_DOWN_MASK 0x0400
  46. #define BUTTON2_DOWN_MASK 0x0800
  47. #define BUTTON3_DOWN_MASK 0x1000
  48. #define ALT_GRAPH_DOWN_MASK 0x2000
  49. /* Virtual Keys */
  50. /* This list should be kept in the same order as the VK_ field
  51. declarations in KeyEvent.java. */
  52. #define VK_ENTER '\n'
  53. #define VK_BACK_SPACE '\b'
  54. #define VK_TAB '\t'
  55. #define VK_CANCEL 3
  56. #define VK_CLEAR 12
  57. #define VK_SHIFT 16
  58. #define VK_CONTROL 17
  59. #define VK_ALT 18
  60. #define VK_PAUSE 19
  61. #define VK_CAPS_LOCK 20
  62. #define VK_ESCAPE 27
  63. #define VK_SPACE ' '
  64. #define VK_PAGE_UP 33
  65. #define VK_PAGE_DOWN 34
  66. #define VK_END 35
  67. #define VK_HOME 36
  68. #define VK_LEFT 37
  69. #define VK_UP 38
  70. #define VK_RIGHT 39
  71. #define VK_DOWN 40
  72. #define VK_COMMA ','
  73. #define VK_MINUS '-'
  74. #define VK_PERIOD '.'
  75. #define VK_SLASH '/'
  76. #define VK_0 '0'
  77. #define VK_1 '1'
  78. #define VK_2 '2'
  79. #define VK_3 '3'
  80. #define VK_4 '4'
  81. #define VK_5 '5'
  82. #define VK_6 '6'
  83. #define VK_7 '7'
  84. #define VK_8 '8'
  85. #define VK_9 '9'
  86. #define VK_SEMICOLON ';'
  87. #define VK_EQUALS '='
  88. #define VK_A 'A'
  89. #define VK_B 'B'
  90. #define VK_C 'C'
  91. #define VK_D 'D'
  92. #define VK_E 'E'
  93. #define VK_F 'F'
  94. #define VK_G 'G'
  95. #define VK_H 'H'
  96. #define VK_I 'I'
  97. #define VK_J 'J'
  98. #define VK_K 'K'
  99. #define VK_L 'L'
  100. #define VK_M 'M'
  101. #define VK_N 'N'
  102. #define VK_O 'O'
  103. #define VK_P 'P'
  104. #define VK_Q 'Q'
  105. #define VK_R 'R'
  106. #define VK_S 'S'
  107. #define VK_T 'T'
  108. #define VK_U 'U'
  109. #define VK_V 'V'
  110. #define VK_W 'W'
  111. #define VK_X 'X'
  112. #define VK_Y 'Y'
  113. #define VK_Z 'Z'
  114. #define VK_OPEN_BRACKET '['
  115. #define VK_BACK_SLASH '\\'
  116. #define VK_CLOSE_BRACKET ']'
  117. #define VK_NUMPAD0 96
  118. #define VK_NUMPAD1 97
  119. #define VK_NUMPAD2 98
  120. #define VK_NUMPAD3 99
  121. #define VK_NUMPAD4 100
  122. #define VK_NUMPAD5 101
  123. #define VK_NUMPAD6 102
  124. #define VK_NUMPAD7 103
  125. #define VK_NUMPAD8 104
  126. #define VK_NUMPAD9 105
  127. #define VK_MULTIPLY 106
  128. #define VK_ADD 107
  129. #define VK_SEPARATER 108
  130. #define VK_SEPARATOR 108
  131. #define VK_SUBTRACT 109
  132. #define VK_DECIMAL 110
  133. #define VK_DIVIDE 111
  134. #define VK_DELETE 127
  135. #define VK_NUM_LOCK 144
  136. #define VK_SCROLL_LOCK 145
  137. #define VK_F1 112
  138. #define VK_F2 113
  139. #define VK_F3 114
  140. #define VK_F4 115
  141. #define VK_F5 116
  142. #define VK_F6 117
  143. #define VK_F7 118
  144. #define VK_F8 119
  145. #define VK_F9 120
  146. #define VK_F10 121
  147. #define VK_F11 122
  148. #define VK_F12 123
  149. #define VK_F13 61440
  150. #define VK_F14 61441
  151. #define VK_F15 61442
  152. #define VK_F16 61443
  153. #define VK_F17 61444
  154. #define VK_F18 61445
  155. #define VK_F19 61446
  156. #define VK_F20 61447
  157. #define VK_F21 61448
  158. #define VK_F22 61449
  159. #define VK_F23 61450
  160. #define VK_F24 61451
  161. #define VK_PRINTSCREEN 154
  162. #define VK_INSERT 155
  163. #define VK_HELP 156
  164. #define VK_META 157
  165. #define VK_BACK_QUOTE 192
  166. #define VK_QUOTE 222
  167. #define VK_KP_UP 224
  168. #define VK_KP_DOWN 225
  169. #define VK_KP_LEFT 226
  170. #define VK_KP_RIGHT 227
  171. #define VK_DEAD_GRAVE 128
  172. #define VK_DEAD_ACUTE 129
  173. #define VK_DEAD_CIRCUMFLEX 130
  174. #define VK_DEAD_TILDE 131
  175. #define VK_DEAD_MACRON 132
  176. #define VK_DEAD_BREVE 133
  177. #define VK_DEAD_ABOVEDOT 134
  178. #define VK_DEAD_DIAERESIS 135
  179. #define VK_DEAD_ABOVERING 136
  180. #define VK_DEAD_DOUBLEACUTE 137
  181. #define VK_DEAD_CARON 138
  182. #define VK_DEAD_CEDILLA 139
  183. #define VK_DEAD_OGONEK 140
  184. #define VK_DEAD_IOTA 141
  185. #define VK_DEAD_VOICED_SOUND 142
  186. #define VK_DEAD_SEMIVOICED_SOUND 143
  187. #define VK_AMPERSAND 150
  188. #define VK_ASTERISK 151
  189. #define VK_QUOTEDBL 152
  190. #define VK_LESS 153
  191. #define VK_GREATER 160
  192. #define VK_BRACELEFT 161
  193. #define VK_BRACERIGHT 162
  194. #define VK_AT 512
  195. #define VK_COLON 513
  196. #define VK_CIRCUMFLEX 514
  197. #define VK_DOLLAR 515
  198. #define VK_EURO_SIGN 516
  199. #define VK_EXCLAMATION_MARK 517
  200. #define VK_INVERTED_EXCLAMATION_MARK 518
  201. #define VK_LEFT_PARENTHESIS 519
  202. #define VK_NUMBER_SIGN 520
  203. #define VK_PLUS 521
  204. #define VK_RIGHT_PARENTHESIS 522
  205. #define VK_UNDERSCORE 523
  206. #define VK_FINAL 24
  207. #define VK_CONVERT 28
  208. #define VK_NONCONVERT 29
  209. #define VK_ACCEPT 30
  210. #define VK_MODECHANGE 31
  211. #define VK_KANA 21
  212. #define VK_KANJI 25
  213. #define VK_ALPHANUMERIC 240
  214. #define VK_KATAKANA 241
  215. #define VK_HIRAGANA 242
  216. #define VK_FULL_WIDTH 243
  217. #define VK_HALF_WIDTH 244
  218. #define VK_ROMAN_CHARACTERS 245
  219. #define VK_ALL_CANDIDATES 256
  220. #define VK_PREVIOUS_CANDIDATE 257
  221. #define VK_CODE_INPUT 258
  222. #define VK_JAPANESE_KATAKANA 259
  223. #define VK_JAPANESE_HIRAGANA 260
  224. #define VK_JAPANESE_ROMAN 261
  225. #define VK_KANA_LOCK 262
  226. #define VK_INPUT_METHOD_ON_OFF 263
  227. #define VK_CUT 65489
  228. #define VK_COPY 65485
  229. #define VK_PASTE 65487
  230. #define VK_UNDO 65483
  231. #define VK_AGAIN 65481
  232. #define VK_FIND 65488
  233. #define VK_PROPS 65482
  234. #define VK_STOP 65480
  235. #define VK_COMPOSE 65312
  236. #define VK_ALT_GRAPH 65406
  237. #define VK_UNDEFINED 0
  238. int mapKeyCode(QKeyEvent *key)
  239. {
  240. switch(key->key())
  241. {
  242. case Qt::Key_Escape:
  243. return VK_ESCAPE;
  244. case Qt::Key_Tab:
  245. return VK_TAB;
  246. case Qt::Key_Backspace:
  247. return VK_BACK_SPACE;
  248. case Qt::Key_Return:
  249. case Qt::Key_Enter:
  250. return VK_ENTER;
  251. case Qt::Key_Insert:
  252. return VK_INSERT;
  253. case Qt::Key_Delete:
  254. return VK_DELETE;
  255. case Qt::Key_Pause:
  256. return VK_PAUSE;
  257. case Qt::Key_Print:
  258. case Qt::Key_SysReq:
  259. return VK_PRINTSCREEN;
  260. case Qt::Key_Home:
  261. return VK_HOME;
  262. case Qt::Key_End:
  263. return VK_END;
  264. case Qt::Key_Left:
  265. return VK_LEFT;
  266. case Qt::Key_Up:
  267. return VK_UP;
  268. case Qt::Key_Right:
  269. return VK_RIGHT;
  270. case Qt::Key_Down:
  271. return VK_DOWN;
  272. case Qt::Key_PageUp:
  273. return VK_PAGE_UP;
  274. case Qt::Key_PageDown:
  275. return VK_PAGE_DOWN;
  276. case Qt::Key_Shift:
  277. return VK_SHIFT;
  278. case Qt::Key_Control:
  279. return VK_CONTROL;
  280. case Qt::Key_Meta:
  281. return VK_META;
  282. case Qt::Key_Alt:
  283. return VK_ALT;
  284. case Qt::Key_CapsLock:
  285. return VK_CAPS_LOCK;
  286. case Qt::Key_NumLock:
  287. return VK_NUM_LOCK;
  288. case Qt::Key_ScrollLock:
  289. return VK_SCROLL_LOCK;
  290. case Qt::Key_Clear:
  291. return VK_CLEAR;
  292. case Qt::Key_F1:
  293. return VK_F1;
  294. case Qt::Key_F2:
  295. return VK_F2;
  296. case Qt::Key_F3:
  297. return VK_F3;
  298. case Qt::Key_F4:
  299. return VK_F4;
  300. case Qt::Key_F5:
  301. return VK_F5;
  302. case Qt::Key_F6:
  303. return VK_F6;
  304. case Qt::Key_F7:
  305. return VK_F7;
  306. case Qt::Key_F8:
  307. return VK_F8;
  308. case Qt::Key_F9:
  309. return VK_F9;
  310. case Qt::Key_F10:
  311. return VK_F10;
  312. case Qt::Key_F11:
  313. return VK_F11;
  314. case Qt::Key_F12:
  315. return VK_F12;
  316. case Qt::Key_F13:
  317. return VK_F13;
  318. case Qt::Key_F14:
  319. return VK_F14;
  320. case Qt::Key_F15:
  321. return VK_F15;
  322. case Qt::Key_F16:
  323. return VK_F16;
  324. case Qt::Key_F17:
  325. return VK_F17;
  326. case Qt::Key_F18:
  327. return VK_F18;
  328. case Qt::Key_F19:
  329. return VK_F19;
  330. case Qt::Key_F20:
  331. return VK_F20;
  332. case Qt::Key_F21:
  333. return VK_F21;
  334. case Qt::Key_F22:
  335. return VK_F22;
  336. case Qt::Key_F23:
  337. return VK_F23;
  338. case Qt::Key_F24:
  339. return VK_F24;
  340. case Qt::Key_Help:
  341. return VK_HELP;
  342. case Qt::Key_Space:
  343. return VK_SPACE;
  344. case Qt::Key_Exclam:
  345. return VK_EXCLAMATION_MARK;
  346. case Qt::Key_QuoteDbl:
  347. return VK_QUOTEDBL;
  348. case Qt::Key_NumberSign:
  349. return VK_NUMBER_SIGN;
  350. case Qt::Key_Dollar:
  351. return VK_DOLLAR;
  352. case Qt::Key_Ampersand:
  353. return VK_AMPERSAND;
  354. case Qt::Key_ParenLeft:
  355. return VK_LEFT_PARENTHESIS;
  356. case Qt::Key_ParenRight:
  357. return VK_RIGHT_PARENTHESIS;
  358. case Qt::Key_Asterisk:
  359. return VK_ASTERISK;
  360. case Qt::Key_Plus:
  361. return VK_PLUS;
  362. case Qt::Key_Comma:
  363. return VK_COMMA;
  364. case Qt::Key_Minus:
  365. return VK_MINUS;
  366. case Qt::Key_Period:
  367. return VK_PERIOD;
  368. case Qt::Key_Slash:
  369. return VK_SLASH;
  370. case Qt::Key_0:
  371. return VK_0;
  372. case Qt::Key_1:
  373. return VK_1;
  374. case Qt::Key_2:
  375. return VK_2;
  376. case Qt::Key_3:
  377. return VK_3;
  378. case Qt::Key_4:
  379. return VK_4;
  380. case Qt::Key_5:
  381. return VK_5 ;
  382. case Qt::Key_6:
  383. return VK_6;
  384. case Qt::Key_7:
  385. return VK_7;
  386. case Qt::Key_8:
  387. return VK_8;
  388. case Qt::Key_9:
  389. return VK_9;
  390. case Qt::Key_Colon:
  391. return VK_COLON;
  392. case Qt::Key_Semicolon:
  393. return VK_SEMICOLON;
  394. case Qt::Key_Less:
  395. return VK_LESS;
  396. case Qt::Key_Equal:
  397. return VK_EQUALS;
  398. case Qt::Key_Greater:
  399. return VK_GREATER;
  400. case Qt::Key_Question:
  401. case Qt::Key_At:
  402. case Qt::Key_A:
  403. return VK_A;
  404. case Qt::Key_B:
  405. return VK_B;
  406. case Qt::Key_C:
  407. return VK_C;
  408. case Qt::Key_D:
  409. return VK_D;
  410. case Qt::Key_E:
  411. return VK_E;
  412. case Qt::Key_F:
  413. return VK_F;
  414. case Qt::Key_G:
  415. return VK_G;
  416. case Qt::Key_H:
  417. return VK_H;
  418. case Qt::Key_I:
  419. return VK_I;
  420. case Qt::Key_J:
  421. return VK_J;
  422. case Qt::Key_K:
  423. return VK_K;
  424. case Qt::Key_L:
  425. return VK_L;
  426. case Qt::Key_M:
  427. return VK_M;
  428. case Qt::Key_N:
  429. return VK_N;
  430. case Qt::Key_O:
  431. return VK_O;
  432. case Qt::Key_P:
  433. return VK_P;
  434. case Qt::Key_Q:
  435. return VK_Q;
  436. case Qt::Key_R:
  437. return VK_R;
  438. case Qt::Key_S:
  439. return VK_S;
  440. case Qt::Key_T:
  441. return VK_T;
  442. case Qt::Key_U:
  443. return VK_U;
  444. case Qt::Key_V:
  445. return VK_V;
  446. case Qt::Key_W:
  447. return VK_W;
  448. case Qt::Key_X:
  449. return VK_X;
  450. case Qt::Key_Y:
  451. return VK_Y;
  452. case Qt::Key_Z:
  453. return VK_Z;
  454. case Qt::Key_division:
  455. return VK_DIVIDE;
  456. case Qt::Key_BracketLeft:
  457. return VK_OPEN_BRACKET;
  458. case Qt::Key_Backslash:
  459. return VK_BACK_SLASH;
  460. case Qt::Key_BracketRight:
  461. return VK_CLOSE_BRACKET;
  462. case Qt::Key_BraceLeft:
  463. return VK_BRACELEFT;
  464. case Qt::Key_BraceRight:
  465. return VK_BRACERIGHT;
  466. case Qt::Key_brokenbar:
  467. return VK_SEPARATOR; // correct?
  468. default:
  469. return VK_UNDEFINED;
  470. }
  471. }
  472. int getUnicode(QKeyEvent *key)
  473. {
  474. QString s = key->text();
  475. if(s.isEmpty())
  476. return 0; // CHAR_UNDEFINED
  477. QChar c = s.at(0);
  478. return (int)c.unicode();
  479. }
  480. /**
  481. * Returns the key modifiers in KeyEvent format
  482. */
  483. int getKeyModifiers(Qt::KeyboardModifiers state)
  484. {
  485. int modifier = 0;
  486. if( state & Qt::ShiftModifier )
  487. modifier |= SHIFT_DOWN_MASK;
  488. if( state & Qt::ControlModifier )
  489. modifier |= CTRL_DOWN_MASK;
  490. if( state & Qt::AltModifier )
  491. modifier |= ALT_DOWN_MASK;
  492. if( state & Qt::MetaModifier )
  493. modifier |= META_DOWN_MASK;
  494. return modifier;
  495. }
  496. /**
  497. * Returns the key modifiers in ActionEvent format
  498. */
  499. int getAEKeyModifiers(Qt::KeyboardModifiers state)
  500. {
  501. int modifier = 0;
  502. if( state & Qt::ShiftModifier )
  503. modifier |= SHIFT_MASK;
  504. if( state & Qt::ControlModifier )
  505. modifier |= CTRL_MASK;
  506. if( state & Qt::AltModifier )
  507. modifier |= ALT_MASK;
  508. if( state & Qt::MetaModifier )
  509. modifier |= META_MASK;
  510. return modifier;
  511. }
  512. /**
  513. * Returns the mouse modifiers in InputEvent format
  514. */
  515. int getMouseModifiers(QMouseEvent *e)
  516. {
  517. int modifier = 0;
  518. int buttons = e->buttons();
  519. int state = e->modifiers();
  520. if( buttons & Qt::LeftButton )
  521. modifier |= BUTTON1_DOWN_MASK;
  522. if( buttons & Qt::MidButton )
  523. modifier |= BUTTON2_DOWN_MASK;
  524. if( buttons & Qt::RightButton )
  525. modifier |= BUTTON3_DOWN_MASK;
  526. if( state & Qt::ShiftModifier )
  527. modifier |= SHIFT_DOWN_MASK;
  528. if( state & Qt::ControlModifier )
  529. modifier |= CTRL_DOWN_MASK;
  530. if( state & Qt::AltModifier )
  531. modifier |= ALT_DOWN_MASK;
  532. if( state & Qt::MetaModifier )
  533. modifier |= META_DOWN_MASK;
  534. // FIXME: Alt Gr?
  535. return modifier;
  536. }
  537. /**
  538. * Returns the mouse modifiers in InputEvent format
  539. * We need a different method here because e->buttons() doesn't work for,
  540. * mouseReleased events. (But strangely enough it does for pressed ones)
  541. */
  542. int getReleaseModifiers(QMouseEvent *e)
  543. {
  544. int modifier = 0;
  545. int button = e->button();
  546. int state = e->modifiers();
  547. if( button & Qt::LeftButton )
  548. modifier |= BUTTON1_DOWN_MASK;
  549. if( button & Qt::MidButton )
  550. modifier |= BUTTON2_DOWN_MASK;
  551. if( button & Qt::RightButton )
  552. modifier |= BUTTON3_DOWN_MASK;
  553. if( state & Qt::ShiftModifier )
  554. modifier |= SHIFT_DOWN_MASK;
  555. if( state & Qt::ControlModifier )
  556. modifier |= CTRL_DOWN_MASK;
  557. if( state & Qt::AltModifier )
  558. modifier |= ALT_DOWN_MASK;
  559. if( state & Qt::MetaModifier )
  560. modifier |= META_DOWN_MASK;
  561. // FIXME: Alt Gr?
  562. return modifier;
  563. }