dom_keys.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. int 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_code, 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("NumpadClear", CLEAR);
  52. DOM2GODOT("NumpadClearEntry", CLEAR);
  53. //DOM2GODOT("NumpadComma", UNKNOWN);
  54. DOM2GODOT("NumpadDecimal", KP_PERIOD);
  55. DOM2GODOT("NumpadDivide", KP_DIVIDE);
  56. DOM2GODOT("NumpadEnter", KP_ENTER);
  57. DOM2GODOT("NumpadEqual", EQUAL);
  58. //DOM2GODOT("NumpadHash", UNKNOWN);
  59. //DOM2GODOT("NumpadMemoryAdd", UNKNOWN);
  60. //DOM2GODOT("NumpadMemoryClear", UNKNOWN);
  61. //DOM2GODOT("NumpadMemoryRecall", UNKNOWN);
  62. //DOM2GODOT("NumpadMemoryStore", UNKNOWN);
  63. //DOM2GODOT("NumpadMemorySubtract", UNKNOWN);
  64. DOM2GODOT("NumpadMultiply", KP_MULTIPLY);
  65. DOM2GODOT("NumpadParenLeft", PARENLEFT);
  66. DOM2GODOT("NumpadParenRight", PARENRIGHT);
  67. DOM2GODOT("NumpadStar", KP_MULTIPLY); // or ASTERISK ?
  68. DOM2GODOT("NumpadSubtract", KP_SUBTRACT);
  69. // Printable ASCII.
  70. if (!p_physical) {
  71. uint8_t b0 = (uint8_t)p_key[0];
  72. uint8_t b1 = (uint8_t)p_key[1];
  73. uint8_t b2 = (uint8_t)p_key[2];
  74. if (b1 == 0 && b0 > 0x1F && b0 < 0x7F) { // ASCII.
  75. if (b0 > 0x60 && b0 < 0x7B) { // Lowercase ASCII.
  76. b0 -= 32;
  77. }
  78. return b0;
  79. }
  80. #define _U_2BYTES_MASK 0xE0
  81. #define _U_2BYTES 0xC0
  82. // Latin-1 codes.
  83. if (b2 == 0 && (b0 & _U_2BYTES_MASK) == _U_2BYTES) { // 2-bytes utf8, only known latin.
  84. uint32_t key = ((b0 & ~_U_2BYTES_MASK) << 6) | (b1 & 0x3F);
  85. if (key >= 0xA0 && key <= 0xDF) {
  86. return key;
  87. }
  88. if (key >= 0xE0 && key <= 0xFF) { // Lowercase known latin.
  89. key -= 0x20;
  90. return key;
  91. }
  92. }
  93. #undef _U_2BYTES_MASK
  94. #undef _U_2BYTES
  95. }
  96. // Alphanumeric section.
  97. DOM2GODOT("Backquote", QUOTELEFT);
  98. DOM2GODOT("Backslash", BACKSLASH);
  99. DOM2GODOT("BracketLeft", BRACKETLEFT);
  100. DOM2GODOT("BracketRight", BRACKETRIGHT);
  101. DOM2GODOT("Comma", COMMA);
  102. DOM2GODOT("Digit0", 0);
  103. DOM2GODOT("Digit1", 1);
  104. DOM2GODOT("Digit2", 2);
  105. DOM2GODOT("Digit3", 3);
  106. DOM2GODOT("Digit4", 4);
  107. DOM2GODOT("Digit5", 5);
  108. DOM2GODOT("Digit6", 6);
  109. DOM2GODOT("Digit7", 7);
  110. DOM2GODOT("Digit8", 8);
  111. DOM2GODOT("Digit9", 9);
  112. DOM2GODOT("Equal", EQUAL);
  113. DOM2GODOT("IntlBackslash", BACKSLASH);
  114. //DOM2GODOT("IntlRo", UNKNOWN);
  115. DOM2GODOT("IntlYen", YEN);
  116. DOM2GODOT("KeyA", A);
  117. DOM2GODOT("KeyB", B);
  118. DOM2GODOT("KeyC", C);
  119. DOM2GODOT("KeyD", D);
  120. DOM2GODOT("KeyE", E);
  121. DOM2GODOT("KeyF", F);
  122. DOM2GODOT("KeyG", G);
  123. DOM2GODOT("KeyH", H);
  124. DOM2GODOT("KeyI", I);
  125. DOM2GODOT("KeyJ", J);
  126. DOM2GODOT("KeyK", K);
  127. DOM2GODOT("KeyL", L);
  128. DOM2GODOT("KeyM", M);
  129. DOM2GODOT("KeyN", N);
  130. DOM2GODOT("KeyO", O);
  131. DOM2GODOT("KeyP", P);
  132. DOM2GODOT("KeyQ", Q);
  133. DOM2GODOT("KeyR", R);
  134. DOM2GODOT("KeyS", S);
  135. DOM2GODOT("KeyT", T);
  136. DOM2GODOT("KeyU", U);
  137. DOM2GODOT("KeyV", V);
  138. DOM2GODOT("KeyW", W);
  139. DOM2GODOT("KeyX", X);
  140. DOM2GODOT("KeyY", Y);
  141. DOM2GODOT("KeyZ", Z);
  142. DOM2GODOT("Minus", MINUS);
  143. DOM2GODOT("Period", PERIOD);
  144. DOM2GODOT("Quote", APOSTROPHE);
  145. DOM2GODOT("Semicolon", SEMICOLON);
  146. DOM2GODOT("Slash", SLASH);
  147. // Functional keys in the Alphanumeric section.
  148. DOM2GODOT("AltLeft", ALT);
  149. DOM2GODOT("AltRight", ALT);
  150. DOM2GODOT("Backspace", BACKSPACE);
  151. DOM2GODOT("CapsLock", CAPSLOCK);
  152. DOM2GODOT("ContextMenu", MENU);
  153. DOM2GODOT("ControlLeft", CONTROL);
  154. DOM2GODOT("ControlRight", CONTROL);
  155. DOM2GODOT("Enter", ENTER);
  156. DOM2GODOT("MetaLeft", SUPER_L);
  157. DOM2GODOT("MetaRight", SUPER_R);
  158. DOM2GODOT("ShiftLeft", SHIFT);
  159. DOM2GODOT("ShiftRight", SHIFT);
  160. DOM2GODOT("Space", SPACE);
  161. DOM2GODOT("Tab", TAB);
  162. // ControlPad section.
  163. DOM2GODOT("Delete", DELETE);
  164. DOM2GODOT("End", END);
  165. DOM2GODOT("Help", HELP);
  166. DOM2GODOT("Home", HOME);
  167. DOM2GODOT("Insert", INSERT);
  168. DOM2GODOT("PageDown", PAGEDOWN);
  169. DOM2GODOT("PageUp", PAGEUP);
  170. // ArrowPad section.
  171. DOM2GODOT("ArrowDown", DOWN);
  172. DOM2GODOT("ArrowLeft", LEFT);
  173. DOM2GODOT("ArrowRight", RIGHT);
  174. DOM2GODOT("ArrowUp", UP);
  175. // Function section.
  176. DOM2GODOT("Escape", ESCAPE);
  177. DOM2GODOT("F1", F1);
  178. DOM2GODOT("F2", F2);
  179. DOM2GODOT("F3", F3);
  180. DOM2GODOT("F4", F4);
  181. DOM2GODOT("F5", F5);
  182. DOM2GODOT("F6", F6);
  183. DOM2GODOT("F7", F7);
  184. DOM2GODOT("F8", F8);
  185. DOM2GODOT("F9", F9);
  186. DOM2GODOT("F10", F10);
  187. DOM2GODOT("F11", F11);
  188. DOM2GODOT("F12", F12);
  189. //DOM2GODOT("Fn", UNKNOWN); // never actually fired, but included in the standard draft.
  190. //DOM2GODOT("FnLock", UNKNOWN);
  191. DOM2GODOT("PrintScreen", PRINT);
  192. DOM2GODOT("ScrollLock", SCROLLLOCK);
  193. DOM2GODOT("Pause", PAUSE);
  194. // Media keys section.
  195. DOM2GODOT("BrowserBack", BACK);
  196. DOM2GODOT("BrowserFavorites", FAVORITES);
  197. DOM2GODOT("BrowserForward", FORWARD);
  198. DOM2GODOT("BrowserHome", OPENURL);
  199. DOM2GODOT("BrowserRefresh", REFRESH);
  200. DOM2GODOT("BrowserSearch", SEARCH);
  201. DOM2GODOT("BrowserStop", STOP);
  202. //DOM2GODOT("Eject", UNKNOWN);
  203. DOM2GODOT("LaunchApp1", LAUNCH0);
  204. DOM2GODOT("LaunchApp2", LAUNCH1);
  205. DOM2GODOT("LaunchMail", LAUNCHMAIL);
  206. DOM2GODOT("MediaPlayPause", MEDIAPLAY);
  207. DOM2GODOT("MediaSelect", LAUNCHMEDIA);
  208. DOM2GODOT("MediaStop", MEDIASTOP);
  209. DOM2GODOT("MediaTrackNext", MEDIANEXT);
  210. DOM2GODOT("MediaTrackPrevious", MEDIAPREVIOUS);
  211. //DOM2GODOT("Power", UNKNOWN);
  212. //DOM2GODOT("Sleep", UNKNOWN);
  213. DOM2GODOT("AudioVolumeDown", VOLUMEDOWN);
  214. DOM2GODOT("AudioVolumeMute", VOLUMEMUTE);
  215. DOM2GODOT("AudioVolumeUp", VOLUMEUP);
  216. //DOM2GODOT("WakeUp", UNKNOWN);
  217. return KEY_UNKNOWN;
  218. #undef DOM2GODOT
  219. }