keyboard.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /**************************************************************************/
  2. /* keyboard.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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::CMD_OR_CTRL ,"Command"},
  63. {Key::ALT ,"Option"},
  64. #elif defined(WINDOWS_ENABLED)
  65. {Key::META ,"Windows"},
  66. {Key::CMD_OR_CTRL ,"Ctrl"},
  67. {Key::ALT ,"Alt"},
  68. #else
  69. {Key::META ,"Meta"},
  70. {Key::CMD_OR_CTRL ,"Ctrl"},
  71. {Key::ALT ,"Alt"},
  72. #endif
  73. {Key::CAPSLOCK ,"CapsLock"},
  74. {Key::NUMLOCK ,"NumLock"},
  75. {Key::SCROLLLOCK ,"ScrollLock"},
  76. {Key::F1 ,"F1"},
  77. {Key::F2 ,"F2"},
  78. {Key::F3 ,"F3"},
  79. {Key::F4 ,"F4"},
  80. {Key::F5 ,"F5"},
  81. {Key::F6 ,"F6"},
  82. {Key::F7 ,"F7"},
  83. {Key::F8 ,"F8"},
  84. {Key::F9 ,"F9"},
  85. {Key::F10 ,"F10"},
  86. {Key::F11 ,"F11"},
  87. {Key::F12 ,"F12"},
  88. {Key::F13 ,"F13"},
  89. {Key::F14 ,"F14"},
  90. {Key::F15 ,"F15"},
  91. {Key::F16 ,"F16"},
  92. {Key::F17 ,"F17"},
  93. {Key::F18 ,"F18"},
  94. {Key::F19 ,"F19"},
  95. {Key::F20 ,"F20"},
  96. {Key::F21 ,"F21"},
  97. {Key::F22 ,"F22"},
  98. {Key::F23 ,"F23"},
  99. {Key::F24 ,"F24"},
  100. {Key::F25 ,"F25"},
  101. {Key::F26 ,"F26"},
  102. {Key::F27 ,"F27"},
  103. {Key::F28 ,"F28"},
  104. {Key::F29 ,"F29"},
  105. {Key::F30 ,"F30"},
  106. {Key::F31 ,"F31"},
  107. {Key::F32 ,"F32"},
  108. {Key::F33 ,"F33"},
  109. {Key::F34 ,"F34"},
  110. {Key::F35 ,"F35"},
  111. {Key::KP_MULTIPLY ,"Kp Multiply"},
  112. {Key::KP_DIVIDE ,"Kp Divide"},
  113. {Key::KP_SUBTRACT ,"Kp Subtract"},
  114. {Key::KP_PERIOD ,"Kp Period"},
  115. {Key::KP_ADD ,"Kp Add"},
  116. {Key::KP_0 ,"Kp 0"},
  117. {Key::KP_1 ,"Kp 1"},
  118. {Key::KP_2 ,"Kp 2"},
  119. {Key::KP_3 ,"Kp 3"},
  120. {Key::KP_4 ,"Kp 4"},
  121. {Key::KP_5 ,"Kp 5"},
  122. {Key::KP_6 ,"Kp 6"},
  123. {Key::KP_7 ,"Kp 7"},
  124. {Key::KP_8 ,"Kp 8"},
  125. {Key::KP_9 ,"Kp 9"},
  126. {Key::MENU ,"Menu"},
  127. {Key::HYPER ,"Hyper"},
  128. {Key::HELP ,"Help"},
  129. {Key::BACK ,"Back"},
  130. {Key::FORWARD ,"Forward"},
  131. {Key::STOP ,"Stop"},
  132. {Key::REFRESH ,"Refresh"},
  133. {Key::VOLUMEDOWN ,"VolumeDown"},
  134. {Key::VOLUMEMUTE ,"VolumeMute"},
  135. {Key::VOLUMEUP ,"VolumeUp"},
  136. {Key::MEDIAPLAY ,"MediaPlay"},
  137. {Key::MEDIASTOP ,"MediaStop"},
  138. {Key::MEDIAPREVIOUS ,"MediaPrevious"},
  139. {Key::MEDIANEXT ,"MediaNext"},
  140. {Key::MEDIARECORD ,"MediaRecord"},
  141. {Key::HOMEPAGE ,"HomePage"},
  142. {Key::FAVORITES ,"Favorites"},
  143. {Key::SEARCH ,"Search"},
  144. {Key::STANDBY ,"StandBy"},
  145. {Key::OPENURL ,"OpenURL"},
  146. {Key::LAUNCHMAIL ,"LaunchMail"},
  147. {Key::LAUNCHMEDIA ,"LaunchMedia"},
  148. {Key::LAUNCH0 ,"Launch0"},
  149. {Key::LAUNCH1 ,"Launch1"},
  150. {Key::LAUNCH2 ,"Launch2"},
  151. {Key::LAUNCH3 ,"Launch3"},
  152. {Key::LAUNCH4 ,"Launch4"},
  153. {Key::LAUNCH5 ,"Launch5"},
  154. {Key::LAUNCH6 ,"Launch6"},
  155. {Key::LAUNCH7 ,"Launch7"},
  156. {Key::LAUNCH8 ,"Launch8"},
  157. {Key::LAUNCH9 ,"Launch9"},
  158. {Key::LAUNCHA ,"LaunchA"},
  159. {Key::LAUNCHB ,"LaunchB"},
  160. {Key::LAUNCHC ,"LaunchC"},
  161. {Key::LAUNCHD ,"LaunchD"},
  162. {Key::LAUNCHE ,"LaunchE"},
  163. {Key::LAUNCHF ,"LaunchF"},
  164. {Key::GLOBE ,"Globe"},
  165. {Key::KEYBOARD ,"On-screen keyboard"},
  166. {Key::JIS_EISU ,"JIS Eisu"},
  167. {Key::JIS_KANA ,"JIS Kana"},
  168. {Key::UNKNOWN ,"Unknown"},
  169. {Key::SPACE ,"Space"},
  170. {Key::EXCLAM ,"Exclam"},
  171. {Key::QUOTEDBL ,"QuoteDbl"},
  172. {Key::NUMBERSIGN ,"NumberSign"},
  173. {Key::DOLLAR ,"Dollar"},
  174. {Key::PERCENT ,"Percent"},
  175. {Key::AMPERSAND ,"Ampersand"},
  176. {Key::APOSTROPHE ,"Apostrophe"},
  177. {Key::PARENLEFT ,"ParenLeft"},
  178. {Key::PARENRIGHT ,"ParenRight"},
  179. {Key::ASTERISK ,"Asterisk"},
  180. {Key::PLUS ,"Plus"},
  181. {Key::COMMA ,"Comma"},
  182. {Key::MINUS ,"Minus"},
  183. {Key::PERIOD ,"Period"},
  184. {Key::SLASH ,"Slash"},
  185. {Key::KEY_0 ,"0"},
  186. {Key::KEY_1 ,"1"},
  187. {Key::KEY_2 ,"2"},
  188. {Key::KEY_3 ,"3"},
  189. {Key::KEY_4 ,"4"},
  190. {Key::KEY_5 ,"5"},
  191. {Key::KEY_6 ,"6"},
  192. {Key::KEY_7 ,"7"},
  193. {Key::KEY_8 ,"8"},
  194. {Key::KEY_9 ,"9"},
  195. {Key::COLON ,"Colon"},
  196. {Key::SEMICOLON ,"Semicolon"},
  197. {Key::LESS ,"Less"},
  198. {Key::EQUAL ,"Equal"},
  199. {Key::GREATER ,"Greater"},
  200. {Key::QUESTION ,"Question"},
  201. {Key::AT ,"At"},
  202. {Key::A ,"A"},
  203. {Key::B ,"B"},
  204. {Key::C ,"C"},
  205. {Key::D ,"D"},
  206. {Key::E ,"E"},
  207. {Key::F ,"F"},
  208. {Key::G ,"G"},
  209. {Key::H ,"H"},
  210. {Key::I ,"I"},
  211. {Key::J ,"J"},
  212. {Key::K ,"K"},
  213. {Key::L ,"L"},
  214. {Key::M ,"M"},
  215. {Key::N ,"N"},
  216. {Key::O ,"O"},
  217. {Key::P ,"P"},
  218. {Key::Q ,"Q"},
  219. {Key::R ,"R"},
  220. {Key::S ,"S"},
  221. {Key::T ,"T"},
  222. {Key::U ,"U"},
  223. {Key::V ,"V"},
  224. {Key::W ,"W"},
  225. {Key::X ,"X"},
  226. {Key::Y ,"Y"},
  227. {Key::Z ,"Z"},
  228. {Key::BRACKETLEFT ,"BracketLeft"},
  229. {Key::BACKSLASH ,"BackSlash"},
  230. {Key::BRACKETRIGHT ,"BracketRight"},
  231. {Key::ASCIICIRCUM ,"AsciiCircum"},
  232. {Key::UNDERSCORE ,"UnderScore"},
  233. {Key::QUOTELEFT ,"QuoteLeft"},
  234. {Key::BRACELEFT ,"BraceLeft"},
  235. {Key::BAR ,"Bar"},
  236. {Key::BRACERIGHT ,"BraceRight"},
  237. {Key::ASCIITILDE ,"AsciiTilde"},
  238. {Key::YEN ,"Yen"},
  239. {Key::SECTION ,"Section"},
  240. {Key::NONE ,nullptr}
  241. /* clang-format on */
  242. };
  243. bool keycode_has_unicode(Key p_keycode) {
  244. switch (p_keycode) {
  245. case Key::ESCAPE:
  246. case Key::TAB:
  247. case Key::BACKTAB:
  248. case Key::BACKSPACE:
  249. case Key::ENTER:
  250. case Key::KP_ENTER:
  251. case Key::INSERT:
  252. case Key::KEY_DELETE:
  253. case Key::PAUSE:
  254. case Key::PRINT:
  255. case Key::SYSREQ:
  256. case Key::CLEAR:
  257. case Key::HOME:
  258. case Key::END:
  259. case Key::LEFT:
  260. case Key::UP:
  261. case Key::RIGHT:
  262. case Key::DOWN:
  263. case Key::PAGEUP:
  264. case Key::PAGEDOWN:
  265. case Key::SHIFT:
  266. case Key::CTRL:
  267. case Key::META:
  268. case Key::ALT:
  269. case Key::CAPSLOCK:
  270. case Key::NUMLOCK:
  271. case Key::SCROLLLOCK:
  272. case Key::F1:
  273. case Key::F2:
  274. case Key::F3:
  275. case Key::F4:
  276. case Key::F5:
  277. case Key::F6:
  278. case Key::F7:
  279. case Key::F8:
  280. case Key::F9:
  281. case Key::F10:
  282. case Key::F11:
  283. case Key::F12:
  284. case Key::F13:
  285. case Key::F14:
  286. case Key::F15:
  287. case Key::F16:
  288. case Key::F17:
  289. case Key::F18:
  290. case Key::F19:
  291. case Key::F20:
  292. case Key::F21:
  293. case Key::F22:
  294. case Key::F23:
  295. case Key::F24:
  296. case Key::F25:
  297. case Key::F26:
  298. case Key::F27:
  299. case Key::F28:
  300. case Key::F29:
  301. case Key::F30:
  302. case Key::F31:
  303. case Key::F32:
  304. case Key::F33:
  305. case Key::F34:
  306. case Key::F35:
  307. case Key::MENU:
  308. case Key::HYPER:
  309. case Key::HELP:
  310. case Key::BACK:
  311. case Key::FORWARD:
  312. case Key::STOP:
  313. case Key::REFRESH:
  314. case Key::VOLUMEDOWN:
  315. case Key::VOLUMEMUTE:
  316. case Key::VOLUMEUP:
  317. case Key::MEDIAPLAY:
  318. case Key::MEDIASTOP:
  319. case Key::MEDIAPREVIOUS:
  320. case Key::MEDIANEXT:
  321. case Key::MEDIARECORD:
  322. case Key::HOMEPAGE:
  323. case Key::FAVORITES:
  324. case Key::SEARCH:
  325. case Key::STANDBY:
  326. case Key::OPENURL:
  327. case Key::LAUNCHMAIL:
  328. case Key::LAUNCHMEDIA:
  329. case Key::LAUNCH0:
  330. case Key::LAUNCH1:
  331. case Key::LAUNCH2:
  332. case Key::LAUNCH3:
  333. case Key::LAUNCH4:
  334. case Key::LAUNCH5:
  335. case Key::LAUNCH6:
  336. case Key::LAUNCH7:
  337. case Key::LAUNCH8:
  338. case Key::LAUNCH9:
  339. case Key::LAUNCHA:
  340. case Key::LAUNCHB:
  341. case Key::LAUNCHC:
  342. case Key::LAUNCHD:
  343. case Key::LAUNCHE:
  344. case Key::LAUNCHF:
  345. case Key::GLOBE:
  346. case Key::KEYBOARD:
  347. case Key::JIS_EISU:
  348. case Key::JIS_KANA:
  349. return false;
  350. default: {
  351. }
  352. }
  353. return true;
  354. }
  355. String keycode_get_string(Key p_code) {
  356. String codestr;
  357. if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) {
  358. codestr += find_keycode_name(Key::SHIFT);
  359. codestr += "+";
  360. }
  361. if ((p_code & KeyModifierMask::ALT) != Key::NONE) {
  362. codestr += find_keycode_name(Key::ALT);
  363. codestr += "+";
  364. }
  365. if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
  366. if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
  367. codestr += find_keycode_name(Key::META);
  368. } else {
  369. codestr += find_keycode_name(Key::CTRL);
  370. }
  371. codestr += "+";
  372. }
  373. if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
  374. codestr += find_keycode_name(Key::CTRL);
  375. codestr += "+";
  376. }
  377. if ((p_code & KeyModifierMask::META) != Key::NONE) {
  378. codestr += find_keycode_name(Key::META);
  379. codestr += "+";
  380. }
  381. p_code &= KeyModifierMask::CODE_MASK;
  382. const _KeyCodeText *kct = &_keycodes[0];
  383. while (kct->text) {
  384. if (kct->code == p_code) {
  385. codestr += kct->text;
  386. return codestr;
  387. }
  388. kct++;
  389. }
  390. codestr += String::chr((char32_t)p_code);
  391. return codestr;
  392. }
  393. Key find_keycode(const String &p_codestr) {
  394. Key keycode = Key::NONE;
  395. Vector<String> code_parts = p_codestr.split("+");
  396. if (code_parts.size() < 1) {
  397. return keycode;
  398. }
  399. const String &last_part = code_parts[code_parts.size() - 1];
  400. const _KeyCodeText *kct = &_keycodes[0];
  401. while (kct->text) {
  402. if (last_part.nocasecmp_to(kct->text) == 0) {
  403. keycode = kct->code;
  404. break;
  405. }
  406. kct++;
  407. }
  408. for (int part = 0; part < code_parts.size() - 1; part++) {
  409. const String &code_part = code_parts[part];
  410. if (code_part.nocasecmp_to(find_keycode_name(Key::SHIFT)) == 0) {
  411. keycode |= KeyModifierMask::SHIFT;
  412. } else if (code_part.nocasecmp_to(find_keycode_name(Key::CTRL)) == 0) {
  413. keycode |= KeyModifierMask::CTRL;
  414. } else if (code_part.nocasecmp_to(find_keycode_name(Key::META)) == 0) {
  415. keycode |= KeyModifierMask::META;
  416. } else if (code_part.nocasecmp_to(find_keycode_name(Key::ALT)) == 0) {
  417. keycode |= KeyModifierMask::ALT;
  418. }
  419. }
  420. return keycode;
  421. }
  422. const char *find_keycode_name(Key p_keycode) {
  423. const _KeyCodeText *kct = &_keycodes[0];
  424. while (kct->text) {
  425. if (kct->code == p_keycode) {
  426. return kct->text;
  427. }
  428. kct++;
  429. }
  430. return "";
  431. }
  432. int keycode_get_count() {
  433. const _KeyCodeText *kct = &_keycodes[0];
  434. int count = 0;
  435. while (kct->text) {
  436. count++;
  437. kct++;
  438. }
  439. return count;
  440. }
  441. int keycode_get_value_by_index(int p_index) {
  442. return (int)_keycodes[p_index].code;
  443. }
  444. const char *keycode_get_name_by_index(int p_index) {
  445. return _keycodes[p_index].text;
  446. }
  447. char32_t fix_unicode(char32_t p_char) {
  448. if (p_char >= 0x20 && p_char != 0x7F) {
  449. return p_char;
  450. }
  451. return 0;
  452. }
  453. Key fix_keycode(char32_t p_char, Key p_key) {
  454. if (p_char >= 0x20 && p_char <= 0x7E) {
  455. return (Key)String::char_uppercase(p_char);
  456. }
  457. return p_key;
  458. }
  459. Key fix_key_label(char32_t p_char, Key p_key) {
  460. if (p_char >= 0x20 && p_char != 0x7F) {
  461. return (Key)String::char_uppercase(p_char);
  462. }
  463. return p_key;
  464. }