keyboard.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*************************************************************************/
  2. /* keyboard.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. Key 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::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::CTRL ,"Ctrl"},
  60. #if defined(MACOS_ENABLED)
  61. {Key::META ,"Command"},
  62. {Key::ALT ,"Option"},
  63. #elif defined(WINDOWS_ENABLED)
  64. {Key::META ,"Windows"},
  65. {Key::ALT ,"Alt"},
  66. #else
  67. {Key::META ,"Meta"},
  68. {Key::ALT ,"Alt"},
  69. #endif
  70. {Key::CAPSLOCK ,"CapsLock"},
  71. {Key::NUMLOCK ,"NumLock"},
  72. {Key::SCROLLLOCK ,"ScrollLock"},
  73. {Key::F1 ,"F1"},
  74. {Key::F2 ,"F2"},
  75. {Key::F3 ,"F3"},
  76. {Key::F4 ,"F4"},
  77. {Key::F5 ,"F5"},
  78. {Key::F6 ,"F6"},
  79. {Key::F7 ,"F7"},
  80. {Key::F8 ,"F8"},
  81. {Key::F9 ,"F9"},
  82. {Key::F10 ,"F10"},
  83. {Key::F11 ,"F11"},
  84. {Key::F12 ,"F12"},
  85. {Key::F13 ,"F13"},
  86. {Key::F14 ,"F14"},
  87. {Key::F15 ,"F15"},
  88. {Key::F16 ,"F16"},
  89. {Key::F17 ,"F17"},
  90. {Key::F18 ,"F18"},
  91. {Key::F19 ,"F19"},
  92. {Key::F20 ,"F20"},
  93. {Key::F21 ,"F21"},
  94. {Key::F22 ,"F22"},
  95. {Key::F23 ,"F23"},
  96. {Key::F24 ,"F24"},
  97. {Key::F25 ,"F25"},
  98. {Key::F26 ,"F26"},
  99. {Key::F27 ,"F27"},
  100. {Key::F28 ,"F28"},
  101. {Key::F29 ,"F29"},
  102. {Key::F30 ,"F30"},
  103. {Key::F31 ,"F31"},
  104. {Key::F32 ,"F32"},
  105. {Key::F33 ,"F33"},
  106. {Key::F34 ,"F34"},
  107. {Key::F35 ,"F35"},
  108. {Key::KP_MULTIPLY ,"Kp Multiply"},
  109. {Key::KP_DIVIDE ,"Kp Divide"},
  110. {Key::KP_SUBTRACT ,"Kp Subtract"},
  111. {Key::KP_PERIOD ,"Kp Period"},
  112. {Key::KP_ADD ,"Kp Add"},
  113. {Key::KP_0 ,"Kp 0"},
  114. {Key::KP_1 ,"Kp 1"},
  115. {Key::KP_2 ,"Kp 2"},
  116. {Key::KP_3 ,"Kp 3"},
  117. {Key::KP_4 ,"Kp 4"},
  118. {Key::KP_5 ,"Kp 5"},
  119. {Key::KP_6 ,"Kp 6"},
  120. {Key::KP_7 ,"Kp 7"},
  121. {Key::KP_8 ,"Kp 8"},
  122. {Key::KP_9 ,"Kp 9"},
  123. {Key::SUPER_L ,"Super L"},
  124. {Key::SUPER_R ,"Super R"},
  125. {Key::MENU ,"Menu"},
  126. {Key::HYPER_L ,"Hyper L"},
  127. {Key::HYPER_R ,"Hyper R"},
  128. {Key::HELP ,"Help"},
  129. {Key::DIRECTION_L ,"Direction L"},
  130. {Key::DIRECTION_R ,"Direction R"},
  131. {Key::BACK ,"Back"},
  132. {Key::FORWARD ,"Forward"},
  133. {Key::STOP ,"Stop"},
  134. {Key::REFRESH ,"Refresh"},
  135. {Key::VOLUMEDOWN ,"VolumeDown"},
  136. {Key::VOLUMEMUTE ,"VolumeMute"},
  137. {Key::VOLUMEUP ,"VolumeUp"},
  138. {Key::BASSBOOST ,"BassBoost"},
  139. {Key::BASSUP ,"BassUp"},
  140. {Key::BASSDOWN ,"BassDown"},
  141. {Key::TREBLEUP ,"TrebleUp"},
  142. {Key::TREBLEDOWN ,"TrebleDown"},
  143. {Key::MEDIAPLAY ,"MediaPlay"},
  144. {Key::MEDIASTOP ,"MediaStop"},
  145. {Key::MEDIAPREVIOUS ,"MediaPrevious"},
  146. {Key::MEDIANEXT ,"MediaNext"},
  147. {Key::MEDIARECORD ,"MediaRecord"},
  148. {Key::HOMEPAGE ,"HomePage"},
  149. {Key::FAVORITES ,"Favorites"},
  150. {Key::SEARCH ,"Search"},
  151. {Key::STANDBY ,"StandBy"},
  152. {Key::LAUNCHMAIL ,"LaunchMail"},
  153. {Key::LAUNCHMEDIA ,"LaunchMedia"},
  154. {Key::LAUNCH0 ,"Launch0"},
  155. {Key::LAUNCH1 ,"Launch1"},
  156. {Key::LAUNCH2 ,"Launch2"},
  157. {Key::LAUNCH3 ,"Launch3"},
  158. {Key::LAUNCH4 ,"Launch4"},
  159. {Key::LAUNCH5 ,"Launch5"},
  160. {Key::LAUNCH6 ,"Launch6"},
  161. {Key::LAUNCH7 ,"Launch7"},
  162. {Key::LAUNCH8 ,"Launch8"},
  163. {Key::LAUNCH9 ,"Launch9"},
  164. {Key::LAUNCHA ,"LaunchA"},
  165. {Key::LAUNCHB ,"LaunchB"},
  166. {Key::LAUNCHC ,"LaunchC"},
  167. {Key::LAUNCHD ,"LaunchD"},
  168. {Key::LAUNCHE ,"LaunchE"},
  169. {Key::LAUNCHF ,"LaunchF"},
  170. {Key::UNKNOWN ,"Unknown"},
  171. {Key::SPACE ,"Space"},
  172. {Key::EXCLAM ,"Exclam"},
  173. {Key::QUOTEDBL ,"QuoteDbl"},
  174. {Key::NUMBERSIGN ,"NumberSign"},
  175. {Key::DOLLAR ,"Dollar"},
  176. {Key::PERCENT ,"Percent"},
  177. {Key::AMPERSAND ,"Ampersand"},
  178. {Key::APOSTROPHE ,"Apostrophe"},
  179. {Key::PARENLEFT ,"ParenLeft"},
  180. {Key::PARENRIGHT ,"ParenRight"},
  181. {Key::ASTERISK ,"Asterisk"},
  182. {Key::PLUS ,"Plus"},
  183. {Key::COMMA ,"Comma"},
  184. {Key::MINUS ,"Minus"},
  185. {Key::PERIOD ,"Period"},
  186. {Key::SLASH ,"Slash"},
  187. {Key::KEY_0 ,"0"},
  188. {Key::KEY_1 ,"1"},
  189. {Key::KEY_2 ,"2"},
  190. {Key::KEY_3 ,"3"},
  191. {Key::KEY_4 ,"4"},
  192. {Key::KEY_5 ,"5"},
  193. {Key::KEY_6 ,"6"},
  194. {Key::KEY_7 ,"7"},
  195. {Key::KEY_8 ,"8"},
  196. {Key::KEY_9 ,"9"},
  197. {Key::COLON ,"Colon"},
  198. {Key::SEMICOLON ,"Semicolon"},
  199. {Key::LESS ,"Less"},
  200. {Key::EQUAL ,"Equal"},
  201. {Key::GREATER ,"Greater"},
  202. {Key::QUESTION ,"Question"},
  203. {Key::AT ,"At"},
  204. {Key::A ,"A"},
  205. {Key::B ,"B"},
  206. {Key::C ,"C"},
  207. {Key::D ,"D"},
  208. {Key::E ,"E"},
  209. {Key::F ,"F"},
  210. {Key::G ,"G"},
  211. {Key::H ,"H"},
  212. {Key::I ,"I"},
  213. {Key::J ,"J"},
  214. {Key::K ,"K"},
  215. {Key::L ,"L"},
  216. {Key::M ,"M"},
  217. {Key::N ,"N"},
  218. {Key::O ,"O"},
  219. {Key::P ,"P"},
  220. {Key::Q ,"Q"},
  221. {Key::R ,"R"},
  222. {Key::S ,"S"},
  223. {Key::T ,"T"},
  224. {Key::U ,"U"},
  225. {Key::V ,"V"},
  226. {Key::W ,"W"},
  227. {Key::X ,"X"},
  228. {Key::Y ,"Y"},
  229. {Key::Z ,"Z"},
  230. {Key::BRACKETLEFT ,"BracketLeft"},
  231. {Key::BACKSLASH ,"BackSlash"},
  232. {Key::BRACKETRIGHT ,"BracketRight"},
  233. {Key::ASCIICIRCUM ,"AsciiCircum"},
  234. {Key::UNDERSCORE ,"UnderScore"},
  235. {Key::QUOTELEFT ,"QuoteLeft"},
  236. {Key::BRACELEFT ,"BraceLeft"},
  237. {Key::BAR ,"Bar"},
  238. {Key::BRACERIGHT ,"BraceRight"},
  239. {Key::ASCIITILDE ,"AsciiTilde"},
  240. {Key::NOBREAKSPACE ,"NoBreakSpace"},
  241. {Key::EXCLAMDOWN ,"ExclamDown"},
  242. {Key::CENT ,"Cent"},
  243. {Key::STERLING ,"Sterling"},
  244. {Key::CURRENCY ,"Currency"},
  245. {Key::YEN ,"Yen"},
  246. {Key::BROKENBAR ,"BrokenBar"},
  247. {Key::SECTION ,"Section"},
  248. {Key::DIAERESIS ,"Diaeresis"},
  249. {Key::COPYRIGHT ,"Copyright"},
  250. {Key::ORDFEMININE ,"Ordfeminine"},
  251. {Key::GUILLEMOTLEFT ,"GuillemotLeft"},
  252. {Key::NOTSIGN ,"NotSign"},
  253. {Key::HYPHEN ,"Hyphen"},
  254. {Key::KEY_REGISTERED ,"Registered"},
  255. {Key::MACRON ,"Macron"},
  256. {Key::DEGREE ,"Degree"},
  257. {Key::PLUSMINUS ,"PlusMinus"},
  258. {Key::TWOSUPERIOR ,"TwoSuperior"},
  259. {Key::THREESUPERIOR ,"ThreeSuperior"},
  260. {Key::ACUTE ,"Acute"},
  261. {Key::MU ,"Mu"},
  262. {Key::PARAGRAPH ,"Paragraph"},
  263. {Key::PERIODCENTERED ,"PeriodCentered"},
  264. {Key::CEDILLA ,"Cedilla"},
  265. {Key::ONESUPERIOR ,"OneSuperior"},
  266. {Key::MASCULINE ,"Masculine"},
  267. {Key::GUILLEMOTRIGHT ,"GuillemotRight"},
  268. {Key::ONEQUARTER ,"OneQuarter"},
  269. {Key::ONEHALF ,"OneHalf"},
  270. {Key::THREEQUARTERS ,"ThreeQuarters"},
  271. {Key::QUESTIONDOWN ,"QuestionDown"},
  272. {Key::AGRAVE ,"Agrave"},
  273. {Key::AACUTE ,"Aacute"},
  274. {Key::ACIRCUMFLEX ,"AcircumFlex"},
  275. {Key::ATILDE ,"Atilde"},
  276. {Key::ADIAERESIS ,"Adiaeresis"},
  277. {Key::ARING ,"Aring"},
  278. {Key::AE ,"Ae"},
  279. {Key::CCEDILLA ,"Ccedilla"},
  280. {Key::EGRAVE ,"Egrave"},
  281. {Key::EACUTE ,"Eacute"},
  282. {Key::ECIRCUMFLEX ,"Ecircumflex"},
  283. {Key::EDIAERESIS ,"Ediaeresis"},
  284. {Key::IGRAVE ,"Igrave"},
  285. {Key::IACUTE ,"Iacute"},
  286. {Key::ICIRCUMFLEX ,"Icircumflex"},
  287. {Key::IDIAERESIS ,"Idiaeresis"},
  288. {Key::ETH ,"Eth"},
  289. {Key::NTILDE ,"Ntilde"},
  290. {Key::OGRAVE ,"Ograve"},
  291. {Key::OACUTE ,"Oacute"},
  292. {Key::OCIRCUMFLEX ,"Ocircumflex"},
  293. {Key::OTILDE ,"Otilde"},
  294. {Key::ODIAERESIS ,"Odiaeresis"},
  295. {Key::MULTIPLY ,"Multiply"},
  296. {Key::OOBLIQUE ,"Ooblique"},
  297. {Key::UGRAVE ,"Ugrave"},
  298. {Key::UACUTE ,"Uacute"},
  299. {Key::UCIRCUMFLEX ,"Ucircumflex"},
  300. {Key::UDIAERESIS ,"Udiaeresis"},
  301. {Key::YACUTE ,"Yacute"},
  302. {Key::THORN ,"Thorn"},
  303. {Key::SSHARP ,"Ssharp"},
  304. {Key::DIVISION ,"Division"},
  305. {Key::YDIAERESIS ,"Ydiaeresis"},
  306. {Key::NONE ,nullptr}
  307. /* clang-format on */
  308. };
  309. bool keycode_has_unicode(Key p_keycode) {
  310. switch (p_keycode) {
  311. case Key::ESCAPE:
  312. case Key::TAB:
  313. case Key::BACKTAB:
  314. case Key::BACKSPACE:
  315. case Key::ENTER:
  316. case Key::KP_ENTER:
  317. case Key::INSERT:
  318. case Key::KEY_DELETE:
  319. case Key::PAUSE:
  320. case Key::PRINT:
  321. case Key::SYSREQ:
  322. case Key::CLEAR:
  323. case Key::HOME:
  324. case Key::END:
  325. case Key::LEFT:
  326. case Key::UP:
  327. case Key::RIGHT:
  328. case Key::DOWN:
  329. case Key::PAGEUP:
  330. case Key::PAGEDOWN:
  331. case Key::SHIFT:
  332. case Key::CTRL:
  333. case Key::META:
  334. case Key::ALT:
  335. case Key::CAPSLOCK:
  336. case Key::NUMLOCK:
  337. case Key::SCROLLLOCK:
  338. case Key::F1:
  339. case Key::F2:
  340. case Key::F3:
  341. case Key::F4:
  342. case Key::F5:
  343. case Key::F6:
  344. case Key::F7:
  345. case Key::F8:
  346. case Key::F9:
  347. case Key::F10:
  348. case Key::F11:
  349. case Key::F12:
  350. case Key::F13:
  351. case Key::F14:
  352. case Key::F15:
  353. case Key::F16:
  354. case Key::F17:
  355. case Key::F18:
  356. case Key::F19:
  357. case Key::F20:
  358. case Key::F21:
  359. case Key::F22:
  360. case Key::F23:
  361. case Key::F24:
  362. case Key::F25:
  363. case Key::F26:
  364. case Key::F27:
  365. case Key::F28:
  366. case Key::F29:
  367. case Key::F30:
  368. case Key::F31:
  369. case Key::F32:
  370. case Key::F33:
  371. case Key::F34:
  372. case Key::F35:
  373. case Key::SUPER_L:
  374. case Key::SUPER_R:
  375. case Key::MENU:
  376. case Key::HYPER_L:
  377. case Key::HYPER_R:
  378. case Key::HELP:
  379. case Key::DIRECTION_L:
  380. case Key::DIRECTION_R:
  381. case Key::BACK:
  382. case Key::FORWARD:
  383. case Key::STOP:
  384. case Key::REFRESH:
  385. case Key::VOLUMEDOWN:
  386. case Key::VOLUMEMUTE:
  387. case Key::VOLUMEUP:
  388. case Key::BASSBOOST:
  389. case Key::BASSUP:
  390. case Key::BASSDOWN:
  391. case Key::TREBLEUP:
  392. case Key::TREBLEDOWN:
  393. case Key::MEDIAPLAY:
  394. case Key::MEDIASTOP:
  395. case Key::MEDIAPREVIOUS:
  396. case Key::MEDIANEXT:
  397. case Key::MEDIARECORD:
  398. case Key::HOMEPAGE:
  399. case Key::FAVORITES:
  400. case Key::SEARCH:
  401. case Key::STANDBY:
  402. case Key::OPENURL:
  403. case Key::LAUNCHMAIL:
  404. case Key::LAUNCHMEDIA:
  405. case Key::LAUNCH0:
  406. case Key::LAUNCH1:
  407. case Key::LAUNCH2:
  408. case Key::LAUNCH3:
  409. case Key::LAUNCH4:
  410. case Key::LAUNCH5:
  411. case Key::LAUNCH6:
  412. case Key::LAUNCH7:
  413. case Key::LAUNCH8:
  414. case Key::LAUNCH9:
  415. case Key::LAUNCHA:
  416. case Key::LAUNCHB:
  417. case Key::LAUNCHC:
  418. case Key::LAUNCHD:
  419. case Key::LAUNCHE:
  420. case Key::LAUNCHF:
  421. return false;
  422. default: {
  423. }
  424. }
  425. return true;
  426. }
  427. String keycode_get_string(Key p_code) {
  428. String codestr;
  429. if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) {
  430. codestr += find_keycode_name(Key::SHIFT);
  431. codestr += "+";
  432. }
  433. if ((p_code & KeyModifierMask::ALT) != Key::NONE) {
  434. codestr += find_keycode_name(Key::ALT);
  435. codestr += "+";
  436. }
  437. if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
  438. #ifdef MACOS_ENABLED
  439. codestr += find_keycode_name(Key::META);
  440. #else
  441. codestr += find_keycode_name(Key::CTRL);
  442. #endif
  443. codestr += "+";
  444. }
  445. if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
  446. codestr += find_keycode_name(Key::CTRL);
  447. codestr += "+";
  448. }
  449. if ((p_code & KeyModifierMask::META) != Key::NONE) {
  450. codestr += find_keycode_name(Key::META);
  451. codestr += "+";
  452. }
  453. p_code &= KeyModifierMask::CODE_MASK;
  454. const _KeyCodeText *kct = &_keycodes[0];
  455. while (kct->text) {
  456. if (kct->code == p_code) {
  457. codestr += kct->text;
  458. return codestr;
  459. }
  460. kct++;
  461. }
  462. codestr += String::chr((char32_t)p_code);
  463. return codestr;
  464. }
  465. Key find_keycode(const String &p_code) {
  466. const _KeyCodeText *kct = &_keycodes[0];
  467. while (kct->text) {
  468. if (p_code.nocasecmp_to(kct->text) == 0) {
  469. return kct->code;
  470. }
  471. kct++;
  472. }
  473. return Key::NONE;
  474. }
  475. const char *find_keycode_name(Key p_keycode) {
  476. const _KeyCodeText *kct = &_keycodes[0];
  477. while (kct->text) {
  478. if (kct->code == p_keycode) {
  479. return kct->text;
  480. }
  481. kct++;
  482. }
  483. return "";
  484. }
  485. int keycode_get_count() {
  486. const _KeyCodeText *kct = &_keycodes[0];
  487. int count = 0;
  488. while (kct->text) {
  489. count++;
  490. kct++;
  491. }
  492. return count;
  493. }
  494. int keycode_get_value_by_index(int p_index) {
  495. return (int)_keycodes[p_index].code;
  496. }
  497. const char *keycode_get_name_by_index(int p_index) {
  498. return _keycodes[p_index].text;
  499. }