winInput.cpp 26 KB

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