key_mapping_ios.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* key_mapping_ios.mm */
  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. #import "key_mapping_ios.h"
  31. #include "core/templates/hash_map.h"
  32. struct HashMapHasherKeys {
  33. static _FORCE_INLINE_ uint32_t hash(const Key p_key) { return hash_fmix32(static_cast<uint32_t>(p_key)); }
  34. static _FORCE_INLINE_ uint32_t hash(const CFIndex p_key) { return hash_fmix32(p_key); }
  35. };
  36. HashMap<CFIndex, Key, HashMapHasherKeys> keyusage_map;
  37. HashMap<CFIndex, KeyLocation, HashMapHasherKeys> location_map;
  38. void KeyMappingIOS::initialize() {
  39. if (@available(iOS 13.4, *)) {
  40. keyusage_map[UIKeyboardHIDUsageKeyboardA] = Key::A;
  41. keyusage_map[UIKeyboardHIDUsageKeyboardB] = Key::B;
  42. keyusage_map[UIKeyboardHIDUsageKeyboardC] = Key::C;
  43. keyusage_map[UIKeyboardHIDUsageKeyboardD] = Key::D;
  44. keyusage_map[UIKeyboardHIDUsageKeyboardE] = Key::E;
  45. keyusage_map[UIKeyboardHIDUsageKeyboardF] = Key::F;
  46. keyusage_map[UIKeyboardHIDUsageKeyboardG] = Key::G;
  47. keyusage_map[UIKeyboardHIDUsageKeyboardH] = Key::H;
  48. keyusage_map[UIKeyboardHIDUsageKeyboardI] = Key::I;
  49. keyusage_map[UIKeyboardHIDUsageKeyboardJ] = Key::J;
  50. keyusage_map[UIKeyboardHIDUsageKeyboardK] = Key::K;
  51. keyusage_map[UIKeyboardHIDUsageKeyboardL] = Key::L;
  52. keyusage_map[UIKeyboardHIDUsageKeyboardM] = Key::M;
  53. keyusage_map[UIKeyboardHIDUsageKeyboardN] = Key::N;
  54. keyusage_map[UIKeyboardHIDUsageKeyboardO] = Key::O;
  55. keyusage_map[UIKeyboardHIDUsageKeyboardP] = Key::P;
  56. keyusage_map[UIKeyboardHIDUsageKeyboardQ] = Key::Q;
  57. keyusage_map[UIKeyboardHIDUsageKeyboardR] = Key::R;
  58. keyusage_map[UIKeyboardHIDUsageKeyboardS] = Key::S;
  59. keyusage_map[UIKeyboardHIDUsageKeyboardT] = Key::T;
  60. keyusage_map[UIKeyboardHIDUsageKeyboardU] = Key::U;
  61. keyusage_map[UIKeyboardHIDUsageKeyboardV] = Key::V;
  62. keyusage_map[UIKeyboardHIDUsageKeyboardW] = Key::W;
  63. keyusage_map[UIKeyboardHIDUsageKeyboardX] = Key::X;
  64. keyusage_map[UIKeyboardHIDUsageKeyboardY] = Key::Y;
  65. keyusage_map[UIKeyboardHIDUsageKeyboardZ] = Key::Z;
  66. keyusage_map[UIKeyboardHIDUsageKeyboard0] = Key::KEY_0;
  67. keyusage_map[UIKeyboardHIDUsageKeyboard1] = Key::KEY_1;
  68. keyusage_map[UIKeyboardHIDUsageKeyboard2] = Key::KEY_2;
  69. keyusage_map[UIKeyboardHIDUsageKeyboard3] = Key::KEY_3;
  70. keyusage_map[UIKeyboardHIDUsageKeyboard4] = Key::KEY_4;
  71. keyusage_map[UIKeyboardHIDUsageKeyboard5] = Key::KEY_5;
  72. keyusage_map[UIKeyboardHIDUsageKeyboard6] = Key::KEY_6;
  73. keyusage_map[UIKeyboardHIDUsageKeyboard7] = Key::KEY_7;
  74. keyusage_map[UIKeyboardHIDUsageKeyboard8] = Key::KEY_8;
  75. keyusage_map[UIKeyboardHIDUsageKeyboard9] = Key::KEY_9;
  76. keyusage_map[UIKeyboardHIDUsageKeyboardBackslash] = Key::BACKSLASH;
  77. keyusage_map[UIKeyboardHIDUsageKeyboardCloseBracket] = Key::BRACKETRIGHT;
  78. keyusage_map[UIKeyboardHIDUsageKeyboardComma] = Key::COMMA;
  79. keyusage_map[UIKeyboardHIDUsageKeyboardEqualSign] = Key::EQUAL;
  80. keyusage_map[UIKeyboardHIDUsageKeyboardHyphen] = Key::MINUS;
  81. keyusage_map[UIKeyboardHIDUsageKeyboardNonUSBackslash] = Key::SECTION;
  82. keyusage_map[UIKeyboardHIDUsageKeyboardNonUSPound] = Key::ASCIITILDE;
  83. keyusage_map[UIKeyboardHIDUsageKeyboardOpenBracket] = Key::BRACKETLEFT;
  84. keyusage_map[UIKeyboardHIDUsageKeyboardPeriod] = Key::PERIOD;
  85. keyusage_map[UIKeyboardHIDUsageKeyboardQuote] = Key::QUOTEDBL;
  86. keyusage_map[UIKeyboardHIDUsageKeyboardSemicolon] = Key::SEMICOLON;
  87. keyusage_map[UIKeyboardHIDUsageKeyboardSeparator] = Key::SECTION;
  88. keyusage_map[UIKeyboardHIDUsageKeyboardSlash] = Key::SLASH;
  89. keyusage_map[UIKeyboardHIDUsageKeyboardSpacebar] = Key::SPACE;
  90. keyusage_map[UIKeyboardHIDUsageKeyboardCapsLock] = Key::CAPSLOCK;
  91. keyusage_map[UIKeyboardHIDUsageKeyboardLeftAlt] = Key::ALT;
  92. keyusage_map[UIKeyboardHIDUsageKeyboardLeftControl] = Key::CTRL;
  93. keyusage_map[UIKeyboardHIDUsageKeyboardLeftShift] = Key::SHIFT;
  94. keyusage_map[UIKeyboardHIDUsageKeyboardRightAlt] = Key::ALT;
  95. keyusage_map[UIKeyboardHIDUsageKeyboardRightControl] = Key::CTRL;
  96. keyusage_map[UIKeyboardHIDUsageKeyboardRightShift] = Key::SHIFT;
  97. keyusage_map[UIKeyboardHIDUsageKeyboardScrollLock] = Key::SCROLLLOCK;
  98. keyusage_map[UIKeyboardHIDUsageKeyboardLeftArrow] = Key::LEFT;
  99. keyusage_map[UIKeyboardHIDUsageKeyboardRightArrow] = Key::RIGHT;
  100. keyusage_map[UIKeyboardHIDUsageKeyboardUpArrow] = Key::UP;
  101. keyusage_map[UIKeyboardHIDUsageKeyboardDownArrow] = Key::DOWN;
  102. keyusage_map[UIKeyboardHIDUsageKeyboardPageUp] = Key::PAGEUP;
  103. keyusage_map[UIKeyboardHIDUsageKeyboardPageDown] = Key::PAGEDOWN;
  104. keyusage_map[UIKeyboardHIDUsageKeyboardHome] = Key::HOME;
  105. keyusage_map[UIKeyboardHIDUsageKeyboardEnd] = Key::END;
  106. keyusage_map[UIKeyboardHIDUsageKeyboardDeleteForward] = Key::KEY_DELETE;
  107. keyusage_map[UIKeyboardHIDUsageKeyboardDeleteOrBackspace] = Key::BACKSPACE;
  108. keyusage_map[UIKeyboardHIDUsageKeyboardEscape] = Key::ESCAPE;
  109. keyusage_map[UIKeyboardHIDUsageKeyboardInsert] = Key::INSERT;
  110. keyusage_map[UIKeyboardHIDUsageKeyboardReturn] = Key::ENTER;
  111. keyusage_map[UIKeyboardHIDUsageKeyboardTab] = Key::TAB;
  112. keyusage_map[UIKeyboardHIDUsageKeyboardF1] = Key::F1;
  113. keyusage_map[UIKeyboardHIDUsageKeyboardF2] = Key::F2;
  114. keyusage_map[UIKeyboardHIDUsageKeyboardF3] = Key::F3;
  115. keyusage_map[UIKeyboardHIDUsageKeyboardF4] = Key::F4;
  116. keyusage_map[UIKeyboardHIDUsageKeyboardF5] = Key::F5;
  117. keyusage_map[UIKeyboardHIDUsageKeyboardF6] = Key::F6;
  118. keyusage_map[UIKeyboardHIDUsageKeyboardF7] = Key::F7;
  119. keyusage_map[UIKeyboardHIDUsageKeyboardF8] = Key::F8;
  120. keyusage_map[UIKeyboardHIDUsageKeyboardF9] = Key::F9;
  121. keyusage_map[UIKeyboardHIDUsageKeyboardF10] = Key::F10;
  122. keyusage_map[UIKeyboardHIDUsageKeyboardF11] = Key::F11;
  123. keyusage_map[UIKeyboardHIDUsageKeyboardF12] = Key::F12;
  124. keyusage_map[UIKeyboardHIDUsageKeyboardF13] = Key::F13;
  125. keyusage_map[UIKeyboardHIDUsageKeyboardF14] = Key::F14;
  126. keyusage_map[UIKeyboardHIDUsageKeyboardF15] = Key::F15;
  127. keyusage_map[UIKeyboardHIDUsageKeyboardF16] = Key::F16;
  128. keyusage_map[UIKeyboardHIDUsageKeyboardF17] = Key::F17;
  129. keyusage_map[UIKeyboardHIDUsageKeyboardF18] = Key::F18;
  130. keyusage_map[UIKeyboardHIDUsageKeyboardF19] = Key::F19;
  131. keyusage_map[UIKeyboardHIDUsageKeyboardF20] = Key::F20;
  132. keyusage_map[UIKeyboardHIDUsageKeyboardF21] = Key::F21;
  133. keyusage_map[UIKeyboardHIDUsageKeyboardF22] = Key::F22;
  134. keyusage_map[UIKeyboardHIDUsageKeyboardF23] = Key::F23;
  135. keyusage_map[UIKeyboardHIDUsageKeyboardF24] = Key::F24;
  136. keyusage_map[UIKeyboardHIDUsageKeypad0] = Key::KP_0;
  137. keyusage_map[UIKeyboardHIDUsageKeypad1] = Key::KP_1;
  138. keyusage_map[UIKeyboardHIDUsageKeypad2] = Key::KP_2;
  139. keyusage_map[UIKeyboardHIDUsageKeypad3] = Key::KP_3;
  140. keyusage_map[UIKeyboardHIDUsageKeypad4] = Key::KP_4;
  141. keyusage_map[UIKeyboardHIDUsageKeypad5] = Key::KP_5;
  142. keyusage_map[UIKeyboardHIDUsageKeypad6] = Key::KP_6;
  143. keyusage_map[UIKeyboardHIDUsageKeypad7] = Key::KP_7;
  144. keyusage_map[UIKeyboardHIDUsageKeypad8] = Key::KP_8;
  145. keyusage_map[UIKeyboardHIDUsageKeypad9] = Key::KP_9;
  146. keyusage_map[UIKeyboardHIDUsageKeypadAsterisk] = Key::KP_MULTIPLY;
  147. keyusage_map[UIKeyboardHIDUsageKeyboardGraveAccentAndTilde] = Key::BAR;
  148. keyusage_map[UIKeyboardHIDUsageKeypadEnter] = Key::KP_ENTER;
  149. keyusage_map[UIKeyboardHIDUsageKeypadHyphen] = Key::KP_SUBTRACT;
  150. keyusage_map[UIKeyboardHIDUsageKeypadNumLock] = Key::NUMLOCK;
  151. keyusage_map[UIKeyboardHIDUsageKeypadPeriod] = Key::KP_PERIOD;
  152. keyusage_map[UIKeyboardHIDUsageKeypadPlus] = Key::KP_ADD;
  153. keyusage_map[UIKeyboardHIDUsageKeypadSlash] = Key::KP_DIVIDE;
  154. keyusage_map[UIKeyboardHIDUsageKeyboardPause] = Key::PAUSE;
  155. keyusage_map[UIKeyboardHIDUsageKeyboardStop] = Key::STOP;
  156. keyusage_map[UIKeyboardHIDUsageKeyboardMute] = Key::VOLUMEMUTE;
  157. keyusage_map[UIKeyboardHIDUsageKeyboardVolumeUp] = Key::VOLUMEUP;
  158. keyusage_map[UIKeyboardHIDUsageKeyboardVolumeDown] = Key::VOLUMEDOWN;
  159. keyusage_map[UIKeyboardHIDUsageKeyboardFind] = Key::SEARCH;
  160. keyusage_map[UIKeyboardHIDUsageKeyboardHelp] = Key::HELP;
  161. keyusage_map[UIKeyboardHIDUsageKeyboardLeftGUI] = Key::META;
  162. keyusage_map[UIKeyboardHIDUsageKeyboardRightGUI] = Key::META;
  163. keyusage_map[UIKeyboardHIDUsageKeyboardMenu] = Key::MENU;
  164. keyusage_map[UIKeyboardHIDUsageKeyboardPrintScreen] = Key::PRINT;
  165. keyusage_map[UIKeyboardHIDUsageKeyboardReturnOrEnter] = Key::ENTER;
  166. keyusage_map[UIKeyboardHIDUsageKeyboardSysReqOrAttention] = Key::SYSREQ;
  167. keyusage_map[0x01AE] = Key::KEYBOARD; // On-screen keyboard key on smart connector keyboard.
  168. keyusage_map[0x029D] = Key::GLOBE; // "Globe" key on smart connector / Mac keyboard.
  169. keyusage_map[UIKeyboardHIDUsageKeyboardLANG1] = Key::JIS_EISU;
  170. keyusage_map[UIKeyboardHIDUsageKeyboardLANG2] = Key::JIS_KANA;
  171. location_map[UIKeyboardHIDUsageKeyboardLeftAlt] = KeyLocation::LEFT;
  172. location_map[UIKeyboardHIDUsageKeyboardRightAlt] = KeyLocation::RIGHT;
  173. location_map[UIKeyboardHIDUsageKeyboardLeftControl] = KeyLocation::LEFT;
  174. location_map[UIKeyboardHIDUsageKeyboardRightControl] = KeyLocation::RIGHT;
  175. location_map[UIKeyboardHIDUsageKeyboardLeftShift] = KeyLocation::LEFT;
  176. location_map[UIKeyboardHIDUsageKeyboardRightShift] = KeyLocation::RIGHT;
  177. location_map[UIKeyboardHIDUsageKeyboardLeftGUI] = KeyLocation::LEFT;
  178. location_map[UIKeyboardHIDUsageKeyboardRightGUI] = KeyLocation::RIGHT;
  179. }
  180. }
  181. Key KeyMappingIOS::remap_key(CFIndex p_keycode) {
  182. if (@available(iOS 13.4, *)) {
  183. const Key *key = keyusage_map.getptr(p_keycode);
  184. if (key) {
  185. return *key;
  186. }
  187. }
  188. return Key::NONE;
  189. }
  190. KeyLocation KeyMappingIOS::key_location(CFIndex p_keycode) {
  191. if (@available(iOS 13.4, *)) {
  192. const KeyLocation *location = location_map.getptr(p_keycode);
  193. if (location) {
  194. return *location;
  195. }
  196. }
  197. return KeyLocation::UNSPECIFIED;
  198. }