dom_keys.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**************************************************************************/
  2. /* dom_keys.inc */
  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 "core/os/keyboard.h"
  31. // See https://w3c.github.io/uievents-code/#code-value-tables
  32. Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], bool p_physical) {
  33. #define DOM2GODOT(p_str, p_godot_code) \
  34. if (memcmp((const void *)p_str, (void *)(p_physical ? p_code : p_key), strlen(p_str) + 1) == 0) { \
  35. return Key::p_godot_code; \
  36. }
  37. // Numpad section.
  38. DOM2GODOT("NumLock", NUMLOCK);
  39. DOM2GODOT("Numpad0", KP_0);
  40. DOM2GODOT("Numpad1", KP_1);
  41. DOM2GODOT("Numpad2", KP_2);
  42. DOM2GODOT("Numpad3", KP_3);
  43. DOM2GODOT("Numpad4", KP_4);
  44. DOM2GODOT("Numpad5", KP_5);
  45. DOM2GODOT("Numpad6", KP_6);
  46. DOM2GODOT("Numpad7", KP_7);
  47. DOM2GODOT("Numpad8", KP_8);
  48. DOM2GODOT("Numpad9", KP_9);
  49. DOM2GODOT("NumpadAdd", KP_ADD);
  50. DOM2GODOT("NumpadBackspace", BACKSPACE);
  51. DOM2GODOT("Clear", CLEAR); // NumLock on macOS.
  52. DOM2GODOT("NumpadClear", CLEAR);
  53. DOM2GODOT("NumpadClearEntry", CLEAR);
  54. //DOM2GODOT("NumpadComma", UNKNOWN);
  55. DOM2GODOT("NumpadDecimal", KP_PERIOD);
  56. DOM2GODOT("NumpadDivide", KP_DIVIDE);
  57. DOM2GODOT("NumpadEnter", KP_ENTER);
  58. DOM2GODOT("NumpadEqual", EQUAL);
  59. //DOM2GODOT("NumpadHash", UNKNOWN);
  60. //DOM2GODOT("NumpadMemoryAdd", UNKNOWN);
  61. //DOM2GODOT("NumpadMemoryClear", UNKNOWN);
  62. //DOM2GODOT("NumpadMemoryRecall", UNKNOWN);
  63. //DOM2GODOT("NumpadMemoryStore", UNKNOWN);
  64. //DOM2GODOT("NumpadMemorySubtract", UNKNOWN);
  65. DOM2GODOT("NumpadMultiply", KP_MULTIPLY);
  66. DOM2GODOT("NumpadParenLeft", PARENLEFT);
  67. DOM2GODOT("NumpadParenRight", PARENRIGHT);
  68. DOM2GODOT("NumpadStar", KP_MULTIPLY); // or ASTERISK ?
  69. DOM2GODOT("NumpadSubtract", KP_SUBTRACT);
  70. // Alphanumeric section.
  71. DOM2GODOT("Backquote", QUOTELEFT);
  72. DOM2GODOT("Backslash", BACKSLASH);
  73. DOM2GODOT("BracketLeft", BRACKETLEFT);
  74. DOM2GODOT("BracketRight", BRACKETRIGHT);
  75. DOM2GODOT("Comma", COMMA);
  76. DOM2GODOT("Digit0", KEY_0);
  77. DOM2GODOT("Digit1", KEY_1);
  78. DOM2GODOT("Digit2", KEY_2);
  79. DOM2GODOT("Digit3", KEY_3);
  80. DOM2GODOT("Digit4", KEY_4);
  81. DOM2GODOT("Digit5", KEY_5);
  82. DOM2GODOT("Digit6", KEY_6);
  83. DOM2GODOT("Digit7", KEY_7);
  84. DOM2GODOT("Digit8", KEY_8);
  85. DOM2GODOT("Digit9", KEY_9);
  86. DOM2GODOT("Equal", EQUAL);
  87. DOM2GODOT("IntlBackslash", BACKSLASH);
  88. //DOM2GODOT("IntlRo", UNKNOWN);
  89. DOM2GODOT("IntlYen", YEN);
  90. DOM2GODOT("KeyA", A);
  91. DOM2GODOT("KeyB", B);
  92. DOM2GODOT("KeyC", C);
  93. DOM2GODOT("KeyD", D);
  94. DOM2GODOT("KeyE", E);
  95. DOM2GODOT("KeyF", F);
  96. DOM2GODOT("KeyG", G);
  97. DOM2GODOT("KeyH", H);
  98. DOM2GODOT("KeyI", I);
  99. DOM2GODOT("KeyJ", J);
  100. DOM2GODOT("KeyK", K);
  101. DOM2GODOT("KeyL", L);
  102. DOM2GODOT("KeyM", M);
  103. DOM2GODOT("KeyN", N);
  104. DOM2GODOT("KeyO", O);
  105. DOM2GODOT("KeyP", P);
  106. DOM2GODOT("KeyQ", Q);
  107. DOM2GODOT("KeyR", R);
  108. DOM2GODOT("KeyS", S);
  109. DOM2GODOT("KeyT", T);
  110. DOM2GODOT("KeyU", U);
  111. DOM2GODOT("KeyV", V);
  112. DOM2GODOT("KeyW", W);
  113. DOM2GODOT("KeyX", X);
  114. DOM2GODOT("KeyY", Y);
  115. DOM2GODOT("KeyZ", Z);
  116. DOM2GODOT("Minus", MINUS);
  117. DOM2GODOT("Period", PERIOD);
  118. DOM2GODOT("Quote", APOSTROPHE);
  119. DOM2GODOT("Semicolon", SEMICOLON);
  120. DOM2GODOT("Slash", SLASH);
  121. // Functional keys in the Alphanumeric section.
  122. DOM2GODOT("Alt", ALT);
  123. DOM2GODOT("AltLeft", ALT);
  124. DOM2GODOT("AltRight", ALT);
  125. DOM2GODOT("Backspace", BACKSPACE);
  126. DOM2GODOT("CapsLock", CAPSLOCK);
  127. DOM2GODOT("ContextMenu", MENU);
  128. DOM2GODOT("Control", CTRL);
  129. DOM2GODOT("ControlLeft", CTRL);
  130. DOM2GODOT("ControlRight", CTRL);
  131. DOM2GODOT("Enter", ENTER);
  132. DOM2GODOT("Meta", META);
  133. DOM2GODOT("MetaLeft", META);
  134. DOM2GODOT("MetaRight", META);
  135. DOM2GODOT("OSLeft", META); // Command on macOS.
  136. DOM2GODOT("OSRight", META); // Command on macOS.
  137. DOM2GODOT("Shift", SHIFT);
  138. DOM2GODOT("ShiftLeft", SHIFT);
  139. DOM2GODOT("ShiftRight", SHIFT);
  140. DOM2GODOT("Space", SPACE);
  141. DOM2GODOT("Tab", TAB);
  142. // ControlPad section.
  143. DOM2GODOT("Delete", KEY_DELETE);
  144. DOM2GODOT("End", END);
  145. DOM2GODOT("Help", HELP);
  146. DOM2GODOT("Home", HOME);
  147. DOM2GODOT("Insert", INSERT);
  148. DOM2GODOT("PageDown", PAGEDOWN);
  149. DOM2GODOT("PageUp", PAGEUP);
  150. // ArrowPad section.
  151. DOM2GODOT("ArrowDown", DOWN);
  152. DOM2GODOT("ArrowLeft", LEFT);
  153. DOM2GODOT("ArrowRight", RIGHT);
  154. DOM2GODOT("ArrowUp", UP);
  155. // Function section.
  156. DOM2GODOT("Escape", ESCAPE);
  157. DOM2GODOT("F1", F1);
  158. DOM2GODOT("F2", F2);
  159. DOM2GODOT("F3", F3);
  160. DOM2GODOT("F4", F4);
  161. DOM2GODOT("F5", F5);
  162. DOM2GODOT("F6", F6);
  163. DOM2GODOT("F7", F7);
  164. DOM2GODOT("F8", F8);
  165. DOM2GODOT("F9", F9);
  166. DOM2GODOT("F10", F10);
  167. DOM2GODOT("F11", F11);
  168. DOM2GODOT("F12", F12);
  169. //DOM2GODOT("Fn", UNKNOWN); // never actually fired, but included in the standard draft.
  170. //DOM2GODOT("FnLock", UNKNOWN);
  171. DOM2GODOT("PrintScreen", PRINT);
  172. DOM2GODOT("ScrollLock", SCROLLLOCK);
  173. DOM2GODOT("Pause", PAUSE);
  174. // Media keys section.
  175. DOM2GODOT("BrowserBack", BACK);
  176. DOM2GODOT("BrowserFavorites", FAVORITES);
  177. DOM2GODOT("BrowserForward", FORWARD);
  178. DOM2GODOT("BrowserHome", OPENURL);
  179. DOM2GODOT("BrowserRefresh", REFRESH);
  180. DOM2GODOT("BrowserSearch", SEARCH);
  181. DOM2GODOT("BrowserStop", STOP);
  182. //DOM2GODOT("Eject", UNKNOWN);
  183. DOM2GODOT("LaunchApp1", LAUNCH0);
  184. DOM2GODOT("LaunchApp2", LAUNCH1);
  185. DOM2GODOT("LaunchMail", LAUNCHMAIL);
  186. DOM2GODOT("MediaPlayPause", MEDIAPLAY);
  187. DOM2GODOT("MediaSelect", LAUNCHMEDIA);
  188. DOM2GODOT("MediaStop", MEDIASTOP);
  189. DOM2GODOT("MediaTrackNext", MEDIANEXT);
  190. DOM2GODOT("MediaTrackPrevious", MEDIAPREVIOUS);
  191. //DOM2GODOT("Power", UNKNOWN);
  192. //DOM2GODOT("Sleep", UNKNOWN);
  193. DOM2GODOT("AudioVolumeDown", VOLUMEDOWN);
  194. DOM2GODOT("AudioVolumeMute", VOLUMEMUTE);
  195. DOM2GODOT("AudioVolumeUp", VOLUMEUP);
  196. //DOM2GODOT("WakeUp", UNKNOWN);
  197. // Printable ASCII.
  198. uint8_t b0 = (uint8_t)p_key[0];
  199. uint8_t b1 = (uint8_t)p_key[1];
  200. if (b0 >= 0x20 && b0 < 0x7F) { // ASCII.
  201. if (b0 > 0x60 && b0 < 0x7B) { // Lowercase ASCII.
  202. b0 -= 32;
  203. }
  204. return (Key)b0;
  205. } else if (b0 == 0xC2 && b1 == 0xA5) {
  206. return Key::YEN;
  207. } else if (b0 == 0xC2 && b1 == 0xA7) {
  208. return Key::SECTION;
  209. }
  210. return Key::NONE;
  211. #undef DOM2GODOT
  212. }
  213. KeyLocation dom_code2godot_key_location(EM_UTF8 const p_code[32]) {
  214. #define DOM2GODOT(m_str, m_godot_code) \
  215. if (memcmp((const void *)m_str, (void *)p_code, strlen(m_str) + 1) == 0) { \
  216. return KeyLocation::m_godot_code; \
  217. }
  218. DOM2GODOT("AltLeft", LEFT);
  219. DOM2GODOT("AltRight", RIGHT);
  220. DOM2GODOT("ControlLeft", LEFT);
  221. DOM2GODOT("ControlRight", RIGHT);
  222. DOM2GODOT("MetaLeft", LEFT);
  223. DOM2GODOT("MetaRight", RIGHT);
  224. DOM2GODOT("OSLeft", LEFT);
  225. DOM2GODOT("OSRight", RIGHT);
  226. DOM2GODOT("ShiftLeft", LEFT);
  227. DOM2GODOT("ShiftRight", RIGHT);
  228. return KeyLocation::UNSPECIFIED;
  229. #undef DOM2GODOT
  230. }