dom_keys.inc 7.9 KB

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