winInput.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformWin32.h"
  23. #include "platform/platformInput.h"
  24. #include "platformWin32/winDirectInput.h"
  25. #include "console/console.h"
  26. #include "console/engineAPI.h"
  27. #include "core/util/journal/process.h"
  28. #include "windowManager/platformWindowMgr.h"
  29. #ifdef LOG_INPUT
  30. #include <time.h>
  31. #include <stdarg.h>
  32. #endif
  33. // Static class variables:
  34. InputManager* Input::smManager;
  35. bool Input::smActive;
  36. U8 Input::smModifierKeys;
  37. bool Input::smLastKeyboardActivated;
  38. bool Input::smLastMouseActivated;
  39. bool Input::smLastJoystickActivated;
  40. InputEvent Input::smInputEvent;
  41. #ifdef LOG_INPUT
  42. static HANDLE gInputLog;
  43. #endif
  44. static void fillAsciiTable();
  45. //------------------------------------------------------------------------------
  46. //
  47. // This function gets the standard ASCII code corresponding to our key code
  48. // and the existing modifier key state.
  49. //
  50. //------------------------------------------------------------------------------
  51. struct AsciiData
  52. {
  53. struct KeyData
  54. {
  55. U16 ascii;
  56. bool isDeadChar;
  57. };
  58. KeyData upper;
  59. KeyData lower;
  60. KeyData goofy;
  61. };
  62. #define NUM_KEYS ( KEY_OEM_102 + 1 )
  63. #define KEY_FIRST KEY_ESCAPE
  64. static AsciiData AsciiTable[NUM_KEYS];
  65. //------------------------------------------------------------------------------
  66. void Input::init()
  67. {
  68. Con::printf( "Input Init:" );
  69. destroy();
  70. #ifdef LOG_INPUT
  71. struct tm* newTime;
  72. time_t aclock;
  73. time( &aclock );
  74. newTime = localtime( &aclock );
  75. asctime( newTime );
  76. gInputLog = CreateFile( L"input.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  77. log( "Input log opened at %s\n", asctime( newTime ) );
  78. #endif
  79. smActive = false;
  80. smLastKeyboardActivated = true;
  81. smLastMouseActivated = true;
  82. smLastJoystickActivated = true;
  83. OSVERSIONINFO OSVersionInfo;
  84. dMemset( &OSVersionInfo, 0, sizeof( OSVERSIONINFO ) );
  85. OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  86. if ( GetVersionEx( &OSVersionInfo ) )
  87. {
  88. #ifdef LOG_INPUT
  89. log( "Operating System:\n" );
  90. switch ( OSVersionInfo.dwPlatformId )
  91. {
  92. case VER_PLATFORM_WIN32s:
  93. log( " Win32s on Windows 3.1 version %d.%d\n", OSVersionInfo.dwMajorVersion, OSVersionInfo.dwMinorVersion );
  94. break;
  95. case VER_PLATFORM_WIN32_WINDOWS:
  96. log( " Windows 95 version %d.%d\n", OSVersionInfo.dwMajorVersion, OSVersionInfo.dwMinorVersion );
  97. log( " Build number %d\n", LOWORD( OSVersionInfo.dwBuildNumber ) );
  98. break;
  99. case VER_PLATFORM_WIN32_NT:
  100. log( " WinNT version %d.%d\n", OSVersionInfo.dwMajorVersion, OSVersionInfo.dwMinorVersion );
  101. log( " Build number %d\n", OSVersionInfo.dwBuildNumber );
  102. break;
  103. }
  104. if ( OSVersionInfo.szCSDVersion != NULL )
  105. log( " %s\n", OSVersionInfo.szCSDVersion );
  106. log( "\n" );
  107. #endif
  108. if ( !( OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT && OSVersionInfo.dwMajorVersion < 5 ) )
  109. {
  110. smManager = new DInputManager;
  111. if ( !smManager->enable() )
  112. {
  113. Con::printf( " DirectInput not enabled." );
  114. delete smManager;
  115. smManager = NULL;
  116. }
  117. else
  118. {
  119. DInputManager::init();
  120. Con::printf( " DirectInput enabled." );
  121. }
  122. }
  123. else
  124. Con::printf( " WinNT detected -- DirectInput not enabled." );
  125. }
  126. // Init the current modifier keys
  127. setModifierKeys(0);
  128. fillAsciiTable();
  129. Con::printf( "" );
  130. // Set ourselves to participate in per-frame processing.
  131. Process::notify(Input::process, PROCESS_INPUT_ORDER);
  132. }
  133. //------------------------------------------------------------------------------
  134. DefineConsoleFunction( isJoystickDetected, bool, (), , "isJoystickDetected()")
  135. {
  136. return( DInputDevice::joystickDetected() );
  137. }
  138. //------------------------------------------------------------------------------
  139. DefineConsoleFunction( getJoystickAxes, const char*, (U32 deviceID), , "getJoystickAxes( instance )")
  140. {
  141. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  142. if ( mgr )
  143. return( mgr->getJoystickAxesString( deviceID ) );
  144. return( "" );
  145. }
  146. //------------------------------------------------------------------------------
  147. static void fillAsciiTable()
  148. {
  149. #ifdef LOG_INPUT
  150. char buf[256];
  151. Input::log( "--- Filling the ASCII table! ---\n" );
  152. #endif
  153. //HKL layout = GetKeyboardLayout( 0 );
  154. U8 state[256];
  155. U16 ascii[2];
  156. U32 dikCode, vKeyCode, keyCode;
  157. S32 result;
  158. dMemset( &AsciiTable, 0, sizeof( AsciiTable ) );
  159. dMemset( &state, 0, sizeof( state ) );
  160. for ( keyCode = KEY_FIRST; keyCode < NUM_KEYS; keyCode++ )
  161. {
  162. ascii[0] = ascii[1] = 0;
  163. dikCode = Key_to_DIK( keyCode );
  164. // This is a special case for numpad keys.
  165. //
  166. // The KEY_NUMPAD# torque types represent the event generated when a
  167. // numpad key is pressed WITH NUMLOCK ON. Therefore it does have an ascii
  168. // value, but to get it from windows we must specify in the keyboard state
  169. // that numlock is on.
  170. if ( KEY_NUMPAD0 <= keyCode && keyCode <= KEY_NUMPAD9 )
  171. {
  172. state[VK_NUMLOCK] = 0x80;
  173. // Also, numpad keys return completely different keycodes when
  174. // numlock is not pressed (home,insert,etc) and MapVirtualKey
  175. // appears to always return those values.
  176. //
  177. // So I'm using TranslateKeyCodeToOS instead. Really it looks
  178. // like we could be using this method for all of them and
  179. // cut out the Key_to_DIK middleman.
  180. //
  181. vKeyCode = TranslateKeyCodeToOS( keyCode );
  182. result = ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  183. AsciiTable[keyCode].lower.ascii = ascii[0];
  184. AsciiTable[keyCode].upper.ascii = 0;
  185. AsciiTable[keyCode].goofy.ascii = 0;
  186. state[VK_NUMLOCK] = 0;
  187. continue;
  188. }
  189. if ( dikCode )
  190. {
  191. //vKeyCode = MapVirtualKeyEx( dikCode, 1, layout );
  192. vKeyCode = MapVirtualKey( dikCode, 1 );
  193. #ifdef LOG_INPUT
  194. dSprintf( buf, sizeof( buf ), "KC: %#04X DK: %#04X VK: %#04X\n",
  195. keyCode, dikCode, vKeyCode );
  196. Input::log( buf );
  197. #endif
  198. // Lower case:
  199. ascii[0] = ascii[1] = 0;
  200. //result = ToAsciiEx( vKeyCode, dikCode, state, ascii, 0, layout );
  201. result = ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  202. #ifdef LOG_INPUT
  203. dSprintf( buf, sizeof( buf ), " LOWER- R: %d A[0]: %#06X A[1]: %#06X\n",
  204. result, ascii[0], ascii[1] );
  205. Input::log( buf );
  206. #endif
  207. if ( result == 2 )
  208. AsciiTable[keyCode].lower.ascii = ascii[1] ? ascii[1] : ( ascii[0] >> 8 );
  209. else if ( result == 1 )
  210. AsciiTable[keyCode].lower.ascii = ascii[0];
  211. else if ( result < 0 )
  212. {
  213. AsciiTable[keyCode].lower.ascii = ascii[0];
  214. AsciiTable[keyCode].lower.isDeadChar = true;
  215. // Need to clear the dead character from the keyboard layout:
  216. //ToAsciiEx( vKeyCode, dikCode, state, ascii, 0, layout );
  217. ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  218. }
  219. // Upper case:
  220. ascii[0] = ascii[1] = 0;
  221. state[VK_SHIFT] = 0x80;
  222. //result = ToAsciiEx( vKeyCode, dikCode, state, ascii, 0, layout );
  223. result = ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  224. #ifdef LOG_INPUT
  225. dSprintf( buf, sizeof( buf ), " UPPER- R: %d A[0]: %#06X A[1]: %#06X\n",
  226. result, ascii[0], ascii[1] );
  227. Input::log( buf );
  228. #endif
  229. if ( result == 2 )
  230. AsciiTable[keyCode].upper.ascii = ascii[1] ? ascii[1] : ( ascii[0] >> 8 );
  231. else if ( result == 1 )
  232. AsciiTable[keyCode].upper.ascii = ascii[0];
  233. else if ( result < 0 )
  234. {
  235. AsciiTable[keyCode].upper.ascii = ascii[0];
  236. AsciiTable[keyCode].upper.isDeadChar = true;
  237. // Need to clear the dead character from the keyboard layout:
  238. //ToAsciiEx( vKeyCode, dikCode, state, ascii, 0, layout );
  239. ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  240. }
  241. state[VK_SHIFT] = 0;
  242. // Foreign mod case:
  243. ascii[0] = ascii[1] = 0;
  244. state[VK_CONTROL] = 0x80;
  245. state[VK_MENU] = 0x80;
  246. //result = ToAsciiEx( vKeyCode, dikCode, state, ascii, 0, layout );
  247. result = ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  248. #ifdef LOG_INPUT
  249. dSprintf( buf, sizeof( buf ), " GOOFY- R: %d A[0]: %#06X A[1]: %#06X\n",
  250. result, ascii[0], ascii[1] );
  251. Input::log( buf );
  252. #endif
  253. if ( result == 2 )
  254. AsciiTable[keyCode].goofy.ascii = ascii[1] ? ascii[1] : ( ascii[0] >> 8 );
  255. else if ( result == 1 )
  256. AsciiTable[keyCode].goofy.ascii = ascii[0];
  257. else if ( result < 0 )
  258. {
  259. AsciiTable[keyCode].goofy.ascii = ascii[0];
  260. AsciiTable[keyCode].goofy.isDeadChar = true;
  261. // Need to clear the dead character from the keyboard layout:
  262. //ToAsciiEx( vKeyCode, dikCode, state, ascii, 0, layout );
  263. ToAscii( vKeyCode, dikCode, state, ascii, 0 );
  264. }
  265. state[VK_CONTROL] = 0;
  266. state[VK_MENU] = 0;
  267. }
  268. }
  269. #ifdef LOG_INPUT
  270. Input::log( "--- Finished filling the ASCII table! ---\n\n" );
  271. #endif
  272. }
  273. //------------------------------------------------------------------------------
  274. U16 Input::getKeyCode( U16 asciiCode )
  275. {
  276. U16 keyCode = 0;
  277. U16 i;
  278. // This is done three times so the lowerkey will always
  279. // be found first. Some foreign keyboards have duplicate
  280. // chars on some keys.
  281. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  282. {
  283. if ( AsciiTable[i].lower.ascii == asciiCode )
  284. {
  285. keyCode = i;
  286. break;
  287. };
  288. }
  289. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  290. {
  291. if ( AsciiTable[i].upper.ascii == asciiCode )
  292. {
  293. keyCode = i;
  294. break;
  295. };
  296. }
  297. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  298. {
  299. if ( AsciiTable[i].goofy.ascii == asciiCode )
  300. {
  301. keyCode = i;
  302. break;
  303. };
  304. }
  305. return( keyCode );
  306. }
  307. //------------------------------------------------------------------------------
  308. U16 Input::getAscii( U16 keyCode, KEY_STATE keyState )
  309. {
  310. if ( keyCode >= NUM_KEYS )
  311. return 0;
  312. switch ( keyState )
  313. {
  314. case STATE_LOWER:
  315. return AsciiTable[keyCode].lower.ascii;
  316. case STATE_UPPER:
  317. return AsciiTable[keyCode].upper.ascii;
  318. case STATE_GOOFY:
  319. return AsciiTable[keyCode].goofy.ascii;
  320. default:
  321. return(0);
  322. }
  323. }
  324. //------------------------------------------------------------------------------
  325. void Input::destroy()
  326. {
  327. Process::remove(Input::process);
  328. #ifdef LOG_INPUT
  329. if ( gInputLog )
  330. {
  331. log( "*** CLOSING LOG ***\n" );
  332. CloseHandle( gInputLog );
  333. gInputLog = NULL;
  334. }
  335. #endif
  336. if ( smManager && smManager->isEnabled() )
  337. {
  338. smManager->disable();
  339. delete smManager;
  340. smManager = NULL;
  341. }
  342. }
  343. //------------------------------------------------------------------------------
  344. bool Input::enable()
  345. {
  346. if ( smManager && !smManager->isEnabled() )
  347. return( smManager->enable() );
  348. return( false );
  349. }
  350. //------------------------------------------------------------------------------
  351. void Input::disable()
  352. {
  353. if ( smManager && smManager->isEnabled() )
  354. smManager->disable();
  355. }
  356. //------------------------------------------------------------------------------
  357. void Input::activate()
  358. {
  359. #ifdef UNICODE
  360. //winState.imeHandle = ImmGetContext( getWin32WindowHandle() );
  361. //ImmReleaseContext( getWin32WindowHandle(), winState.imeHandle );
  362. #endif
  363. if ( !Con::getBoolVariable( "$enableDirectInput" ) )
  364. return;
  365. if ( smManager && smManager->isEnabled() && !smActive )
  366. {
  367. Con::printf( "Activating DirectInput..." );
  368. #ifdef LOG_INPUT
  369. Input::log( "Activating DirectInput...\n" );
  370. #endif
  371. smActive = true;
  372. DInputManager* dInputManager = dynamic_cast<DInputManager*>( smManager );
  373. if ( dInputManager )
  374. {
  375. if ( dInputManager->isJoystickEnabled() && smLastJoystickActivated )
  376. dInputManager->activateJoystick();
  377. }
  378. }
  379. }
  380. //------------------------------------------------------------------------------
  381. void Input::deactivate()
  382. {
  383. if ( smManager && smManager->isEnabled() && smActive )
  384. {
  385. #ifdef LOG_INPUT
  386. Input::log( "Deactivating DirectInput...\n" );
  387. #endif
  388. DInputManager* dInputManager = dynamic_cast<DInputManager*>( smManager );
  389. if ( dInputManager )
  390. {
  391. smLastJoystickActivated = dInputManager->isJoystickActive();
  392. dInputManager->deactivateJoystick();
  393. }
  394. smActive = false;
  395. Con::printf( "DirectInput deactivated." );
  396. }
  397. }
  398. //------------------------------------------------------------------------------
  399. bool Input::isEnabled()
  400. {
  401. if ( smManager )
  402. return smManager->isEnabled();
  403. return false;
  404. }
  405. //------------------------------------------------------------------------------
  406. bool Input::isActive()
  407. {
  408. return smActive;
  409. }
  410. //------------------------------------------------------------------------------
  411. void Input::process()
  412. {
  413. if ( smManager && smManager->isEnabled() && smActive )
  414. smManager->process();
  415. }
  416. //------------------------------------------------------------------------------
  417. InputManager* Input::getManager()
  418. {
  419. return( smManager );
  420. }
  421. #ifdef LOG_INPUT
  422. //------------------------------------------------------------------------------
  423. void Input::log( const char* format, ... )
  424. {
  425. if ( !gInputLog )
  426. return;
  427. va_list argptr;
  428. va_start( argptr, format );
  429. char buffer[512];
  430. dVsprintf( buffer, 511, format, argptr );
  431. DWORD bytes;
  432. WriteFile( gInputLog, buffer, dStrlen( buffer ), &bytes, NULL );
  433. va_end( argptr );
  434. }
  435. DefineConsoleFunction( inputLog, void, (const char * log), , "inputLog( string )")
  436. {
  437. Input::log( "%s\n", log );
  438. }
  439. #endif // LOG_INPUT
  440. //------------------------------------------------------------------------------
  441. //------------------------------------------------------------------------------
  442. static U8 VcodeRemap[256] =
  443. {
  444. 0, // 0x00
  445. 0, // 0x01 VK_LBUTTON
  446. 0, // 0x02 VK_RBUTTON
  447. 0, // 0x03 VK_CANCEL
  448. 0, // 0x04 VK_MBUTTON
  449. 0, // 0x05
  450. 0, // 0x06
  451. 0, // 0x07
  452. KEY_BACKSPACE, // 0x08 VK_BACK
  453. KEY_TAB, // 0x09 VK_TAB
  454. 0, // 0x0A
  455. 0, // 0x0B
  456. 0, // 0x0C VK_CLEAR
  457. KEY_RETURN, // 0x0D VK_RETURN
  458. 0, // 0x0E
  459. 0, // 0x0F
  460. KEY_SHIFT, // 0x10 VK_SHIFT
  461. KEY_CONTROL, // 0x11 VK_CONTROL
  462. KEY_ALT, // 0x12 VK_MENU
  463. KEY_PAUSE, // 0x13 VK_PAUSE
  464. KEY_CAPSLOCK, // 0x14 VK_CAPITAL
  465. 0, // 0x15 VK_KANA, VK_HANGEUL, VK_HANGUL
  466. 0, // 0x16
  467. 0, // 0x17 VK_JUNJA
  468. 0, // 0x18 VK_FINAL
  469. 0, // 0x19 VK_HANJA, VK_KANJI
  470. 0, // 0x1A
  471. KEY_ESCAPE, // 0x1B VK_ESCAPE
  472. 0, // 0x1C VK_CONVERT
  473. 0, // 0x1D VK_NONCONVERT
  474. 0, // 0x1E VK_ACCEPT
  475. 0, // 0x1F VK_MODECHANGE
  476. KEY_SPACE, // 0x20 VK_SPACE
  477. KEY_PAGE_UP, // 0x21 VK_PRIOR
  478. KEY_PAGE_DOWN, // 0x22 VK_NEXT
  479. KEY_END, // 0x23 VK_END
  480. KEY_HOME, // 0x24 VK_HOME
  481. KEY_LEFT, // 0x25 VK_LEFT
  482. KEY_UP, // 0x26 VK_UP
  483. KEY_RIGHT, // 0x27 VK_RIGHT
  484. KEY_DOWN, // 0x28 VK_DOWN
  485. 0, // 0x29 VK_SELECT
  486. KEY_PRINT, // 0x2A VK_PRINT
  487. 0, // 0x2B VK_EXECUTE
  488. 0, // 0x2C VK_SNAPSHOT
  489. KEY_INSERT, // 0x2D VK_INSERT
  490. KEY_DELETE, // 0x2E VK_DELETE
  491. KEY_HELP, // 0x2F VK_HELP
  492. KEY_0, // 0x30 VK_0 VK_0 thru VK_9 are the same as ASCII '0' thru '9' (// 0x30 - // 0x39)
  493. KEY_1, // 0x31 VK_1
  494. KEY_2, // 0x32 VK_2
  495. KEY_3, // 0x33 VK_3
  496. KEY_4, // 0x34 VK_4
  497. KEY_5, // 0x35 VK_5
  498. KEY_6, // 0x36 VK_6
  499. KEY_7, // 0x37 VK_7
  500. KEY_8, // 0x38 VK_8
  501. KEY_9, // 0x39 VK_9
  502. 0, // 0x3A
  503. 0, // 0x3B
  504. 0, // 0x3C
  505. 0, // 0x3D
  506. 0, // 0x3E
  507. 0, // 0x3F
  508. 0, // 0x40
  509. KEY_A, // 0x41 VK_A VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (// 0x41 - // 0x5A)
  510. KEY_B, // 0x42 VK_B
  511. KEY_C, // 0x43 VK_C
  512. KEY_D, // 0x44 VK_D
  513. KEY_E, // 0x45 VK_E
  514. KEY_F, // 0x46 VK_F
  515. KEY_G, // 0x47 VK_G
  516. KEY_H, // 0x48 VK_H
  517. KEY_I, // 0x49 VK_I
  518. KEY_J, // 0x4A VK_J
  519. KEY_K, // 0x4B VK_K
  520. KEY_L, // 0x4C VK_L
  521. KEY_M, // 0x4D VK_M
  522. KEY_N, // 0x4E VK_N
  523. KEY_O, // 0x4F VK_O
  524. KEY_P, // 0x50 VK_P
  525. KEY_Q, // 0x51 VK_Q
  526. KEY_R, // 0x52 VK_R
  527. KEY_S, // 0x53 VK_S
  528. KEY_T, // 0x54 VK_T
  529. KEY_U, // 0x55 VK_U
  530. KEY_V, // 0x56 VK_V
  531. KEY_W, // 0x57 VK_W
  532. KEY_X, // 0x58 VK_X
  533. KEY_Y, // 0x59 VK_Y
  534. KEY_Z, // 0x5A VK_Z
  535. KEY_WIN_LWINDOW, // 0x5B VK_LWIN
  536. KEY_WIN_RWINDOW, // 0x5C VK_RWIN
  537. KEY_WIN_APPS, // 0x5D VK_APPS
  538. 0, // 0x5E
  539. 0, // 0x5F
  540. KEY_NUMPAD0, // 0x60 VK_NUMPAD0
  541. KEY_NUMPAD1, // 0x61 VK_NUMPAD1
  542. KEY_NUMPAD2, // 0x62 VK_NUMPAD2
  543. KEY_NUMPAD3, // 0x63 VK_NUMPAD3
  544. KEY_NUMPAD4, // 0x64 VK_NUMPAD4
  545. KEY_NUMPAD5, // 0x65 VK_NUMPAD5
  546. KEY_NUMPAD6, // 0x66 VK_NUMPAD6
  547. KEY_NUMPAD7, // 0x67 VK_NUMPAD7
  548. KEY_NUMPAD8, // 0x68 VK_NUMPAD8
  549. KEY_NUMPAD9, // 0x69 VK_NUMPAD9
  550. KEY_MULTIPLY, // 0x6A VK_MULTIPLY
  551. KEY_ADD, // 0x6B VK_ADD
  552. KEY_SEPARATOR, // 0x6C VK_SEPARATOR
  553. KEY_SUBTRACT, // 0x6D VK_SUBTRACT
  554. KEY_DECIMAL, // 0x6E VK_DECIMAL
  555. KEY_DIVIDE, // 0x6F VK_DIVIDE
  556. KEY_F1, // 0x70 VK_F1
  557. KEY_F2, // 0x71 VK_F2
  558. KEY_F3, // 0x72 VK_F3
  559. KEY_F4, // 0x73 VK_F4
  560. KEY_F5, // 0x74 VK_F5
  561. KEY_F6, // 0x75 VK_F6
  562. KEY_F7, // 0x76 VK_F7
  563. KEY_F8, // 0x77 VK_F8
  564. KEY_F9, // 0x78 VK_F9
  565. KEY_F10, // 0x79 VK_F10
  566. KEY_F11, // 0x7A VK_F11
  567. KEY_F12, // 0x7B VK_F12
  568. KEY_F13, // 0x7C VK_F13
  569. KEY_F14, // 0x7D VK_F14
  570. KEY_F15, // 0x7E VK_F15
  571. KEY_F16, // 0x7F VK_F16
  572. KEY_F17, // 0x80 VK_F17
  573. KEY_F18, // 0x81 VK_F18
  574. KEY_F19, // 0x82 VK_F19
  575. KEY_F20, // 0x83 VK_F20
  576. KEY_F21, // 0x84 VK_F21
  577. KEY_F22, // 0x85 VK_F22
  578. KEY_F23, // 0x86 VK_F23
  579. KEY_F24, // 0x87 VK_F24
  580. 0, // 0x88
  581. 0, // 0x89
  582. 0, // 0x8A
  583. 0, // 0x8B
  584. 0, // 0x8C
  585. 0, // 0x8D
  586. 0, // 0x8E
  587. 0, // 0x8F
  588. KEY_NUMLOCK, // 0x90 VK_NUMLOCK
  589. KEY_SCROLLLOCK, // 0x91 VK_OEM_SCROLL
  590. 0, // 0x92
  591. 0, // 0x93
  592. 0, // 0x94
  593. 0, // 0x95
  594. 0, // 0x96
  595. 0, // 0x97
  596. 0, // 0x98
  597. 0, // 0x99
  598. 0, // 0x9A
  599. 0, // 0x9B
  600. 0, // 0x9C
  601. 0, // 0x9D
  602. 0, // 0x9E
  603. 0, // 0x9F
  604. KEY_LSHIFT, // 0xA0 VK_LSHIFT
  605. KEY_RSHIFT, // 0xA1 VK_RSHIFT
  606. KEY_LCONTROL, // 0xA2 VK_LCONTROL
  607. KEY_RCONTROL, // 0xA3 VK_RCONTROL
  608. KEY_LALT, // 0xA4 VK_LMENU
  609. KEY_RALT, // 0xA5 VK_RMENU
  610. 0, // 0xA6
  611. 0, // 0xA7
  612. 0, // 0xA8
  613. 0, // 0xA9
  614. 0, // 0xAA
  615. 0, // 0xAB
  616. 0, // 0xAC
  617. 0, // 0xAD
  618. 0, // 0xAE
  619. 0, // 0xAF
  620. 0, // 0xB0
  621. 0, // 0xB1
  622. 0, // 0xB2
  623. 0, // 0xB3
  624. 0, // 0xB4
  625. 0, // 0xB5
  626. 0, // 0xB6
  627. 0, // 0xB7
  628. 0, // 0xB8
  629. 0, // 0xB9
  630. KEY_SEMICOLON, // 0xBA VK_OEM_1
  631. KEY_EQUALS, // 0xBB VK_OEM_PLUS
  632. KEY_COMMA, // 0xBC VK_OEM_COMMA
  633. KEY_MINUS, // 0xBD VK_OEM_MINUS
  634. KEY_PERIOD, // 0xBE VK_OEM_PERIOD
  635. KEY_SLASH, // 0xBF VK_OEM_2
  636. KEY_TILDE, // 0xC0 VK_OEM_3
  637. 0, // 0xC1
  638. 0, // 0xC2
  639. 0, // 0xC3
  640. 0, // 0xC4
  641. 0, // 0xC5
  642. 0, // 0xC6
  643. 0, // 0xC7
  644. 0, // 0xC8
  645. 0, // 0xC9
  646. 0, // 0xCA
  647. 0, // 0xCB
  648. 0, // 0xCC
  649. 0, // 0xCD
  650. 0, // 0xCE
  651. 0, // 0xCF
  652. 0, // 0xD0
  653. 0, // 0xD1
  654. 0, // 0xD2
  655. 0, // 0xD3
  656. 0, // 0xD4
  657. 0, // 0xD5
  658. 0, // 0xD6
  659. 0, // 0xD7
  660. 0, // 0xD8
  661. 0, // 0xD9
  662. 0, // 0xDA
  663. KEY_LBRACKET, // 0xDB VK_OEM_4
  664. KEY_BACKSLASH, // 0xDC VK_OEM_5
  665. KEY_RBRACKET, // 0xDD VK_OEM_6
  666. KEY_APOSTROPHE, // 0xDE VK_OEM_7
  667. 0, // 0xDF VK_OEM_8
  668. 0, // 0xE0
  669. 0, // 0xE1 VK_OEM_AX AX key on Japanese AX keyboard
  670. KEY_OEM_102, // 0xE2 VK_OEM_102
  671. 0, // 0xE3
  672. 0, // 0xE4
  673. 0, // 0xE5 VK_PROCESSKEY
  674. 0, // 0xE6
  675. 0, // 0xE7
  676. 0, // 0xE8
  677. 0, // 0xE9
  678. 0, // 0xEA
  679. 0, // 0xEB
  680. 0, // 0xEC
  681. 0, // 0xED
  682. 0, // 0xEE
  683. 0, // 0xEF
  684. 0, // 0xF0
  685. 0, // 0xF1
  686. 0, // 0xF2
  687. 0, // 0xF3
  688. 0, // 0xF4
  689. 0, // 0xF5
  690. 0, // 0xF6 VK_ATTN
  691. 0, // 0xF7 VK_CRSEL
  692. 0, // 0xF8 VK_EXSEL
  693. 0, // 0xF9 VK_EREOF
  694. 0, // 0xFA VK_PLAY
  695. 0, // 0xFB VK_ZOOM
  696. 0, // 0xFC VK_NONAME
  697. 0, // 0xFD VK_PA1
  698. 0, // 0xFE VK_OEM_CLEAR
  699. 0 // 0xFF
  700. };
  701. //------------------------------------------------------------------------------
  702. //
  703. // This function translates a virtual key code to our corresponding internal
  704. // key code using the preceding table.
  705. //
  706. //------------------------------------------------------------------------------
  707. U8 TranslateOSKeyCode(U8 vcode)
  708. {
  709. return VcodeRemap[vcode];
  710. }
  711. U8 TranslateKeyCodeToOS(U8 keycode)
  712. {
  713. for(S32 i = 0;i < sizeof(VcodeRemap) / sizeof(U8);++i)
  714. {
  715. if(VcodeRemap[i] == keycode)
  716. return i;
  717. }
  718. return 0;
  719. }
  720. //-----------------------------------------------------------------------------
  721. // Clipboard functions
  722. const char* Platform::getClipboard()
  723. {
  724. HGLOBAL hGlobal;
  725. LPVOID pGlobal;
  726. //make sure we can access the clipboard
  727. if (!IsClipboardFormatAvailable(CF_TEXT))
  728. return "";
  729. if (!OpenClipboard(NULL))
  730. return "";
  731. hGlobal = GetClipboardData(CF_TEXT);
  732. pGlobal = GlobalLock(hGlobal);
  733. S32 cbLength = strlen((char *)pGlobal);
  734. char *returnBuf = Con::getReturnBuffer(cbLength + 1);
  735. strcpy(returnBuf, (char *)pGlobal);
  736. returnBuf[cbLength] = '\0';
  737. GlobalUnlock(hGlobal);
  738. CloseClipboard();
  739. //note - this function never returns NULL
  740. return returnBuf;
  741. }
  742. //-----------------------------------------------------------------------------
  743. bool Platform::setClipboard(const char *text)
  744. {
  745. if (!text)
  746. return false;
  747. //make sure we can access the clipboard
  748. if (!OpenClipboard(NULL))
  749. return false;
  750. S32 cbLength = strlen(text);
  751. HGLOBAL hGlobal;
  752. LPVOID pGlobal;
  753. hGlobal = GlobalAlloc(GHND, cbLength + 1);
  754. pGlobal = GlobalLock (hGlobal);
  755. strcpy((char *)pGlobal, text);
  756. GlobalUnlock(hGlobal);
  757. EmptyClipboard();
  758. SetClipboardData(CF_TEXT, hGlobal);
  759. CloseClipboard();
  760. return true;
  761. }