Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

911 lines
26 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #if defined( WIN32 )
  7. #if !defined( _X360 )
  8. #include <wtypes.h>
  9. #include <winuser.h>
  10. #include "xbox/xboxstubs.h"
  11. #else
  12. #include "xbox/xbox_win32stubs.h"
  13. #endif
  14. #endif // WIN32
  15. #include "key_translation.h"
  16. #include "tier1/convar.h"
  17. #include "tier1/strtools.h"
  18. #include "tier0/dbg.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. #if defined(__clang__)
  22. #pragma GCC diagnostic ignored "-Wchar-subscripts"
  23. #endif
  24. static ButtonCode_t s_pVirtualKeyToButtonCode[256];
  25. static ButtonCode_t s_pSKeytoButtonCode[SK_MAX_KEYS];
  26. #if !defined( POSIX )
  27. static ButtonCode_t s_pXKeyTrans[XK_MAX_KEYS];
  28. #endif
  29. #define SCONTROLLERBUTTONS_BUTTONS( x ) \
  30. "SC_A",\
  31. "SC_B",\
  32. "SC_X",\
  33. "SC_Y",\
  34. "SC_DPAD_UP",\
  35. "SC_DPAD_RIGHT",\
  36. "SC_DPAD_DOWN",\
  37. "SC_DPAD_LEFT",\
  38. "SC_LEFT_BUMPER",\
  39. "SC_RIGHT_BUMPER",\
  40. "SC_LEFT_TRIGGER",\
  41. "SC_RIGHT_TRIGGER",\
  42. "SC_LEFT_GRIP",\
  43. "SC_RIGHT_GRIP",\
  44. "SC_LEFT_PAD_TOUCH",\
  45. "SC_RIGHT_PAD_TOUCH",\
  46. "SC_LEFT_PAD_CLICK",\
  47. "SC_RIGHT_PAD_CLICK",\
  48. "SC_LPAD_UP",\
  49. "SC_LPAD_RIGHT",\
  50. "SC_LPAD_DOWN",\
  51. "SC_LPAD_LEFT",\
  52. "SC_RPAD_UP",\
  53. "SC_RPAD_RIGHT",\
  54. "SC_RPAD_DOWN",\
  55. "SC_RPAD_LEFT",\
  56. "SC_SELECT",\
  57. "SC_START",\
  58. "SC_STEAM",\
  59. "SC_NULL"
  60. #define SCONTROLLERBUTTONS_AXIS( x ) \
  61. "SC_LPAD_AXIS_RIGHT",\
  62. "SC_LPAD_AXIS_LEFT",\
  63. "SC_LPAD_AXIS_DOWN",\
  64. "SC_LPAD_AXIS_UP",\
  65. "SC_AXIS_L_TRIGGER",\
  66. "SC_AXIS_R_TRIGGER",\
  67. "SC_RPAD_AXIS_RIGHT",\
  68. "SC_RPAD_AXIS_LEFT",\
  69. "SC_RPAD_AXIS_DOWN",\
  70. "SC_RPAD_AXIS_UP",\
  71. "SC_GYRO_AXIS_PITCH_POSITIVE",\
  72. "SC_GYRO_AXIS_PITCH_NEGATIVE",\
  73. "SC_GYRO_AXIS_ROLL_POSITIVE",\
  74. "SC_GYRO_AXIS_ROLL_NEGATIVE",\
  75. "SC_GYRO_AXIS_YAW_POSITIVE",\
  76. "SC_GYRO_AXIS_YAW_NEGATIVE"
  77. #define SCONTROLLERBUTTONS_VBUTTONS( x ) \
  78. "SC_F1",\
  79. "SC_F2",\
  80. "SC_F3",\
  81. "SC_F4",\
  82. "SC_F5",\
  83. "SC_F6",\
  84. "SC_F7",\
  85. "SC_F8",\
  86. "SC_F9",\
  87. "SC_F10",\
  88. "SC_F11",\
  89. "SC_F12"
  90. static int s_pButtonCodeToVirtual[BUTTON_CODE_LAST];
  91. static const char *s_pButtonCodeName[ ] =
  92. {
  93. "", // KEY_NONE
  94. "0", // KEY_0,
  95. "1", // KEY_1,
  96. "2", // KEY_2,
  97. "3", // KEY_3,
  98. "4", // KEY_4,
  99. "5", // KEY_5,
  100. "6", // KEY_6,
  101. "7", // KEY_7,
  102. "8", // KEY_8,
  103. "9", // KEY_9,
  104. "a", // KEY_A,
  105. "b", // KEY_B,
  106. "c", // KEY_C,
  107. "d", // KEY_D,
  108. "e", // KEY_E,
  109. "f", // KEY_F,
  110. "g", // KEY_G,
  111. "h", // KEY_H,
  112. "i", // KEY_I,
  113. "j", // KEY_J,
  114. "k", // KEY_K,
  115. "l", // KEY_L,
  116. "m", // KEY_M,
  117. "n", // KEY_N,
  118. "o", // KEY_O,
  119. "p", // KEY_P,
  120. "q", // KEY_Q,
  121. "r", // KEY_R,
  122. "s", // KEY_S,
  123. "t", // KEY_T,
  124. "u", // KEY_U,
  125. "v", // KEY_V,
  126. "w", // KEY_W,
  127. "x", // KEY_X,
  128. "y", // KEY_Y,
  129. "z", // KEY_Z,
  130. "KP_INS", // KEY_PAD_0,
  131. "KP_END", // KEY_PAD_1,
  132. "KP_DOWNARROW", // KEY_PAD_2,
  133. "KP_PGDN", // KEY_PAD_3,
  134. "KP_LEFTARROW", // KEY_PAD_4,
  135. "KP_5", // KEY_PAD_5,
  136. "KP_RIGHTARROW",// KEY_PAD_6,
  137. "KP_HOME", // KEY_PAD_7,
  138. "KP_UPARROW", // KEY_PAD_8,
  139. "KP_PGUP", // KEY_PAD_9,
  140. "KP_SLASH", // KEY_PAD_DIVIDE,
  141. "KP_MULTIPLY", // KEY_PAD_MULTIPLY,
  142. "KP_MINUS", // KEY_PAD_MINUS,
  143. "KP_PLUS", // KEY_PAD_PLUS,
  144. "KP_ENTER", // KEY_PAD_ENTER,
  145. "KP_DEL", // KEY_PAD_DECIMAL,
  146. "[", // KEY_LBRACKET,
  147. "]", // KEY_RBRACKET,
  148. "SEMICOLON", // KEY_SEMICOLON,
  149. "'", // KEY_APOSTROPHE,
  150. "`", // KEY_BACKQUOTE,
  151. ",", // KEY_COMMA,
  152. ".", // KEY_PERIOD,
  153. "/", // KEY_SLASH,
  154. "\\", // KEY_BACKSLASH,
  155. "-", // KEY_MINUS,
  156. "=", // KEY_EQUAL,
  157. "ENTER", // KEY_ENTER,
  158. "SPACE", // KEY_SPACE,
  159. "BACKSPACE", // KEY_BACKSPACE,
  160. "TAB", // KEY_TAB,
  161. "CAPSLOCK", // KEY_CAPSLOCK,
  162. "NUMLOCK", // KEY_NUMLOCK,
  163. "ESCAPE", // KEY_ESCAPE,
  164. "SCROLLLOCK", // KEY_SCROLLLOCK,
  165. "INS", // KEY_INSERT,
  166. "DEL", // KEY_DELETE,
  167. "HOME", // KEY_HOME,
  168. "END", // KEY_END,
  169. "PGUP", // KEY_PAGEUP,
  170. "PGDN", // KEY_PAGEDOWN,
  171. "PAUSE", // KEY_BREAK,
  172. "SHIFT", // KEY_LSHIFT,
  173. "RSHIFT", // KEY_RSHIFT,
  174. "ALT", // KEY_LALT,
  175. "RALT", // KEY_RALT,
  176. "CTRL", // KEY_LCONTROL,
  177. "RCTRL", // KEY_RCONTROL,
  178. "LWIN", // KEY_LWIN,
  179. "RWIN", // KEY_RWIN,
  180. "APP", // KEY_APP,
  181. "UPARROW", // KEY_UP,
  182. "LEFTARROW", // KEY_LEFT,
  183. "DOWNARROW", // KEY_DOWN,
  184. "RIGHTARROW", // KEY_RIGHT,
  185. "F1", // KEY_F1,
  186. "F2", // KEY_F2,
  187. "F3", // KEY_F3,
  188. "F4", // KEY_F4,
  189. "F5", // KEY_F5,
  190. "F6", // KEY_F6,
  191. "F7", // KEY_F7,
  192. "F8", // KEY_F8,
  193. "F9", // KEY_F9,
  194. "F10", // KEY_F10,
  195. "F11", // KEY_F11,
  196. "F12", // KEY_F12,
  197. // FIXME: CAPSLOCK/NUMLOCK/SCROLLLOCK all appear above. What are these for?!
  198. // They only appear in CInputWin32::UpdateToggleButtonState in vgui2
  199. "CAPSLOCKTOGGLE", // KEY_CAPSLOCKTOGGLE,
  200. "NUMLOCKTOGGLE", // KEY_NUMLOCKTOGGLE,
  201. "SCROLLLOCKTOGGLE", // KEY_SCROLLLOCKTOGGLE,
  202. // Mouse
  203. "MOUSE1", // MOUSE_LEFT,
  204. "MOUSE2", // MOUSE_RIGHT,
  205. "MOUSE3", // MOUSE_MIDDLE,
  206. "MOUSE4", // MOUSE_4,
  207. "MOUSE5", // MOUSE_5,
  208. "MWHEELUP", // MOUSE_WHEEL_UP
  209. "MWHEELDOWN", // MOUSE_WHEEL_DOWN
  210. #if defined ( _X360 ) || defined ( _LINUX )
  211. "A_BUTTON", // JOYSTICK_FIRST_BUTTON
  212. "B_BUTTON",
  213. "X_BUTTON",
  214. "Y_BUTTON",
  215. "L_SHOULDER",
  216. "R_SHOULDER",
  217. "BACK",
  218. "START",
  219. "STICK1",
  220. "STICK2",
  221. #else
  222. "JOY1", // JOYSTICK_FIRST_BUTTON
  223. "JOY2",
  224. "JOY3",
  225. "JOY4",
  226. "JOY5",
  227. "JOY6",
  228. "JOY7",
  229. "JOY8",
  230. "JOY9",
  231. "JOY10",
  232. #endif
  233. "JOY11",
  234. "JOY12",
  235. "JOY13",
  236. "JOY14",
  237. "JOY15",
  238. "JOY16",
  239. "JOY17",
  240. "JOY18",
  241. "JOY19",
  242. "JOY20",
  243. "JOY21",
  244. "JOY22",
  245. "JOY23",
  246. "JOY24",
  247. "JOY25",
  248. "JOY26",
  249. "JOY27",
  250. "JOY28",
  251. "JOY29",
  252. "JOY30",
  253. "JOY31",
  254. "JOY32", // JOYSTICK_LAST_BUTTON
  255. #if defined ( _X360 )
  256. "UP", // JOYSTICK_FIRST_POV_BUTTON
  257. "RIGHT",
  258. "DOWN",
  259. "LEFT", // JOYSTICK_LAST_POV_BUTTON
  260. "S1_RIGHT", // JOYSTICK_FIRST_AXIS_BUTTON
  261. "S1_LEFT",
  262. "S1_DOWN",
  263. "S1_UP",
  264. "L_TRIGGER",
  265. "R_TRIGGER",
  266. "S2_RIGHT",
  267. "S2_LEFT",
  268. "S2_DOWN",
  269. "S2_UP", // JOYSTICK_LAST_AXIS_BUTTON
  270. "V AXIS POS",
  271. "V AXIS NEG",
  272. #else
  273. "POV_UP", // JOYSTICK_FIRST_POV_BUTTON
  274. "POV_RIGHT",
  275. "POV_DOWN",
  276. "POV_LEFT", // JOYSTICK_LAST_POV_BUTTON
  277. "X AXIS POS", // JOYSTICK_FIRST_AXIS_BUTTON
  278. "X AXIS NEG",
  279. "Y AXIS POS",
  280. "Y AXIS NEG",
  281. "Z AXIS POS",
  282. "Z AXIS NEG",
  283. "R AXIS POS",
  284. "R AXIS NEG",
  285. "U AXIS POS",
  286. "U AXIS NEG",
  287. "V AXIS POS",
  288. "V AXIS NEG", // JOYSTICK_LAST_AXIS_BUTTON
  289. "FALCON_NULL", // NVNT temp Fix for unaligned joystick enumeration
  290. "FALCON_1", // NOVINT_FIRST
  291. "FALCON_2",
  292. "FALCON_3",
  293. "FALCON_4",
  294. "FALCON2_1",
  295. "FALCON2_2",
  296. "FALCON2_3",
  297. "FALCON2_4", // NOVINT_LAST
  298. #endif
  299. SCONTROLLERBUTTONS_BUTTONS( 0 ),
  300. SCONTROLLERBUTTONS_BUTTONS( 1 ),
  301. SCONTROLLERBUTTONS_BUTTONS( 2 ),
  302. SCONTROLLERBUTTONS_BUTTONS( 3 ),
  303. SCONTROLLERBUTTONS_BUTTONS( 4 ),
  304. SCONTROLLERBUTTONS_BUTTONS( 5 ),
  305. SCONTROLLERBUTTONS_BUTTONS( 6 ),
  306. SCONTROLLERBUTTONS_BUTTONS( 7 ),
  307. SCONTROLLERBUTTONS_AXIS( 0 ),
  308. SCONTROLLERBUTTONS_AXIS( 1 ),
  309. SCONTROLLERBUTTONS_AXIS( 2 ),
  310. SCONTROLLERBUTTONS_AXIS( 3 ),
  311. SCONTROLLERBUTTONS_AXIS( 4 ),
  312. SCONTROLLERBUTTONS_AXIS( 5 ),
  313. SCONTROLLERBUTTONS_AXIS( 6 ),
  314. SCONTROLLERBUTTONS_AXIS( 7 ),
  315. SCONTROLLERBUTTONS_VBUTTONS( 0 ),
  316. SCONTROLLERBUTTONS_VBUTTONS( 1 ),
  317. SCONTROLLERBUTTONS_VBUTTONS( 2 ),
  318. SCONTROLLERBUTTONS_VBUTTONS( 3 ),
  319. SCONTROLLERBUTTONS_VBUTTONS( 4 ),
  320. SCONTROLLERBUTTONS_VBUTTONS( 5 ),
  321. SCONTROLLERBUTTONS_VBUTTONS( 6 ),
  322. SCONTROLLERBUTTONS_VBUTTONS( 7 ),
  323. };
  324. static const char *s_pAnalogCodeName[ ] =
  325. {
  326. "MOUSE_X", // MOUSE_X = 0,
  327. "MOUSE_Y", // MOUSE_Y,
  328. "MOUSE_XY", // MOUSE_XY, // Invoked when either x or y changes
  329. "MOUSE_WHEEL", // MOUSE_WHEEL,
  330. "X AXIS", // JOY_AXIS_X
  331. "Y AXIS", // JOY_AXIS_Y
  332. "Z AXIS", // JOY_AXIS_Z
  333. "R AXIS", // JOY_AXIS_R
  334. "U AXIS", // JOY_AXIS_U
  335. "V AXIS", // JOY_AXIS_V
  336. };
  337. #if !defined ( _X360 )
  338. static const char *s_pXControllerButtonCodeNames[ ] =
  339. {
  340. "A_BUTTON", // JOYSTICK_FIRST_BUTTON
  341. "B_BUTTON",
  342. "X_BUTTON",
  343. "Y_BUTTON",
  344. "L_SHOULDER",
  345. "R_SHOULDER",
  346. "BACK",
  347. "START",
  348. "STICK1",
  349. "STICK2",
  350. "JOY11",
  351. "JOY12",
  352. "JOY13",
  353. "JOY14",
  354. "JOY15",
  355. "JOY16",
  356. "JOY17",
  357. "JOY18",
  358. "JOY19",
  359. "JOY20",
  360. "JOY21",
  361. "JOY22",
  362. "JOY23",
  363. "JOY24",
  364. "JOY25",
  365. "JOY26",
  366. "JOY27",
  367. "JOY28",
  368. "JOY29",
  369. "JOY30",
  370. "JOY31",
  371. "JOY32", // JOYSTICK_LAST_BUTTON
  372. "UP", // JOYSTICK_FIRST_POV_BUTTON
  373. "RIGHT",
  374. "DOWN",
  375. "LEFT", // JOYSTICK_LAST_POV_BUTTON
  376. "S1_RIGHT", // JOYSTICK_FIRST_AXIS_BUTTON
  377. "S1_LEFT",
  378. "S1_DOWN",
  379. "S1_UP",
  380. "L_TRIGGER",
  381. "R_TRIGGER",
  382. "S2_RIGHT",
  383. "S2_LEFT",
  384. "S2_DOWN",
  385. "S2_UP", // JOYSTICK_LAST_AXIS_BUTTON
  386. "V AXIS POS",
  387. "V AXIS NEG",
  388. };
  389. #endif
  390. // this maps non-translated keyboard scan codes to engine key codes
  391. // Google for 'Keyboard Scan Code Specification'
  392. static ButtonCode_t s_pScanToButtonCode_QWERTY[128] =
  393. {
  394. // 0 1 2 3 4 5 6 7
  395. // 8 9 A B C D E F
  396. KEY_NONE, KEY_ESCAPE, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, // 0
  397. KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS, KEY_EQUAL, KEY_BACKSPACE, KEY_TAB, // 0
  398. KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, // 1
  399. KEY_O, KEY_P, KEY_LBRACKET, KEY_RBRACKET, KEY_ENTER, KEY_LCONTROL, KEY_A, KEY_S, // 1
  400. KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON, // 2
  401. KEY_APOSTROPHE, KEY_BACKQUOTE, KEY_LSHIFT, KEY_BACKSLASH, KEY_Z, KEY_X, KEY_C, KEY_V, // 2
  402. KEY_B, KEY_N, KEY_M, KEY_COMMA, KEY_PERIOD, KEY_SLASH, KEY_RSHIFT, KEY_PAD_MULTIPLY,// 3
  403. KEY_LALT, KEY_SPACE, KEY_CAPSLOCK, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, // 3
  404. KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_NUMLOCK, KEY_SCROLLLOCK, KEY_HOME, // 4
  405. KEY_UP, KEY_PAGEUP, KEY_PAD_MINUS, KEY_LEFT, KEY_PAD_5, KEY_RIGHT, KEY_PAD_PLUS, KEY_END, // 4
  406. KEY_DOWN, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_F11, // 5
  407. KEY_F12, KEY_BREAK, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, // 5
  408. KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, // 6
  409. KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, // 6
  410. KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, // 7
  411. KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE // 7
  412. };
  413. static ButtonCode_t s_pScanToButtonCode[128];
  414. void ButtonCode_InitKeyTranslationTable()
  415. {
  416. COMPILE_TIME_ASSERT( sizeof(s_pButtonCodeName) / sizeof( const char * ) == BUTTON_CODE_LAST );
  417. COMPILE_TIME_ASSERT( sizeof(s_pAnalogCodeName) / sizeof( const char * ) == ANALOG_CODE_LAST );
  418. // set virtual key translation table
  419. memset( s_pVirtualKeyToButtonCode, KEY_NONE, sizeof(s_pVirtualKeyToButtonCode) );
  420. s_pVirtualKeyToButtonCode['0'] =KEY_0;
  421. s_pVirtualKeyToButtonCode['1'] =KEY_1;
  422. s_pVirtualKeyToButtonCode['2'] =KEY_2;
  423. s_pVirtualKeyToButtonCode['3'] =KEY_3;
  424. s_pVirtualKeyToButtonCode['4'] =KEY_4;
  425. s_pVirtualKeyToButtonCode['5'] =KEY_5;
  426. s_pVirtualKeyToButtonCode['6'] =KEY_6;
  427. s_pVirtualKeyToButtonCode['7'] =KEY_7;
  428. s_pVirtualKeyToButtonCode['8'] =KEY_8;
  429. s_pVirtualKeyToButtonCode['9'] =KEY_9;
  430. s_pVirtualKeyToButtonCode['A'] =KEY_A;
  431. s_pVirtualKeyToButtonCode['B'] =KEY_B;
  432. s_pVirtualKeyToButtonCode['C'] =KEY_C;
  433. s_pVirtualKeyToButtonCode['D'] =KEY_D;
  434. s_pVirtualKeyToButtonCode['E'] =KEY_E;
  435. s_pVirtualKeyToButtonCode['F'] =KEY_F;
  436. s_pVirtualKeyToButtonCode['G'] =KEY_G;
  437. s_pVirtualKeyToButtonCode['H'] =KEY_H;
  438. s_pVirtualKeyToButtonCode['I'] =KEY_I;
  439. s_pVirtualKeyToButtonCode['J'] =KEY_J;
  440. s_pVirtualKeyToButtonCode['K'] =KEY_K;
  441. s_pVirtualKeyToButtonCode['L'] =KEY_L;
  442. s_pVirtualKeyToButtonCode['M'] =KEY_M;
  443. s_pVirtualKeyToButtonCode['N'] =KEY_N;
  444. s_pVirtualKeyToButtonCode['O'] =KEY_O;
  445. s_pVirtualKeyToButtonCode['P'] =KEY_P;
  446. s_pVirtualKeyToButtonCode['Q'] =KEY_Q;
  447. s_pVirtualKeyToButtonCode['R'] =KEY_R;
  448. s_pVirtualKeyToButtonCode['S'] =KEY_S;
  449. s_pVirtualKeyToButtonCode['T'] =KEY_T;
  450. s_pVirtualKeyToButtonCode['U'] =KEY_U;
  451. s_pVirtualKeyToButtonCode['V'] =KEY_V;
  452. s_pVirtualKeyToButtonCode['W'] =KEY_W;
  453. s_pVirtualKeyToButtonCode['X'] =KEY_X;
  454. s_pVirtualKeyToButtonCode['Y'] =KEY_Y;
  455. s_pVirtualKeyToButtonCode['Z'] =KEY_Z;
  456. #if !defined( POSIX )
  457. s_pVirtualKeyToButtonCode[VK_NUMPAD0] =KEY_PAD_0;
  458. s_pVirtualKeyToButtonCode[VK_NUMPAD1] =KEY_PAD_1;
  459. s_pVirtualKeyToButtonCode[VK_NUMPAD2] =KEY_PAD_2;
  460. s_pVirtualKeyToButtonCode[VK_NUMPAD3] =KEY_PAD_3;
  461. s_pVirtualKeyToButtonCode[VK_NUMPAD4] =KEY_PAD_4;
  462. s_pVirtualKeyToButtonCode[VK_NUMPAD5] =KEY_PAD_5;
  463. s_pVirtualKeyToButtonCode[VK_NUMPAD6] =KEY_PAD_6;
  464. s_pVirtualKeyToButtonCode[VK_NUMPAD7] =KEY_PAD_7;
  465. s_pVirtualKeyToButtonCode[VK_NUMPAD8] =KEY_PAD_8;
  466. s_pVirtualKeyToButtonCode[VK_NUMPAD9] =KEY_PAD_9;
  467. s_pVirtualKeyToButtonCode[VK_DIVIDE] =KEY_PAD_DIVIDE;
  468. s_pVirtualKeyToButtonCode[VK_MULTIPLY] =KEY_PAD_MULTIPLY;
  469. s_pVirtualKeyToButtonCode[VK_SUBTRACT] =KEY_PAD_MINUS;
  470. s_pVirtualKeyToButtonCode[VK_ADD] =KEY_PAD_PLUS;
  471. s_pVirtualKeyToButtonCode[VK_RETURN] =KEY_PAD_ENTER;
  472. s_pVirtualKeyToButtonCode[VK_DECIMAL] =KEY_PAD_DECIMAL;
  473. #endif
  474. s_pVirtualKeyToButtonCode[0xdb] =KEY_LBRACKET;
  475. s_pVirtualKeyToButtonCode[0xdd] =KEY_RBRACKET;
  476. s_pVirtualKeyToButtonCode[0xba] =KEY_SEMICOLON;
  477. s_pVirtualKeyToButtonCode[0xde] =KEY_APOSTROPHE;
  478. s_pVirtualKeyToButtonCode[0xc0] =KEY_BACKQUOTE;
  479. s_pVirtualKeyToButtonCode[0xbc] =KEY_COMMA;
  480. s_pVirtualKeyToButtonCode[0xbe] =KEY_PERIOD;
  481. s_pVirtualKeyToButtonCode[0xbf] =KEY_SLASH;
  482. s_pVirtualKeyToButtonCode[0xdc] =KEY_BACKSLASH;
  483. s_pVirtualKeyToButtonCode[0xbd] =KEY_MINUS;
  484. s_pVirtualKeyToButtonCode[0xbb] =KEY_EQUAL;
  485. #if !defined( POSIX )
  486. s_pVirtualKeyToButtonCode[VK_RETURN] =KEY_ENTER;
  487. s_pVirtualKeyToButtonCode[VK_SPACE] =KEY_SPACE;
  488. s_pVirtualKeyToButtonCode[VK_BACK] =KEY_BACKSPACE;
  489. s_pVirtualKeyToButtonCode[VK_TAB] =KEY_TAB;
  490. s_pVirtualKeyToButtonCode[VK_CAPITAL] =KEY_CAPSLOCK;
  491. s_pVirtualKeyToButtonCode[VK_NUMLOCK] =KEY_NUMLOCK;
  492. s_pVirtualKeyToButtonCode[VK_ESCAPE] =KEY_ESCAPE;
  493. s_pVirtualKeyToButtonCode[VK_SCROLL] =KEY_SCROLLLOCK;
  494. s_pVirtualKeyToButtonCode[VK_INSERT] =KEY_INSERT;
  495. s_pVirtualKeyToButtonCode[VK_DELETE] =KEY_DELETE;
  496. s_pVirtualKeyToButtonCode[VK_HOME] =KEY_HOME;
  497. s_pVirtualKeyToButtonCode[VK_END] =KEY_END;
  498. s_pVirtualKeyToButtonCode[VK_PRIOR] =KEY_PAGEUP;
  499. s_pVirtualKeyToButtonCode[VK_NEXT] =KEY_PAGEDOWN;
  500. s_pVirtualKeyToButtonCode[VK_PAUSE] =KEY_BREAK;
  501. s_pVirtualKeyToButtonCode[VK_SHIFT] =KEY_RSHIFT;
  502. s_pVirtualKeyToButtonCode[VK_SHIFT] =KEY_LSHIFT; // SHIFT -> left SHIFT
  503. s_pVirtualKeyToButtonCode[VK_MENU] =KEY_RALT;
  504. s_pVirtualKeyToButtonCode[VK_MENU] =KEY_LALT; // ALT -> left ALT
  505. s_pVirtualKeyToButtonCode[VK_CONTROL] =KEY_RCONTROL;
  506. s_pVirtualKeyToButtonCode[VK_CONTROL] =KEY_LCONTROL; // CTRL -> left CTRL
  507. s_pVirtualKeyToButtonCode[VK_LWIN] =KEY_LWIN;
  508. s_pVirtualKeyToButtonCode[VK_RWIN] =KEY_RWIN;
  509. s_pVirtualKeyToButtonCode[VK_APPS] =KEY_APP;
  510. s_pVirtualKeyToButtonCode[VK_UP] =KEY_UP;
  511. s_pVirtualKeyToButtonCode[VK_LEFT] =KEY_LEFT;
  512. s_pVirtualKeyToButtonCode[VK_DOWN] =KEY_DOWN;
  513. s_pVirtualKeyToButtonCode[VK_RIGHT] =KEY_RIGHT;
  514. s_pVirtualKeyToButtonCode[VK_F1] =KEY_F1;
  515. s_pVirtualKeyToButtonCode[VK_F2] =KEY_F2;
  516. s_pVirtualKeyToButtonCode[VK_F3] =KEY_F3;
  517. s_pVirtualKeyToButtonCode[VK_F4] =KEY_F4;
  518. s_pVirtualKeyToButtonCode[VK_F5] =KEY_F5;
  519. s_pVirtualKeyToButtonCode[VK_F6] =KEY_F6;
  520. s_pVirtualKeyToButtonCode[VK_F7] =KEY_F7;
  521. s_pVirtualKeyToButtonCode[VK_F8] =KEY_F8;
  522. s_pVirtualKeyToButtonCode[VK_F9] =KEY_F9;
  523. s_pVirtualKeyToButtonCode[VK_F10] =KEY_F10;
  524. s_pVirtualKeyToButtonCode[VK_F11] =KEY_F11;
  525. s_pVirtualKeyToButtonCode[VK_F12] =KEY_F12;
  526. #endif
  527. // init the xkey translation table
  528. #if !defined( POSIX )
  529. s_pXKeyTrans[XK_NULL] = KEY_NONE;
  530. s_pXKeyTrans[XK_BUTTON_UP] = KEY_XBUTTON_UP;
  531. s_pXKeyTrans[XK_BUTTON_DOWN] = KEY_XBUTTON_DOWN;
  532. s_pXKeyTrans[XK_BUTTON_LEFT] = KEY_XBUTTON_LEFT;
  533. s_pXKeyTrans[XK_BUTTON_RIGHT] = KEY_XBUTTON_RIGHT;
  534. s_pXKeyTrans[XK_BUTTON_START] = KEY_XBUTTON_START;
  535. s_pXKeyTrans[XK_BUTTON_BACK] = KEY_XBUTTON_BACK;
  536. s_pXKeyTrans[XK_BUTTON_STICK1] = KEY_XBUTTON_STICK1;
  537. s_pXKeyTrans[XK_BUTTON_STICK2] = KEY_XBUTTON_STICK2;
  538. s_pXKeyTrans[XK_BUTTON_A] = KEY_XBUTTON_A;
  539. s_pXKeyTrans[XK_BUTTON_B] = KEY_XBUTTON_B;
  540. s_pXKeyTrans[XK_BUTTON_X] = KEY_XBUTTON_X;
  541. s_pXKeyTrans[XK_BUTTON_Y] = KEY_XBUTTON_Y;
  542. s_pXKeyTrans[XK_BUTTON_LEFT_SHOULDER] = KEY_XBUTTON_LEFT_SHOULDER;
  543. s_pXKeyTrans[XK_BUTTON_RIGHT_SHOULDER] = KEY_XBUTTON_RIGHT_SHOULDER;
  544. s_pXKeyTrans[XK_BUTTON_LTRIGGER] = KEY_XBUTTON_LTRIGGER;
  545. s_pXKeyTrans[XK_BUTTON_RTRIGGER] = KEY_XBUTTON_RTRIGGER;
  546. s_pXKeyTrans[XK_STICK1_UP] = KEY_XSTICK1_UP;
  547. s_pXKeyTrans[XK_STICK1_DOWN] = KEY_XSTICK1_DOWN;
  548. s_pXKeyTrans[XK_STICK1_LEFT] = KEY_XSTICK1_LEFT;
  549. s_pXKeyTrans[XK_STICK1_RIGHT] = KEY_XSTICK1_RIGHT;
  550. s_pXKeyTrans[XK_STICK2_UP] = KEY_XSTICK2_UP;
  551. s_pXKeyTrans[XK_STICK2_DOWN] = KEY_XSTICK2_DOWN;
  552. s_pXKeyTrans[XK_STICK2_LEFT] = KEY_XSTICK2_LEFT;
  553. s_pXKeyTrans[XK_STICK2_RIGHT] = KEY_XSTICK2_RIGHT;
  554. #endif // PLATFORM_POSIX
  555. // create reverse table engine to virtual
  556. for ( int i = 0; i < ARRAYSIZE( s_pVirtualKeyToButtonCode ); i++ )
  557. {
  558. s_pButtonCodeToVirtual[ s_pVirtualKeyToButtonCode[i] ] = i;
  559. }
  560. s_pButtonCodeToVirtual[0] = 0;
  561. s_pSKeytoButtonCode[SK_NULL] = KEY_NONE;
  562. s_pSKeytoButtonCode[SK_BUTTON_A] = STEAMCONTROLLER_A;
  563. s_pSKeytoButtonCode[SK_BUTTON_B] = STEAMCONTROLLER_B;
  564. s_pSKeytoButtonCode[SK_BUTTON_X] = STEAMCONTROLLER_X;
  565. s_pSKeytoButtonCode[SK_BUTTON_Y] = STEAMCONTROLLER_Y;
  566. s_pSKeytoButtonCode[SK_BUTTON_UP] = STEAMCONTROLLER_DPAD_UP;
  567. s_pSKeytoButtonCode[SK_BUTTON_RIGHT] = STEAMCONTROLLER_DPAD_RIGHT;
  568. s_pSKeytoButtonCode[SK_BUTTON_DOWN] = STEAMCONTROLLER_DPAD_DOWN;
  569. s_pSKeytoButtonCode[SK_BUTTON_LEFT] = STEAMCONTROLLER_DPAD_LEFT;
  570. s_pSKeytoButtonCode[SK_BUTTON_LEFT_BUMPER] = STEAMCONTROLLER_LEFT_BUMPER;
  571. s_pSKeytoButtonCode[SK_BUTTON_RIGHT_BUMPER] = STEAMCONTROLLER_RIGHT_BUMPER;
  572. s_pSKeytoButtonCode[SK_BUTTON_LEFT_TRIGGER] = STEAMCONTROLLER_LEFT_TRIGGER;
  573. s_pSKeytoButtonCode[SK_BUTTON_RIGHT_TRIGGER] = STEAMCONTROLLER_RIGHT_TRIGGER;
  574. s_pSKeytoButtonCode[SK_BUTTON_LEFT_GRIP] = STEAMCONTROLLER_LEFT_GRIP;
  575. s_pSKeytoButtonCode[SK_BUTTON_RIGHT_GRIP] = STEAMCONTROLLER_RIGHT_GRIP;
  576. s_pSKeytoButtonCode[SK_BUTTON_LPAD_TOUCH] = STEAMCONTROLLER_LEFT_PAD_FINGERDOWN;
  577. s_pSKeytoButtonCode[SK_BUTTON_RPAD_TOUCH] = STEAMCONTROLLER_RIGHT_PAD_FINGERDOWN;
  578. s_pSKeytoButtonCode[SK_BUTTON_LPAD_CLICK] = STEAMCONTROLLER_LEFT_PAD_CLICK;
  579. s_pSKeytoButtonCode[SK_BUTTON_RPAD_CLICK] = STEAMCONTROLLER_RIGHT_PAD_CLICK;
  580. s_pSKeytoButtonCode[SK_BUTTON_LPAD_UP] = STEAMCONTROLLER_LEFT_PAD_UP;
  581. s_pSKeytoButtonCode[SK_BUTTON_LPAD_RIGHT] = STEAMCONTROLLER_LEFT_PAD_RIGHT;
  582. s_pSKeytoButtonCode[SK_BUTTON_LPAD_DOWN] = STEAMCONTROLLER_LEFT_PAD_DOWN;
  583. s_pSKeytoButtonCode[SK_BUTTON_LPAD_LEFT] = STEAMCONTROLLER_LEFT_PAD_LEFT;
  584. s_pSKeytoButtonCode[SK_BUTTON_RPAD_UP] = STEAMCONTROLLER_RIGHT_PAD_UP;
  585. s_pSKeytoButtonCode[SK_BUTTON_RPAD_RIGHT] = STEAMCONTROLLER_RIGHT_PAD_RIGHT;
  586. s_pSKeytoButtonCode[SK_BUTTON_RPAD_DOWN] = STEAMCONTROLLER_RIGHT_PAD_DOWN;
  587. s_pSKeytoButtonCode[SK_BUTTON_RPAD_LEFT] = STEAMCONTROLLER_RIGHT_PAD_LEFT;
  588. s_pSKeytoButtonCode[SK_BUTTON_SELECT] = STEAMCONTROLLER_SELECT;
  589. s_pSKeytoButtonCode[SK_BUTTON_START] = STEAMCONTROLLER_START;
  590. s_pSKeytoButtonCode[SK_BUTTON_STEAM] = STEAMCONTROLLER_STEAM;
  591. s_pSKeytoButtonCode[SK_BUTTON_INACTIVE_START] = STEAMCONTROLLER_INACTIVE_START;
  592. // These are fake ("virtual") steam controller buttons that don't physically exist, but we can manufacture to make internal routing
  593. // to old school UI (which is expecting button code rather than actions) without clashing with other butt
  594. s_pSKeytoButtonCode[SK_VBUTTON_F1] = STEAMCONTROLLER_F1;
  595. s_pSKeytoButtonCode[SK_VBUTTON_F2] = STEAMCONTROLLER_F2;
  596. s_pSKeytoButtonCode[SK_VBUTTON_F3] = STEAMCONTROLLER_F3;
  597. s_pSKeytoButtonCode[SK_VBUTTON_F4] = STEAMCONTROLLER_F4;
  598. s_pSKeytoButtonCode[SK_VBUTTON_F5] = STEAMCONTROLLER_F5;
  599. s_pSKeytoButtonCode[SK_VBUTTON_F6] = STEAMCONTROLLER_F6;
  600. s_pSKeytoButtonCode[SK_VBUTTON_F7] = STEAMCONTROLLER_F7;
  601. s_pSKeytoButtonCode[SK_VBUTTON_F8] = STEAMCONTROLLER_F8;
  602. s_pSKeytoButtonCode[SK_VBUTTON_F9] = STEAMCONTROLLER_F9;
  603. s_pSKeytoButtonCode[SK_VBUTTON_F10] = STEAMCONTROLLER_F10;
  604. s_pSKeytoButtonCode[SK_VBUTTON_F11] = STEAMCONTROLLER_F11;
  605. s_pSKeytoButtonCode[SK_VBUTTON_F12] = STEAMCONTROLLER_F12;
  606. }
  607. ButtonCode_t ButtonCode_VirtualKeyToButtonCode( int keyCode )
  608. {
  609. if ( keyCode < 0 || keyCode >= sizeof( s_pVirtualKeyToButtonCode ) / sizeof( s_pVirtualKeyToButtonCode[0] ) )
  610. {
  611. Assert( false );
  612. return KEY_NONE;
  613. }
  614. return s_pVirtualKeyToButtonCode[keyCode];
  615. }
  616. int ButtonCode_ButtonCodeToVirtualKey( ButtonCode_t code )
  617. {
  618. return s_pButtonCodeToVirtual[code];
  619. }
  620. ButtonCode_t ButtonCode_XKeyToButtonCode( int nPort, int keyCode )
  621. {
  622. #if !defined( POSIX )
  623. if ( keyCode < 0 || keyCode >= sizeof( s_pXKeyTrans ) / sizeof( s_pXKeyTrans[0] ) )
  624. {
  625. Assert( false );
  626. return KEY_NONE;
  627. }
  628. ButtonCode_t code = s_pXKeyTrans[keyCode];
  629. if ( IsJoystickButtonCode( code ) )
  630. {
  631. int nOffset = code - JOYSTICK_FIRST_BUTTON;
  632. return JOYSTICK_BUTTON( nPort, nOffset );
  633. }
  634. if ( IsJoystickPOVCode( code ) )
  635. {
  636. int nOffset = code - JOYSTICK_FIRST_POV_BUTTON;
  637. return JOYSTICK_POV_BUTTON( nPort, nOffset );
  638. }
  639. if ( IsJoystickAxisCode( code ) )
  640. {
  641. int nOffset = code - JOYSTICK_FIRST_AXIS_BUTTON;
  642. return JOYSTICK_AXIS_BUTTON( nPort, nOffset );
  643. }
  644. return code;
  645. #else // POSIX
  646. return KEY_NONE;
  647. #endif // POSIX
  648. }
  649. // Convert back + forth between ButtonCode/AnalogCode + strings
  650. const char *ButtonCode_ButtonCodeToString( ButtonCode_t code, bool bXController )
  651. {
  652. #if !defined ( _X360 )
  653. if ( bXController && code >= JOYSTICK_FIRST_BUTTON && code <= JOYSTICK_LAST_AXIS_BUTTON )
  654. return s_pXControllerButtonCodeNames[ code - JOYSTICK_FIRST_BUTTON ];
  655. #endif
  656. return s_pButtonCodeName[ code ];
  657. }
  658. const char *AnalogCode_AnalogCodeToString( AnalogCode_t code )
  659. {
  660. return s_pAnalogCodeName[ code ];
  661. }
  662. ButtonCode_t ButtonCode_StringToButtonCode( const char *pString, bool bXController )
  663. {
  664. if ( !pString || !pString[0] )
  665. return BUTTON_CODE_INVALID;
  666. // Backward compat for screwed up previous joystick button names
  667. if ( !Q_strnicmp( pString, "aux", 3 ) )
  668. {
  669. int nIndex = atoi( &pString[3] );
  670. if ( nIndex < 29 )
  671. return JOYSTICK_BUTTON( 0, nIndex );
  672. if ( ( nIndex >= 29 ) && ( nIndex <= 32 ) )
  673. return JOYSTICK_POV_BUTTON( 0, nIndex - 29 );
  674. return BUTTON_CODE_INVALID;
  675. }
  676. for ( int i = 0; i < BUTTON_CODE_LAST; ++i )
  677. {
  678. if ( !Q_stricmp( s_pButtonCodeName[i], pString ) )
  679. return (ButtonCode_t)i;
  680. }
  681. #if !defined ( _X360 )
  682. if ( bXController )
  683. {
  684. for ( int i = 0; i < ARRAYSIZE(s_pXControllerButtonCodeNames); ++i )
  685. {
  686. if ( !Q_stricmp( s_pXControllerButtonCodeNames[i], pString ) )
  687. return (ButtonCode_t)(JOYSTICK_FIRST_BUTTON + i);
  688. }
  689. }
  690. #endif
  691. return BUTTON_CODE_INVALID;
  692. }
  693. ButtonCode_t ButtonCode_SKeyToButtonCode( int nPort, int keyCode )
  694. {
  695. #if !defined( _GAMECONSOLE )
  696. if ( keyCode < 0 || keyCode >= sizeof( s_pSKeytoButtonCode ) / sizeof( s_pSKeytoButtonCode[0] ) )
  697. {
  698. Assert( false );
  699. return KEY_NONE;
  700. }
  701. ButtonCode_t code = s_pSKeytoButtonCode[keyCode];
  702. // if ( IsSteamControllerCode( code ) )
  703. // {
  704. // // Need Per Controller Offset here.
  705. // return code;
  706. // }
  707. if ( IsSteamControllerButtonCode( code ) )
  708. {
  709. int nOffset = code - STEAMCONTROLLER_FIRST_BUTTON;
  710. return STEAMCONTROLLER_BUTTON( nPort, nOffset );
  711. }
  712. if ( IsSteamControllerAxisCode( code ) )
  713. {
  714. int nOffset = code - STEAMCONTROLLER_FIRST_AXIS_BUTTON;
  715. return STEAMCONTROLLER_AXIS_BUTTON( nPort, nOffset );
  716. }
  717. return code;
  718. #else // _GAMECONSOLE
  719. return KEY_NONE;
  720. #endif // _GAMECONSOLE
  721. }
  722. AnalogCode_t AnalogCode_StringToAnalogCode( const char *pString )
  723. {
  724. if ( !pString || !pString[0] )
  725. return ANALOG_CODE_INVALID;
  726. for ( int i = 0; i < ANALOG_CODE_LAST; ++i )
  727. {
  728. if ( !Q_stricmp( s_pAnalogCodeName[i], pString ) )
  729. return (AnalogCode_t)i;
  730. }
  731. return ANALOG_CODE_INVALID;
  732. }
  733. ButtonCode_t ButtonCode_ScanCodeToButtonCode( int lParam )
  734. {
  735. int nScanCode = ( lParam >> 16 ) & 0xFF;
  736. if ( nScanCode > 127 )
  737. return KEY_NONE;
  738. ButtonCode_t result = s_pScanToButtonCode[nScanCode];
  739. bool bIsExtended = ( lParam & ( 1 << 24 ) ) != 0;
  740. if ( !bIsExtended )
  741. {
  742. switch ( result )
  743. {
  744. case KEY_HOME:
  745. return KEY_PAD_7;
  746. case KEY_UP:
  747. return KEY_PAD_8;
  748. case KEY_PAGEUP:
  749. return KEY_PAD_9;
  750. case KEY_LEFT:
  751. return KEY_PAD_4;
  752. case KEY_RIGHT:
  753. return KEY_PAD_6;
  754. case KEY_END:
  755. return KEY_PAD_1;
  756. case KEY_DOWN:
  757. return KEY_PAD_2;
  758. case KEY_PAGEDOWN:
  759. return KEY_PAD_3;
  760. case KEY_INSERT:
  761. return KEY_PAD_0;
  762. case KEY_DELETE:
  763. return KEY_PAD_DECIMAL;
  764. default:
  765. break;
  766. }
  767. }
  768. else
  769. {
  770. switch ( result )
  771. {
  772. case KEY_ENTER:
  773. return KEY_PAD_ENTER;
  774. case KEY_LALT:
  775. return KEY_RALT;
  776. case KEY_LCONTROL:
  777. return KEY_RCONTROL;
  778. case KEY_SLASH:
  779. return KEY_PAD_DIVIDE;
  780. case KEY_CAPSLOCK:
  781. return KEY_PAD_PLUS;
  782. }
  783. }
  784. return result;
  785. }
  786. //-----------------------------------------------------------------------------
  787. // Update scan codes for foreign keyboards
  788. //-----------------------------------------------------------------------------
  789. void ButtonCode_UpdateScanCodeLayout( )
  790. {
  791. // reset the keyboard
  792. memcpy( s_pScanToButtonCode, s_pScanToButtonCode_QWERTY, sizeof(s_pScanToButtonCode) );
  793. #if !defined( _X360 ) && !defined( POSIX )
  794. // fix up keyboard layout for other languages
  795. HKL currentKb = ::GetKeyboardLayout( 0 );
  796. HKL englishKb = ::LoadKeyboardLayout("00000409", 0);
  797. if (englishKb && englishKb != currentKb)
  798. {
  799. for ( int i = 0; i < ARRAYSIZE(s_pScanToButtonCode); i++ )
  800. {
  801. // take the english/QWERTY
  802. ButtonCode_t code = s_pScanToButtonCode_QWERTY[ i ];
  803. // only remap printable keys
  804. if ( code != KEY_NONE && code != KEY_BACKQUOTE && ( IsAlphaNumeric( code ) || IsPunctuation( code ) ) )
  805. {
  806. // get it's virtual key based on the old layout
  807. int vk = ::MapVirtualKeyEx( i, 1, englishKb );
  808. // turn in into a scancode on the new layout
  809. int newScanCode = ::MapVirtualKeyEx( vk, 0, currentKb );
  810. // strip off any high bits
  811. newScanCode &= 0x0000007F;
  812. // set in the new layout
  813. s_pScanToButtonCode[newScanCode] = code;
  814. }
  815. }
  816. }
  817. s_pScanToButtonCode[0] = KEY_NONE;
  818. #endif
  819. }
  820. //-----------------------------------------------------------------------------
  821. // Purpose: Updates the current keyboard layout
  822. //-----------------------------------------------------------------------------
  823. CON_COMMAND( key_updatelayout, "Updates game keyboard layout to current windows keyboard setting." )
  824. {
  825. ButtonCode_UpdateScanCodeLayout();
  826. }