winInput.cpp 26 KB

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