dom_keys.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*************************************************************************/
  2. /* dom_keys.inc */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#Constants_for_keyCode_value
  32. #define DOM_VK_CANCEL 0x03
  33. #define DOM_VK_HELP 0x06
  34. #define DOM_VK_BACK_SPACE 0x08
  35. #define DOM_VK_TAB 0x09
  36. #define DOM_VK_CLEAR 0x0C
  37. #define DOM_VK_RETURN 0x0D
  38. #define DOM_VK_ENTER 0x0E // "Reserved, but not used."
  39. #define DOM_VK_SHIFT 0x10
  40. #define DOM_VK_CONTROL 0x11
  41. #define DOM_VK_ALT 0x12
  42. #define DOM_VK_PAUSE 0x13
  43. #define DOM_VK_CAPS_LOCK 0x14
  44. #define DOM_VK_KANA 0x15
  45. #define DOM_VK_HANGUL 0x15
  46. #define DOM_VK_EISU 0x16
  47. #define DOM_VK_JUNJA 0x17
  48. #define DOM_VK_FINAL 0x18
  49. #define DOM_VK_HANJA 0x19
  50. #define DOM_VK_KANJI 0x19
  51. #define DOM_VK_ESCAPE 0x1B
  52. #define DOM_VK_CONVERT 0x1C
  53. #define DOM_VK_NONCONVERT 0x1D
  54. #define DOM_VK_ACCEPT 0x1E
  55. #define DOM_VK_MODECHANGE 0x1F
  56. #define DOM_VK_SPACE 0x20
  57. #define DOM_VK_PAGE_UP 0x21
  58. #define DOM_VK_PAGE_DOWN 0x22
  59. #define DOM_VK_END 0x23
  60. #define DOM_VK_HOME 0x24
  61. #define DOM_VK_LEFT 0x25
  62. #define DOM_VK_UP 0x26
  63. #define DOM_VK_RIGHT 0x27
  64. #define DOM_VK_DOWN 0x28
  65. #define DOM_VK_SELECT 0x29
  66. #define DOM_VK_PRINT 0x2A
  67. #define DOM_VK_EXECUTE 0x2B
  68. #define DOM_VK_PRINTSCREEN 0x2C
  69. #define DOM_VK_INSERT 0x2D
  70. #define DOM_VK_DELETE 0x2E
  71. #define DOM_VK_0 0x30
  72. #define DOM_VK_1 0x31
  73. #define DOM_VK_2 0x32
  74. #define DOM_VK_3 0x33
  75. #define DOM_VK_4 0x34
  76. #define DOM_VK_5 0x35
  77. #define DOM_VK_6 0x36
  78. #define DOM_VK_7 0x37
  79. #define DOM_VK_8 0x38
  80. #define DOM_VK_9 0x39
  81. #define DOM_VK_COLON 0x3A
  82. #define DOM_VK_SEMICOLON 0x3B
  83. #define DOM_VK_LESS_THAN 0x3C
  84. #define DOM_VK_EQUALS 0x3D
  85. #define DOM_VK_GREATER_THAN 0x3E
  86. #define DOM_VK_QUESTION_MARK 0x3F
  87. #define DOM_VK_AT 0x40
  88. #define DOM_VK_A 0x41
  89. #define DOM_VK_B 0x42
  90. #define DOM_VK_C 0x43
  91. #define DOM_VK_D 0x44
  92. #define DOM_VK_E 0x45
  93. #define DOM_VK_F 0x46
  94. #define DOM_VK_G 0x47
  95. #define DOM_VK_H 0x48
  96. #define DOM_VK_I 0x49
  97. #define DOM_VK_J 0x4A
  98. #define DOM_VK_K 0x4B
  99. #define DOM_VK_L 0x4C
  100. #define DOM_VK_M 0x4D
  101. #define DOM_VK_N 0x4E
  102. #define DOM_VK_O 0x4F
  103. #define DOM_VK_P 0x50
  104. #define DOM_VK_Q 0x51
  105. #define DOM_VK_R 0x52
  106. #define DOM_VK_S 0x53
  107. #define DOM_VK_T 0x54
  108. #define DOM_VK_U 0x55
  109. #define DOM_VK_V 0x56
  110. #define DOM_VK_W 0x57
  111. #define DOM_VK_X 0x58
  112. #define DOM_VK_Y 0x59
  113. #define DOM_VK_Z 0x5A
  114. #define DOM_VK_WIN 0x5B
  115. #define DOM_VK_CONTEXT_MENU 0x5D
  116. #define DOM_VK_SLEEP 0x5F
  117. #define DOM_VK_NUMPAD0 0x60
  118. #define DOM_VK_NUMPAD1 0x61
  119. #define DOM_VK_NUMPAD2 0x62
  120. #define DOM_VK_NUMPAD3 0x63
  121. #define DOM_VK_NUMPAD4 0x64
  122. #define DOM_VK_NUMPAD5 0x65
  123. #define DOM_VK_NUMPAD6 0x66
  124. #define DOM_VK_NUMPAD7 0x67
  125. #define DOM_VK_NUMPAD8 0x68
  126. #define DOM_VK_NUMPAD9 0x69
  127. #define DOM_VK_MULTIPLY 0x6A
  128. #define DOM_VK_ADD 0x6B
  129. #define DOM_VK_SEPARATOR 0x6C
  130. #define DOM_VK_SUBTRACT 0x6D
  131. #define DOM_VK_DECIMAL 0x6E
  132. #define DOM_VK_DIVIDE 0x6F
  133. #define DOM_VK_F1 0x70
  134. #define DOM_VK_F2 0x71
  135. #define DOM_VK_F3 0x72
  136. #define DOM_VK_F4 0x73
  137. #define DOM_VK_F5 0x74
  138. #define DOM_VK_F6 0x75
  139. #define DOM_VK_F7 0x76
  140. #define DOM_VK_F8 0x77
  141. #define DOM_VK_F9 0x78
  142. #define DOM_VK_F10 0x79
  143. #define DOM_VK_F11 0x7A
  144. #define DOM_VK_F12 0x7B
  145. #define DOM_VK_F13 0x7C
  146. #define DOM_VK_F14 0x7D
  147. #define DOM_VK_F15 0x7E
  148. #define DOM_VK_F16 0x7F
  149. #define DOM_VK_F17 0x80
  150. #define DOM_VK_F18 0x81
  151. #define DOM_VK_F19 0x82
  152. #define DOM_VK_F20 0x83
  153. #define DOM_VK_F21 0x84
  154. #define DOM_VK_F22 0x85
  155. #define DOM_VK_F23 0x86
  156. #define DOM_VK_F24 0x87
  157. #define DOM_VK_NUM_LOCK 0x90
  158. #define DOM_VK_SCROLL_LOCK 0x91
  159. #define DOM_VK_WIN_OEM_FJ_JISHO 0x92
  160. #define DOM_VK_WIN_OEM_FJ_MASSHOU 0x93
  161. #define DOM_VK_WIN_OEM_FJ_TOUROKU 0x94
  162. #define DOM_VK_WIN_OEM_FJ_LOYA 0x95
  163. #define DOM_VK_WIN_OEM_FJ_ROYA 0x96
  164. #define DOM_VK_CIRCUMFLEX 0xA0
  165. #define DOM_VK_EXCLAMATION 0xA1
  166. #define DOM_VK_DOUBLE_QUOTE 0xA2
  167. #define DOM_VK_HASH 0xA3
  168. #define DOM_VK_DOLLAR 0xA4
  169. #define DOM_VK_PERCENT 0xA5
  170. #define DOM_VK_AMPERSAND 0xA6
  171. #define DOM_VK_UNDERSCORE 0xA7
  172. #define DOM_VK_OPEN_PAREN 0xA8
  173. #define DOM_VK_CLOSE_PAREN 0xA9
  174. #define DOM_VK_ASTERISK 0xAA
  175. #define DOM_VK_PLUS 0xAB
  176. #define DOM_VK_PIPE 0xAC
  177. #define DOM_VK_HYPHEN_MINUS 0xAD
  178. #define DOM_VK_OPEN_CURLY_BRACKET 0xAE
  179. #define DOM_VK_CLOSE_CURLY_BRACKET 0xAF
  180. #define DOM_VK_TILDE 0xB0
  181. #define DOM_VK_VOLUME_MUTE 0xB5
  182. #define DOM_VK_VOLUME_DOWN 0xB6
  183. #define DOM_VK_VOLUME_UP 0xB7
  184. #define DOM_VK_COMMA 0xBC
  185. #define DOM_VK_PERIOD 0xBE
  186. #define DOM_VK_SLASH 0xBF
  187. #define DOM_VK_BACK_QUOTE 0xC0
  188. #define DOM_VK_OPEN_BRACKET 0xDB
  189. #define DOM_VK_BACK_SLASH 0xDC
  190. #define DOM_VK_CLOSE_BRACKET 0xDD
  191. #define DOM_VK_QUOTE 0xDE
  192. #define DOM_VK_META 0xE0
  193. #define DOM_VK_ALTGR 0xE1
  194. #define DOM_VK_WIN_ICO_HELP 0xE3
  195. #define DOM_VK_WIN_ICO_00 0xE4
  196. #define DOM_VK_WIN_ICO_CLEAR 0xE6
  197. #define DOM_VK_WIN_OEM_RESET 0xE9
  198. #define DOM_VK_WIN_OEM_JUMP 0xEA
  199. #define DOM_VK_WIN_OEM_PA1 0xEB
  200. #define DOM_VK_WIN_OEM_PA2 0xEC
  201. #define DOM_VK_WIN_OEM_PA3 0xED
  202. #define DOM_VK_WIN_OEM_WSCTRL 0xEE
  203. #define DOM_VK_WIN_OEM_CUSEL 0xEF
  204. #define DOM_VK_WIN_OEM_ATTN 0xF0
  205. #define DOM_VK_WIN_OEM_FINISH 0xF1
  206. #define DOM_VK_WIN_OEM_COPY 0xF2
  207. #define DOM_VK_WIN_OEM_AUTO 0xF3
  208. #define DOM_VK_WIN_OEM_ENLW 0xF4
  209. #define DOM_VK_WIN_OEM_BACKTAB 0xF5
  210. #define DOM_VK_ATTN 0xF6
  211. #define DOM_VK_CRSEL 0xF7
  212. #define DOM_VK_EXSEL 0xF8
  213. #define DOM_VK_EREOF 0xF9
  214. #define DOM_VK_PLAY 0xFA
  215. #define DOM_VK_ZOOM 0xFB
  216. #define DOM_VK_PA1 0xFD
  217. #define DOM_VK_WIN_OEM_CLEAR 0xFE
  218. int dom2godot_scancode(int dom_keycode) {
  219. if (DOM_VK_0 <= dom_keycode && dom_keycode <= DOM_VK_Z) {
  220. // ASCII intersection
  221. return dom_keycode;
  222. }
  223. if (DOM_VK_NUMPAD0 <= dom_keycode && dom_keycode <= DOM_VK_NUMPAD9) {
  224. // Numpad numbers
  225. return KEY_KP_0 + (dom_keycode - DOM_VK_NUMPAD0);
  226. }
  227. if (DOM_VK_F1 <= dom_keycode && dom_keycode <= DOM_VK_F16) {
  228. // F1-F16
  229. return KEY_F1 + (dom_keycode - DOM_VK_F1);
  230. }
  231. switch (dom_keycode) {
  232. //case DOM_VK_CANCEL: return KEY_UNKNOWN;
  233. case DOM_VK_HELP: return KEY_HELP;
  234. case DOM_VK_BACK_SPACE: return KEY_BACKSPACE;
  235. case DOM_VK_TAB: return KEY_TAB;
  236. case DOM_VK_CLEAR:
  237. case DOM_VK_WIN_OEM_CLEAR: // OEM duplicate
  238. return KEY_CLEAR;
  239. case DOM_VK_RETURN:
  240. case DOM_VK_ENTER: // unused according to MDN
  241. return KEY_ENTER;
  242. case DOM_VK_SHIFT: return KEY_SHIFT;
  243. case DOM_VK_CONTROL: return KEY_CONTROL;
  244. case DOM_VK_ALT:
  245. case DOM_VK_ALTGR:
  246. return KEY_ALT;
  247. case DOM_VK_PAUSE: return KEY_PAUSE;
  248. case DOM_VK_CAPS_LOCK:
  249. return KEY_CAPSLOCK;
  250. /*
  251. case DOM_VK_KANA: return KEY_UNKNOWN;
  252. case DOM_VK_HANGUL: return KEY_UNKNOWN;
  253. case DOM_VK_EISU: return KEY_UNKNOWN;
  254. case DOM_VK_JUNJA: return KEY_UNKNOWN;
  255. case DOM_VK_FINAL: return KEY_UNKNOWN;
  256. case DOM_VK_HANJA: return KEY_UNKNOWN;
  257. case DOM_VK_KANJI: return KEY_UNKNOWN;
  258. */
  259. case DOM_VK_ESCAPE:
  260. return KEY_ESCAPE;
  261. /*
  262. case DOM_VK_CONVERT: return KEY_UNKNOWN;
  263. case DOM_VK_NONCONVERT: return KEY_UNKNOWN;
  264. case DOM_VK_ACCEPT: return KEY_UNKNOWN;
  265. case DOM_VK_MODECHANGE: return KEY_UNKNOWN;
  266. */
  267. case DOM_VK_SPACE: return KEY_SPACE;
  268. case DOM_VK_PAGE_UP: return KEY_PAGEUP;
  269. case DOM_VK_PAGE_DOWN: return KEY_PAGEDOWN;
  270. case DOM_VK_END: return KEY_END;
  271. case DOM_VK_HOME: return KEY_HOME;
  272. case DOM_VK_LEFT: return KEY_LEFT;
  273. case DOM_VK_UP: return KEY_UP;
  274. case DOM_VK_RIGHT: return KEY_RIGHT;
  275. case DOM_VK_DOWN:
  276. return KEY_DOWN;
  277. //case DOM_VK_SELECT: return KEY_UNKNOWN;
  278. case DOM_VK_PRINTSCREEN:
  279. case DOM_VK_PRINT:
  280. return KEY_PRINT;
  281. //case DOM_VK_EXECUTE: return KEY_UNKNOWN;
  282. case DOM_VK_INSERT: return KEY_INSERT;
  283. case DOM_VK_DELETE: return KEY_DELETE;
  284. case DOM_VK_META:
  285. case DOM_VK_WIN:
  286. return KEY_META;
  287. case DOM_VK_CONTEXT_MENU: return KEY_MENU;
  288. case DOM_VK_SLEEP:
  289. return KEY_STANDBY;
  290. // Numpad keys
  291. case DOM_VK_MULTIPLY: return KEY_KP_MULTIPLY;
  292. case DOM_VK_ADD: return KEY_KP_ADD;
  293. case DOM_VK_SEPARATOR:
  294. return KEY_KP_PERIOD; // Good enough?
  295. case DOM_VK_SUBTRACT: return KEY_KP_SUBTRACT;
  296. case DOM_VK_DECIMAL: return KEY_KP_PERIOD;
  297. case DOM_VK_DIVIDE:
  298. return KEY_KP_DIVIDE;
  299. /*
  300. case DOM_VK_F17: return KEY_UNKNOWN;
  301. case DOM_VK_F18: return KEY_UNKNOWN;
  302. case DOM_VK_F19: return KEY_UNKNOWN;
  303. case DOM_VK_F20: return KEY_UNKNOWN;
  304. case DOM_VK_F21: return KEY_UNKNOWN;
  305. case DOM_VK_F22: return KEY_UNKNOWN;
  306. case DOM_VK_F23: return KEY_UNKNOWN;
  307. case DOM_VK_F24: return KEY_UNKNOWN;
  308. */
  309. case DOM_VK_NUM_LOCK: return KEY_NUMLOCK;
  310. case DOM_VK_SCROLL_LOCK:
  311. return KEY_SCROLLLOCK;
  312. /*
  313. case DOM_VK_WIN_OEM_FJ_JISHO: return KEY_UNKNOWN;
  314. case DOM_VK_WIN_OEM_FJ_MASSHOU: return KEY_UNKNOWN;
  315. case DOM_VK_WIN_OEM_FJ_TOUROKU: return KEY_UNKNOWN;
  316. case DOM_VK_WIN_OEM_FJ_LOYA: return KEY_UNKNOWN;
  317. case DOM_VK_WIN_OEM_FJ_ROYA: return KEY_UNKNOWN;
  318. */
  319. case DOM_VK_CIRCUMFLEX: return KEY_ASCIICIRCUM;
  320. case DOM_VK_EXCLAMATION: return KEY_EXCLAM;
  321. case DOM_VK_DOUBLE_QUOTE: return KEY_QUOTEDBL;
  322. case DOM_VK_HASH: return KEY_NUMBERSIGN;
  323. case DOM_VK_DOLLAR: return KEY_DOLLAR;
  324. case DOM_VK_PERCENT: return KEY_PERCENT;
  325. case DOM_VK_AMPERSAND: return KEY_AMPERSAND;
  326. case DOM_VK_UNDERSCORE: return KEY_UNDERSCORE;
  327. case DOM_VK_OPEN_PAREN: return KEY_PARENLEFT;
  328. case DOM_VK_CLOSE_PAREN: return KEY_PARENRIGHT;
  329. case DOM_VK_ASTERISK: return KEY_ASTERISK;
  330. case DOM_VK_PLUS: return KEY_PLUS;
  331. case DOM_VK_PIPE: return KEY_BAR;
  332. case DOM_VK_HYPHEN_MINUS: return KEY_MINUS;
  333. case DOM_VK_OPEN_CURLY_BRACKET: return KEY_BRACELEFT;
  334. case DOM_VK_CLOSE_CURLY_BRACKET: return KEY_BRACERIGHT;
  335. case DOM_VK_TILDE: return KEY_ASCIITILDE;
  336. case DOM_VK_VOLUME_MUTE: return KEY_VOLUMEMUTE;
  337. case DOM_VK_VOLUME_DOWN: return KEY_VOLUMEDOWN;
  338. case DOM_VK_VOLUME_UP: return KEY_VOLUMEUP;
  339. case DOM_VK_COMMA: return KEY_COMMA;
  340. case DOM_VK_PERIOD: return KEY_PERIOD;
  341. case DOM_VK_SLASH: return KEY_SLASH;
  342. case DOM_VK_BACK_QUOTE: return KEY_QUOTELEFT;
  343. case DOM_VK_OPEN_BRACKET: return KEY_BRACKETLEFT;
  344. case DOM_VK_BACK_SLASH: return KEY_BACKSLASH;
  345. case DOM_VK_CLOSE_BRACKET: return KEY_BRACKETRIGHT;
  346. case DOM_VK_QUOTE:
  347. return KEY_APOSTROPHE;
  348. // The rest is OEM/unusual.
  349. default: return KEY_UNKNOWN;
  350. };
  351. }