key_mapping_windows.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**************************************************************************/
  2. /* key_mapping_windows.cpp */
  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 "key_mapping_windows.h"
  31. #include <stdio.h>
  32. struct _WinTranslatePair {
  33. unsigned int keysym;
  34. unsigned int keycode;
  35. };
  36. static _WinTranslatePair _vk_to_keycode[] = {
  37. { KEY_BACKSPACE, VK_BACK }, // (0x08) // backspace
  38. { KEY_TAB, VK_TAB }, //(0x09)
  39. //VK_CLEAR (0x0C)
  40. { KEY_ENTER, VK_RETURN }, //(0x0D)
  41. { KEY_SHIFT, VK_SHIFT }, //(0x10)
  42. { KEY_CONTROL, VK_CONTROL }, //(0x11)
  43. { KEY_ALT, VK_MENU }, //(0x12)
  44. { KEY_PAUSE, VK_PAUSE }, //(0x13)
  45. { KEY_CAPSLOCK, VK_CAPITAL }, //(0x14)
  46. { KEY_ESCAPE, VK_ESCAPE }, //(0x1B)
  47. { KEY_SPACE, VK_SPACE }, //(0x20)
  48. { KEY_PAGEUP, VK_PRIOR }, //(0x21)
  49. { KEY_PAGEDOWN, VK_NEXT }, //(0x22)
  50. { KEY_END, VK_END }, //(0x23)
  51. { KEY_HOME, VK_HOME }, //(0x24)
  52. { KEY_LEFT, VK_LEFT }, //(0x25)
  53. { KEY_UP, VK_UP }, //(0x26)
  54. { KEY_RIGHT, VK_RIGHT }, //(0x27)
  55. { KEY_DOWN, VK_DOWN }, // (0x28)
  56. //VK_SELECT (0x29)
  57. { KEY_PRINT, VK_PRINT }, // (0x2A)
  58. //VK_EXECUTE (0x2B)
  59. { KEY_PRINT, VK_SNAPSHOT }, // (0x2C)
  60. { KEY_INSERT, VK_INSERT }, // (0x2D)
  61. { KEY_DELETE, VK_DELETE }, // (0x2E)
  62. { KEY_HELP, VK_HELP }, // (0x2F)
  63. { KEY_0, (0x30) }, ////0 key
  64. { KEY_1, (0x31) }, ////1 key
  65. { KEY_2, (0x32) }, ////2 key
  66. { KEY_3, (0x33) }, ////3 key
  67. { KEY_4, (0x34) }, ////4 key
  68. { KEY_5, (0x35) }, ////5 key
  69. { KEY_6, (0x36) }, ////6 key
  70. { KEY_7, (0x37) }, ////7 key
  71. { KEY_8, (0x38) }, ////8 key
  72. { KEY_9, (0x39) }, ////9 key
  73. { KEY_A, (0x41) }, ////A key
  74. { KEY_B, (0x42) }, ////B key
  75. { KEY_C, (0x43) }, ////C key
  76. { KEY_D, (0x44) }, ////D key
  77. { KEY_E, (0x45) }, ////E key
  78. { KEY_F, (0x46) }, ////F key
  79. { KEY_G, (0x47) }, ////G key
  80. { KEY_H, (0x48) }, ////H key
  81. { KEY_I, (0x49) }, ////I key
  82. { KEY_J, (0x4A) }, ////J key
  83. { KEY_K, (0x4B) }, ////K key
  84. { KEY_L, (0x4C) }, ////L key
  85. { KEY_M, (0x4D) }, ////M key
  86. { KEY_N, (0x4E) }, ////N key
  87. { KEY_O, (0x4F) }, ////O key
  88. { KEY_P, (0x50) }, ////P key
  89. { KEY_Q, (0x51) }, ////Q key
  90. { KEY_R, (0x52) }, ////R key
  91. { KEY_S, (0x53) }, ////S key
  92. { KEY_T, (0x54) }, ////T key
  93. { KEY_U, (0x55) }, ////U key
  94. { KEY_V, (0x56) }, ////V key
  95. { KEY_W, (0x57) }, ////W key
  96. { KEY_X, (0x58) }, ////X key
  97. { KEY_Y, (0x59) }, ////Y key
  98. { KEY_Z, (0x5A) }, ////Z key
  99. { KEY_MASK_META, VK_LWIN }, //(0x5B)
  100. { KEY_MASK_META, VK_RWIN }, //(0x5C)
  101. { KEY_MENU, VK_APPS }, //(0x5D)
  102. { KEY_STANDBY, VK_SLEEP }, //(0x5F)
  103. { KEY_KP_0, VK_NUMPAD0 }, //(0x60)
  104. { KEY_KP_1, VK_NUMPAD1 }, //(0x61)
  105. { KEY_KP_2, VK_NUMPAD2 }, //(0x62)
  106. { KEY_KP_3, VK_NUMPAD3 }, //(0x63)
  107. { KEY_KP_4, VK_NUMPAD4 }, //(0x64)
  108. { KEY_KP_5, VK_NUMPAD5 }, //(0x65)
  109. { KEY_KP_6, VK_NUMPAD6 }, //(0x66)
  110. { KEY_KP_7, VK_NUMPAD7 }, //(0x67)
  111. { KEY_KP_8, VK_NUMPAD8 }, //(0x68)
  112. { KEY_KP_9, VK_NUMPAD9 }, //(0x69)
  113. { KEY_KP_MULTIPLY, VK_MULTIPLY }, // (0x6A)
  114. { KEY_KP_ADD, VK_ADD }, // (0x6B)
  115. //VK_SEPARATOR (0x6C)
  116. { KEY_KP_SUBTRACT, VK_SUBTRACT }, // (0x6D)
  117. { KEY_KP_PERIOD, VK_DECIMAL }, // (0x6E)
  118. { KEY_KP_DIVIDE, VK_DIVIDE }, // (0x6F)
  119. { KEY_F1, VK_F1 }, // (0x70)
  120. { KEY_F2, VK_F2 }, // (0x71)
  121. { KEY_F3, VK_F3 }, // (0x72)
  122. { KEY_F4, VK_F4 }, // (0x73)
  123. { KEY_F5, VK_F5 }, // (0x74)
  124. { KEY_F6, VK_F6 }, // (0x75)
  125. { KEY_F7, VK_F7 }, // (0x76)
  126. { KEY_F8, VK_F8 }, // (0x77)
  127. { KEY_F9, VK_F9 }, // (0x78)
  128. { KEY_F10, VK_F10 }, // (0x79)
  129. { KEY_F11, VK_F11 }, // (0x7A)
  130. { KEY_F12, VK_F12 }, // (0x7B)
  131. { KEY_F13, VK_F13 }, // (0x7C)
  132. { KEY_F14, VK_F14 }, // (0x7D)
  133. { KEY_F15, VK_F15 }, // (0x7E)
  134. { KEY_F16, VK_F16 }, // (0x7F)
  135. { KEY_NUMLOCK, VK_NUMLOCK }, // (0x90)
  136. { KEY_SCROLLLOCK, VK_SCROLL }, // (0x91)
  137. { KEY_SHIFT, VK_LSHIFT }, // (0xA0)
  138. { KEY_SHIFT, VK_RSHIFT }, // (0xA1)
  139. { KEY_CONTROL, VK_LCONTROL }, // (0xA2)
  140. { KEY_CONTROL, VK_RCONTROL }, // (0xA3)
  141. { KEY_MENU, VK_LMENU }, // (0xA4)
  142. { KEY_MENU, VK_RMENU }, // (0xA5)
  143. { KEY_BACK, VK_BROWSER_BACK }, // (0xA6)
  144. { KEY_FORWARD, VK_BROWSER_FORWARD }, // (0xA7)
  145. { KEY_REFRESH, VK_BROWSER_REFRESH }, // (0xA8)
  146. { KEY_STOP, VK_BROWSER_STOP }, // (0xA9)
  147. { KEY_SEARCH, VK_BROWSER_SEARCH }, // (0xAA)
  148. { KEY_FAVORITES, VK_BROWSER_FAVORITES }, // (0xAB)
  149. { KEY_HOMEPAGE, VK_BROWSER_HOME }, // (0xAC)
  150. { KEY_VOLUMEMUTE, VK_VOLUME_MUTE }, // (0xAD)
  151. { KEY_VOLUMEDOWN, VK_VOLUME_DOWN }, // (0xAE)
  152. { KEY_VOLUMEUP, VK_VOLUME_UP }, // (0xAF)
  153. { KEY_MEDIANEXT, VK_MEDIA_NEXT_TRACK }, // (0xB0)
  154. { KEY_MEDIAPREVIOUS, VK_MEDIA_PREV_TRACK }, // (0xB1)
  155. { KEY_MEDIASTOP, VK_MEDIA_STOP }, // (0xB2)
  156. //VK_MEDIA_PLAY_PAUSE (0xB3)
  157. { KEY_LAUNCHMAIL, VK_LAUNCH_MAIL }, // (0xB4)
  158. { KEY_LAUNCHMEDIA, VK_LAUNCH_MEDIA_SELECT }, // (0xB5)
  159. { KEY_LAUNCH0, VK_LAUNCH_APP1 }, // (0xB6)
  160. { KEY_LAUNCH1, VK_LAUNCH_APP2 }, // (0xB7)
  161. { KEY_SEMICOLON, VK_OEM_1 }, // (0xBA)
  162. { KEY_EQUAL, VK_OEM_PLUS }, // (0xBB) // Windows 2000/XP: For any country/region, the '+' key
  163. { KEY_COMMA, VK_OEM_COMMA }, // (0xBC) // Windows 2000/XP: For any country/region, the ',' key
  164. { KEY_MINUS, VK_OEM_MINUS }, // (0xBD) // Windows 2000/XP: For any country/region, the '-' key
  165. { KEY_PERIOD, VK_OEM_PERIOD }, // (0xBE) // Windows 2000/XP: For any country/region, the '.' key
  166. { KEY_SLASH, VK_OEM_2 }, // (0xBF) //Windows 2000/XP: For the US standard keyboard, the '/?' key
  167. { KEY_QUOTELEFT, VK_OEM_3 }, // (0xC0)
  168. { KEY_BRACKETLEFT, VK_OEM_4 }, // (0xDB)
  169. { KEY_BACKSLASH, VK_OEM_5 }, // (0xDC)
  170. { KEY_BRACKETRIGHT, VK_OEM_6 }, // (0xDD)
  171. { KEY_APOSTROPHE, VK_OEM_7 }, // (0xDE)
  172. /*
  173. {VK_OEM_8 (0xDF)
  174. {VK_OEM_102 (0xE2) // Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
  175. */
  176. //{ KEY_PLAY, VK_PLAY},// (0xFA)
  177. { KEY_UNKNOWN, 0 }
  178. };
  179. /*
  180. VK_ZOOM (0xFB)
  181. VK_NONAME (0xFC)
  182. VK_PA1 (0xFD)
  183. VK_OEM_CLEAR (0xFE)
  184. */
  185. static _WinTranslatePair _scancode_to_keycode[] = {
  186. { KEY_ESCAPE, 0x01 },
  187. { KEY_1, 0x02 },
  188. { KEY_2, 0x03 },
  189. { KEY_3, 0x04 },
  190. { KEY_4, 0x05 },
  191. { KEY_5, 0x06 },
  192. { KEY_6, 0x07 },
  193. { KEY_7, 0x08 },
  194. { KEY_8, 0x09 },
  195. { KEY_9, 0x0A },
  196. { KEY_0, 0x0B },
  197. { KEY_MINUS, 0x0C },
  198. { KEY_EQUAL, 0x0D },
  199. { KEY_BACKSPACE, 0x0E },
  200. { KEY_TAB, 0x0F },
  201. { KEY_Q, 0x10 },
  202. { KEY_W, 0x11 },
  203. { KEY_E, 0x12 },
  204. { KEY_R, 0x13 },
  205. { KEY_T, 0x14 },
  206. { KEY_Y, 0x15 },
  207. { KEY_U, 0x16 },
  208. { KEY_I, 0x17 },
  209. { KEY_O, 0x18 },
  210. { KEY_P, 0x19 },
  211. { KEY_BRACELEFT, 0x1A },
  212. { KEY_BRACERIGHT, 0x1B },
  213. { KEY_ENTER, 0x1C },
  214. { KEY_CONTROL, 0x1D },
  215. { KEY_A, 0x1E },
  216. { KEY_S, 0x1F },
  217. { KEY_D, 0x20 },
  218. { KEY_F, 0x21 },
  219. { KEY_G, 0x22 },
  220. { KEY_H, 0x23 },
  221. { KEY_J, 0x24 },
  222. { KEY_K, 0x25 },
  223. { KEY_L, 0x26 },
  224. { KEY_SEMICOLON, 0x27 },
  225. { KEY_APOSTROPHE, 0x28 },
  226. { KEY_QUOTELEFT, 0x29 },
  227. { KEY_SHIFT, 0x2A },
  228. { KEY_BACKSLASH, 0x2B },
  229. { KEY_Z, 0x2C },
  230. { KEY_X, 0x2D },
  231. { KEY_C, 0x2E },
  232. { KEY_V, 0x2F },
  233. { KEY_B, 0x30 },
  234. { KEY_N, 0x31 },
  235. { KEY_M, 0x32 },
  236. { KEY_COMMA, 0x33 },
  237. { KEY_PERIOD, 0x34 },
  238. { KEY_SLASH, 0x35 },
  239. { KEY_SHIFT, 0x36 },
  240. { KEY_PRINT, 0x37 },
  241. { KEY_ALT, 0x38 },
  242. { KEY_SPACE, 0x39 },
  243. { KEY_CAPSLOCK, 0x3A },
  244. { KEY_F1, 0x3B },
  245. { KEY_F2, 0x3C },
  246. { KEY_F3, 0x3D },
  247. { KEY_F4, 0x3E },
  248. { KEY_F5, 0x3F },
  249. { KEY_F6, 0x40 },
  250. { KEY_F7, 0x41 },
  251. { KEY_F8, 0x42 },
  252. { KEY_F9, 0x43 },
  253. { KEY_F10, 0x44 },
  254. { KEY_NUMLOCK, 0x45 },
  255. { KEY_SCROLLLOCK, 0x46 },
  256. { KEY_HOME, 0x47 },
  257. { KEY_UP, 0x48 },
  258. { KEY_PAGEUP, 0x49 },
  259. { KEY_KP_SUBTRACT, 0x4A },
  260. { KEY_LEFT, 0x4B },
  261. { KEY_KP_5, 0x4C },
  262. { KEY_RIGHT, 0x4D },
  263. { KEY_KP_ADD, 0x4E },
  264. { KEY_END, 0x4F },
  265. { KEY_DOWN, 0x50 },
  266. { KEY_PAGEDOWN, 0x51 },
  267. { KEY_INSERT, 0x52 },
  268. { KEY_DELETE, 0x53 },
  269. //{ KEY_???, 0x56 }, //NON US BACKSLASH
  270. { KEY_F11, 0x57 },
  271. { KEY_F12, 0x58 },
  272. { KEY_META, 0x5B },
  273. { KEY_META, 0x5C },
  274. { KEY_MENU, 0x5D },
  275. { KEY_F13, 0x64 },
  276. { KEY_F14, 0x65 },
  277. { KEY_F15, 0x66 },
  278. { KEY_F16, 0x67 },
  279. { KEY_UNKNOWN, 0 }
  280. };
  281. unsigned int KeyMappingWindows::get_keysym(unsigned int p_code) {
  282. for (int i = 0; _vk_to_keycode[i].keysym != KEY_UNKNOWN; i++) {
  283. if (_vk_to_keycode[i].keycode == p_code) {
  284. //printf("outcode: %x\n",_vk_to_keycode[i].keysym);
  285. return _vk_to_keycode[i].keysym;
  286. }
  287. }
  288. return KEY_UNKNOWN;
  289. }
  290. unsigned int KeyMappingWindows::get_scancode(unsigned int p_keycode) {
  291. for (int i = 0; _scancode_to_keycode[i].keysym != KEY_UNKNOWN; i++) {
  292. if (_scancode_to_keycode[i].keysym == p_keycode) {
  293. return _scancode_to_keycode[i].keycode;
  294. }
  295. }
  296. return 0;
  297. }
  298. unsigned int KeyMappingWindows::get_scansym(unsigned int p_code, bool p_extended) {
  299. unsigned int keycode = KEY_UNKNOWN;
  300. for (int i = 0; _scancode_to_keycode[i].keysym != KEY_UNKNOWN; i++) {
  301. if (_scancode_to_keycode[i].keycode == p_code) {
  302. keycode = _scancode_to_keycode[i].keysym;
  303. break;
  304. }
  305. }
  306. if (p_extended) {
  307. switch (keycode) {
  308. case KEY_ENTER: {
  309. keycode = KEY_KP_ENTER;
  310. } break;
  311. case KEY_SLASH: {
  312. keycode = KEY_KP_DIVIDE;
  313. } break;
  314. case KEY_CAPSLOCK: {
  315. keycode = KEY_KP_ADD;
  316. } break;
  317. }
  318. } else {
  319. switch (keycode) {
  320. case KEY_NUMLOCK: {
  321. keycode = KEY_PAUSE;
  322. } break;
  323. case KEY_HOME: {
  324. keycode = KEY_KP_7;
  325. } break;
  326. case KEY_UP: {
  327. keycode = KEY_KP_8;
  328. } break;
  329. case KEY_PAGEUP: {
  330. keycode = KEY_KP_9;
  331. } break;
  332. case KEY_LEFT: {
  333. keycode = KEY_KP_4;
  334. } break;
  335. case KEY_RIGHT: {
  336. keycode = KEY_KP_6;
  337. } break;
  338. case KEY_END: {
  339. keycode = KEY_KP_1;
  340. } break;
  341. case KEY_DOWN: {
  342. keycode = KEY_KP_2;
  343. } break;
  344. case KEY_PAGEDOWN: {
  345. keycode = KEY_KP_3;
  346. } break;
  347. case KEY_INSERT: {
  348. keycode = KEY_KP_0;
  349. } break;
  350. case KEY_DELETE: {
  351. keycode = KEY_KP_PERIOD;
  352. } break;
  353. case KEY_PRINT: {
  354. keycode = KEY_KP_MULTIPLY;
  355. } break;
  356. }
  357. }
  358. return keycode;
  359. }
  360. bool KeyMappingWindows::is_extended_key(unsigned int p_code) {
  361. return p_code == VK_INSERT ||
  362. p_code == VK_DELETE ||
  363. p_code == VK_HOME ||
  364. p_code == VK_END ||
  365. p_code == VK_PRIOR ||
  366. p_code == VK_NEXT ||
  367. p_code == VK_LEFT ||
  368. p_code == VK_UP ||
  369. p_code == VK_RIGHT ||
  370. p_code == VK_DOWN;
  371. }