keyboard.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*************************************************************************/
  2. /* keyboard.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "keyboard.h"
  31. #include "core/os/os.h"
  32. struct _KeyCodeText {
  33. int code;
  34. const char *text;
  35. };
  36. static const _KeyCodeText _keycodes[] = {
  37. /* clang-format off */
  38. {KEY_ESCAPE ,"Escape"},
  39. {KEY_TAB ,"Tab"},
  40. {KEY_BACKTAB ,"BackTab"},
  41. {KEY_BACKSPACE ,"BackSpace"},
  42. {KEY_ENTER ,"Enter"},
  43. {KEY_KP_ENTER ,"Kp Enter"},
  44. {KEY_INSERT ,"Insert"},
  45. {KEY_DELETE ,"Delete"},
  46. {KEY_PAUSE ,"Pause"},
  47. {KEY_PRINT ,"Print"},
  48. {KEY_SYSREQ ,"SysReq"},
  49. {KEY_CLEAR ,"Clear"},
  50. {KEY_HOME ,"Home"},
  51. {KEY_END ,"End"},
  52. {KEY_LEFT ,"Left"},
  53. {KEY_UP ,"Up"},
  54. {KEY_RIGHT ,"Right"},
  55. {KEY_DOWN ,"Down"},
  56. {KEY_PAGEUP ,"PageUp"},
  57. {KEY_PAGEDOWN ,"PageDown"},
  58. {KEY_SHIFT ,"Shift"},
  59. {KEY_CONTROL ,"Control"},
  60. #ifdef OSX_ENABLED
  61. {KEY_META ,"Command"},
  62. #else
  63. {KEY_META ,"Meta"},
  64. #endif
  65. {KEY_ALT ,"Alt"},
  66. {KEY_CAPSLOCK ,"CapsLock"},
  67. {KEY_NUMLOCK ,"NumLock"},
  68. {KEY_SCROLLLOCK ,"ScrollLock"},
  69. {KEY_F1 ,"F1"},
  70. {KEY_F2 ,"F2"},
  71. {KEY_F3 ,"F3"},
  72. {KEY_F4 ,"F4"},
  73. {KEY_F5 ,"F5"},
  74. {KEY_F6 ,"F6"},
  75. {KEY_F7 ,"F7"},
  76. {KEY_F8 ,"F8"},
  77. {KEY_F9 ,"F9"},
  78. {KEY_F10 ,"F10"},
  79. {KEY_F11 ,"F11"},
  80. {KEY_F12 ,"F12"},
  81. {KEY_F13 ,"F13"},
  82. {KEY_F14 ,"F14"},
  83. {KEY_F15 ,"F15"},
  84. {KEY_F16 ,"F16"},
  85. {KEY_KP_MULTIPLY ,"Kp Multiply"},
  86. {KEY_KP_DIVIDE ,"Kp Divide"},
  87. {KEY_KP_SUBTRACT ,"Kp Subtract"},
  88. {KEY_KP_PERIOD ,"Kp Period"},
  89. {KEY_KP_ADD ,"Kp Add"},
  90. {KEY_KP_0 ,"Kp 0"},
  91. {KEY_KP_1 ,"Kp 1"},
  92. {KEY_KP_2 ,"Kp 2"},
  93. {KEY_KP_3 ,"Kp 3"},
  94. {KEY_KP_4 ,"Kp 4"},
  95. {KEY_KP_5 ,"Kp 5"},
  96. {KEY_KP_6 ,"Kp 6"},
  97. {KEY_KP_7 ,"Kp 7"},
  98. {KEY_KP_8 ,"Kp 8"},
  99. {KEY_KP_9 ,"Kp 9"},
  100. {KEY_SUPER_L ,"Super L"},
  101. {KEY_SUPER_R ,"Super R"},
  102. {KEY_MENU ,"Menu"},
  103. {KEY_HYPER_L ,"Hyper L"},
  104. {KEY_HYPER_R ,"Hyper R"},
  105. {KEY_HELP ,"Help"},
  106. {KEY_DIRECTION_L ,"Direction L"},
  107. {KEY_DIRECTION_R ,"Direction R"},
  108. {KEY_BACK ,"Back"},
  109. {KEY_FORWARD ,"Forward"},
  110. {KEY_STOP ,"Stop"},
  111. {KEY_REFRESH ,"Refresh"},
  112. {KEY_VOLUMEDOWN ,"VolumeDown"},
  113. {KEY_VOLUMEMUTE ,"VolumeMute"},
  114. {KEY_VOLUMEUP ,"VolumeUp"},
  115. {KEY_BASSBOOST ,"BassBoost"},
  116. {KEY_BASSUP ,"BassUp"},
  117. {KEY_BASSDOWN ,"BassDown"},
  118. {KEY_TREBLEUP ,"TrebleUp"},
  119. {KEY_TREBLEDOWN ,"TrebleDown"},
  120. {KEY_MEDIAPLAY ,"MediaPlay"},
  121. {KEY_MEDIASTOP ,"MediaStop"},
  122. {KEY_MEDIAPREVIOUS ,"MediaPrevious"},
  123. {KEY_MEDIANEXT ,"MediaNext"},
  124. {KEY_MEDIARECORD ,"MediaRecord"},
  125. {KEY_HOMEPAGE ,"HomePage"},
  126. {KEY_FAVORITES ,"Favorites"},
  127. {KEY_SEARCH ,"Search"},
  128. {KEY_STANDBY ,"StandBy"},
  129. {KEY_LAUNCHMAIL ,"LaunchMail"},
  130. {KEY_LAUNCHMEDIA ,"LaunchMedia"},
  131. {KEY_LAUNCH0 ,"Launch0"},
  132. {KEY_LAUNCH1 ,"Launch1"},
  133. {KEY_LAUNCH2 ,"Launch2"},
  134. {KEY_LAUNCH3 ,"Launch3"},
  135. {KEY_LAUNCH4 ,"Launch4"},
  136. {KEY_LAUNCH5 ,"Launch5"},
  137. {KEY_LAUNCH6 ,"Launch6"},
  138. {KEY_LAUNCH7 ,"Launch7"},
  139. {KEY_LAUNCH8 ,"Launch8"},
  140. {KEY_LAUNCH9 ,"Launch9"},
  141. {KEY_LAUNCHA ,"LaunchA"},
  142. {KEY_LAUNCHB ,"LaunchB"},
  143. {KEY_LAUNCHC ,"LaunchC"},
  144. {KEY_LAUNCHD ,"LaunchD"},
  145. {KEY_LAUNCHE ,"LaunchE"},
  146. {KEY_LAUNCHF ,"LaunchF"},
  147. {KEY_UNKNOWN ,"Unknown"},
  148. {KEY_SPACE ,"Space"},
  149. {KEY_EXCLAM ,"Exclam"},
  150. {KEY_QUOTEDBL ,"QuoteDbl"},
  151. {KEY_NUMBERSIGN ,"NumberSign"},
  152. {KEY_DOLLAR ,"Dollar"},
  153. {KEY_PERCENT ,"Percent"},
  154. {KEY_AMPERSAND ,"Ampersand"},
  155. {KEY_APOSTROPHE ,"Apostrophe"},
  156. {KEY_PARENLEFT ,"ParenLeft"},
  157. {KEY_PARENRIGHT ,"ParenRight"},
  158. {KEY_ASTERISK ,"Asterisk"},
  159. {KEY_PLUS ,"Plus"},
  160. {KEY_COMMA ,"Comma"},
  161. {KEY_MINUS ,"Minus"},
  162. {KEY_PERIOD ,"Period"},
  163. {KEY_SLASH ,"Slash"},
  164. {KEY_0 ,"0"},
  165. {KEY_1 ,"1"},
  166. {KEY_2 ,"2"},
  167. {KEY_3 ,"3"},
  168. {KEY_4 ,"4"},
  169. {KEY_5 ,"5"},
  170. {KEY_6 ,"6"},
  171. {KEY_7 ,"7"},
  172. {KEY_8 ,"8"},
  173. {KEY_9 ,"9"},
  174. {KEY_COLON ,"Colon"},
  175. {KEY_SEMICOLON ,"Semicolon"},
  176. {KEY_LESS ,"Less"},
  177. {KEY_EQUAL ,"Equal"},
  178. {KEY_GREATER ,"Greater"},
  179. {KEY_QUESTION ,"Question"},
  180. {KEY_AT ,"At"},
  181. {KEY_A ,"A"},
  182. {KEY_B ,"B"},
  183. {KEY_C ,"C"},
  184. {KEY_D ,"D"},
  185. {KEY_E ,"E"},
  186. {KEY_F ,"F"},
  187. {KEY_G ,"G"},
  188. {KEY_H ,"H"},
  189. {KEY_I ,"I"},
  190. {KEY_J ,"J"},
  191. {KEY_K ,"K"},
  192. {KEY_L ,"L"},
  193. {KEY_M ,"M"},
  194. {KEY_N ,"N"},
  195. {KEY_O ,"O"},
  196. {KEY_P ,"P"},
  197. {KEY_Q ,"Q"},
  198. {KEY_R ,"R"},
  199. {KEY_S ,"S"},
  200. {KEY_T ,"T"},
  201. {KEY_U ,"U"},
  202. {KEY_V ,"V"},
  203. {KEY_W ,"W"},
  204. {KEY_X ,"X"},
  205. {KEY_Y ,"Y"},
  206. {KEY_Z ,"Z"},
  207. {KEY_BRACKETLEFT ,"BracketLeft"},
  208. {KEY_BACKSLASH ,"BackSlash"},
  209. {KEY_BRACKETRIGHT ,"BracketRight"},
  210. {KEY_ASCIICIRCUM ,"AsciiCircum"},
  211. {KEY_UNDERSCORE ,"UnderScore"},
  212. {KEY_QUOTELEFT ,"QuoteLeft"},
  213. {KEY_BRACELEFT ,"BraceLeft"},
  214. {KEY_BAR ,"Bar"},
  215. {KEY_BRACERIGHT ,"BraceRight"},
  216. {KEY_ASCIITILDE ,"AsciiTilde"},
  217. {KEY_NOBREAKSPACE ,"NoBreakSpace"},
  218. {KEY_EXCLAMDOWN ,"ExclamDown"},
  219. {KEY_CENT ,"Cent"},
  220. {KEY_STERLING ,"Sterling"},
  221. {KEY_CURRENCY ,"Currency"},
  222. {KEY_YEN ,"Yen"},
  223. {KEY_BROKENBAR ,"BrokenBar"},
  224. {KEY_SECTION ,"Section"},
  225. {KEY_DIAERESIS ,"Diaeresis"},
  226. {KEY_COPYRIGHT ,"Copyright"},
  227. {KEY_ORDFEMININE ,"Ordfeminine"},
  228. {KEY_GUILLEMOTLEFT ,"GuillemotLeft"},
  229. {KEY_NOTSIGN ,"NotSign"},
  230. {KEY_HYPHEN ,"Hyphen"},
  231. {KEY_REGISTERED ,"Registered"},
  232. {KEY_MACRON ,"Macron"},
  233. {KEY_DEGREE ,"Degree"},
  234. {KEY_PLUSMINUS ,"PlusMinus"},
  235. {KEY_TWOSUPERIOR ,"TwoSuperior"},
  236. {KEY_THREESUPERIOR ,"ThreeSuperior"},
  237. {KEY_ACUTE ,"Acute"},
  238. {KEY_MU ,"Mu"},
  239. {KEY_PARAGRAPH ,"Paragraph"},
  240. {KEY_PERIODCENTERED ,"PeriodCentered"},
  241. {KEY_CEDILLA ,"Cedilla"},
  242. {KEY_ONESUPERIOR ,"OneSuperior"},
  243. {KEY_MASCULINE ,"Masculine"},
  244. {KEY_GUILLEMOTRIGHT ,"GuillemotRight"},
  245. {KEY_ONEQUARTER ,"OneQuarter"},
  246. {KEY_ONEHALF ,"OneHalf"},
  247. {KEY_THREEQUARTERS ,"ThreeQuarters"},
  248. {KEY_QUESTIONDOWN ,"QuestionDown"},
  249. {KEY_AGRAVE ,"Agrave"},
  250. {KEY_AACUTE ,"Aacute"},
  251. {KEY_ACIRCUMFLEX ,"AcircumFlex"},
  252. {KEY_ATILDE ,"Atilde"},
  253. {KEY_ADIAERESIS ,"Adiaeresis"},
  254. {KEY_ARING ,"Aring"},
  255. {KEY_AE ,"Ae"},
  256. {KEY_CCEDILLA ,"Ccedilla"},
  257. {KEY_EGRAVE ,"Egrave"},
  258. {KEY_EACUTE ,"Eacute"},
  259. {KEY_ECIRCUMFLEX ,"Ecircumflex"},
  260. {KEY_EDIAERESIS ,"Ediaeresis"},
  261. {KEY_IGRAVE ,"Igrave"},
  262. {KEY_IACUTE ,"Iacute"},
  263. {KEY_ICIRCUMFLEX ,"Icircumflex"},
  264. {KEY_IDIAERESIS ,"Idiaeresis"},
  265. {KEY_ETH ,"Eth"},
  266. {KEY_NTILDE ,"Ntilde"},
  267. {KEY_OGRAVE ,"Ograve"},
  268. {KEY_OACUTE ,"Oacute"},
  269. {KEY_OCIRCUMFLEX ,"Ocircumflex"},
  270. {KEY_OTILDE ,"Otilde"},
  271. {KEY_ODIAERESIS ,"Odiaeresis"},
  272. {KEY_MULTIPLY ,"Multiply"},
  273. {KEY_OOBLIQUE ,"Ooblique"},
  274. {KEY_UGRAVE ,"Ugrave"},
  275. {KEY_UACUTE ,"Uacute"},
  276. {KEY_UCIRCUMFLEX ,"Ucircumflex"},
  277. {KEY_UDIAERESIS ,"Udiaeresis"},
  278. {KEY_YACUTE ,"Yacute"},
  279. {KEY_THORN ,"Thorn"},
  280. {KEY_SSHARP ,"Ssharp"},
  281. {KEY_DIVISION ,"Division"},
  282. {KEY_YDIAERESIS ,"Ydiaeresis"},
  283. {0 ,0}
  284. /* clang-format on */
  285. };
  286. bool keycode_has_unicode(uint32_t p_keycode) {
  287. switch (p_keycode) {
  288. case KEY_ESCAPE:
  289. case KEY_TAB:
  290. case KEY_BACKTAB:
  291. case KEY_BACKSPACE:
  292. case KEY_ENTER:
  293. case KEY_KP_ENTER:
  294. case KEY_INSERT:
  295. case KEY_DELETE:
  296. case KEY_PAUSE:
  297. case KEY_PRINT:
  298. case KEY_SYSREQ:
  299. case KEY_CLEAR:
  300. case KEY_HOME:
  301. case KEY_END:
  302. case KEY_LEFT:
  303. case KEY_UP:
  304. case KEY_RIGHT:
  305. case KEY_DOWN:
  306. case KEY_PAGEUP:
  307. case KEY_PAGEDOWN:
  308. case KEY_SHIFT:
  309. case KEY_CONTROL:
  310. case KEY_META:
  311. case KEY_ALT:
  312. case KEY_CAPSLOCK:
  313. case KEY_NUMLOCK:
  314. case KEY_SCROLLLOCK:
  315. case KEY_F1:
  316. case KEY_F2:
  317. case KEY_F3:
  318. case KEY_F4:
  319. case KEY_F5:
  320. case KEY_F6:
  321. case KEY_F7:
  322. case KEY_F8:
  323. case KEY_F9:
  324. case KEY_F10:
  325. case KEY_F11:
  326. case KEY_F12:
  327. case KEY_F13:
  328. case KEY_F14:
  329. case KEY_F15:
  330. case KEY_F16:
  331. case KEY_SUPER_L:
  332. case KEY_SUPER_R:
  333. case KEY_MENU:
  334. case KEY_HYPER_L:
  335. case KEY_HYPER_R:
  336. case KEY_HELP:
  337. case KEY_DIRECTION_L:
  338. case KEY_DIRECTION_R:
  339. case KEY_BACK:
  340. case KEY_FORWARD:
  341. case KEY_STOP:
  342. case KEY_REFRESH:
  343. case KEY_VOLUMEDOWN:
  344. case KEY_VOLUMEMUTE:
  345. case KEY_VOLUMEUP:
  346. case KEY_BASSBOOST:
  347. case KEY_BASSUP:
  348. case KEY_BASSDOWN:
  349. case KEY_TREBLEUP:
  350. case KEY_TREBLEDOWN:
  351. case KEY_MEDIAPLAY:
  352. case KEY_MEDIASTOP:
  353. case KEY_MEDIAPREVIOUS:
  354. case KEY_MEDIANEXT:
  355. case KEY_MEDIARECORD:
  356. case KEY_HOMEPAGE:
  357. case KEY_FAVORITES:
  358. case KEY_SEARCH:
  359. case KEY_STANDBY:
  360. case KEY_OPENURL:
  361. case KEY_LAUNCHMAIL:
  362. case KEY_LAUNCHMEDIA:
  363. case KEY_LAUNCH0:
  364. case KEY_LAUNCH1:
  365. case KEY_LAUNCH2:
  366. case KEY_LAUNCH3:
  367. case KEY_LAUNCH4:
  368. case KEY_LAUNCH5:
  369. case KEY_LAUNCH6:
  370. case KEY_LAUNCH7:
  371. case KEY_LAUNCH8:
  372. case KEY_LAUNCH9:
  373. case KEY_LAUNCHA:
  374. case KEY_LAUNCHB:
  375. case KEY_LAUNCHC:
  376. case KEY_LAUNCHD:
  377. case KEY_LAUNCHE:
  378. case KEY_LAUNCHF:
  379. return false;
  380. }
  381. return true;
  382. }
  383. String keycode_get_string(uint32_t p_code) {
  384. String codestr;
  385. if (p_code & KEY_MASK_SHIFT) {
  386. codestr += find_keycode_name(KEY_SHIFT);
  387. codestr += "+";
  388. }
  389. if (p_code & KEY_MASK_ALT) {
  390. codestr += find_keycode_name(KEY_ALT);
  391. codestr += "+";
  392. }
  393. if (p_code & KEY_MASK_CTRL) {
  394. codestr += find_keycode_name(KEY_CONTROL);
  395. codestr += "+";
  396. }
  397. if (p_code & KEY_MASK_META) {
  398. codestr += find_keycode_name(KEY_META);
  399. codestr += "+";
  400. }
  401. p_code &= KEY_CODE_MASK;
  402. const _KeyCodeText *kct = &_keycodes[0];
  403. while (kct->text) {
  404. if (kct->code == (int)p_code) {
  405. codestr += kct->text;
  406. return codestr;
  407. }
  408. kct++;
  409. }
  410. codestr += String::chr(p_code);
  411. return codestr;
  412. }
  413. int find_keycode(const String &p_code) {
  414. const _KeyCodeText *kct = &_keycodes[0];
  415. while (kct->text) {
  416. if (p_code.nocasecmp_to(kct->text) == 0) {
  417. return kct->code;
  418. }
  419. kct++;
  420. }
  421. return 0;
  422. }
  423. const char *find_keycode_name(int p_keycode) {
  424. const _KeyCodeText *kct = &_keycodes[0];
  425. while (kct->text) {
  426. if (kct->code == p_keycode) {
  427. return kct->text;
  428. }
  429. kct++;
  430. }
  431. return "";
  432. }
  433. int keycode_get_count() {
  434. const _KeyCodeText *kct = &_keycodes[0];
  435. int count = 0;
  436. while (kct->text) {
  437. count++;
  438. kct++;
  439. }
  440. return count;
  441. }
  442. int keycode_get_value_by_index(int p_index) {
  443. return _keycodes[p_index].code;
  444. }
  445. const char *keycode_get_name_by_index(int p_index) {
  446. return _keycodes[p_index].text;
  447. }