KEYBOARD.BAK 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. * *
  20. * Project Name : Westwood Keyboard Library *
  21. * *
  22. * File Name : KEYBOARD.CPP *
  23. * *
  24. * Programmer : Philip W. Gorrow *
  25. * *
  26. * Start Date : 10/16/95 *
  27. * *
  28. * Last Update : October 26, 1995 [] *
  29. * *
  30. *---------------------------------------------------------------------------------------------*
  31. * Functions: *
  32. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  33. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  34. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  35. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  36. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  37. * WWKeyboardClass::Get_Mouse_X -- Returns the mouses current x position in pixels *
  38. * WWKeyboardClass::Get_Mouse_Y -- returns the mouses current y position in pixels *
  39. * WWKeyboardClass::Get_Mouse_XY -- Returns the mouses x,y position via reference vars *
  40. * Check_Key -- compatability routine for old 32 bit library *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "keyboard.h"
  43. void Message_Loop(void);
  44. WWKeyboardClass *_Kbd;
  45. /***********************************************************************************************
  46. * WWKeyboardClass::WWKeyBoardClass -- Construction for Westwood Keyboard Class *
  47. * *
  48. * INPUT: none *
  49. * *
  50. * OUTPUT: none *
  51. * *
  52. * HISTORY: *
  53. * 10/16/1995 PWG : Created. *
  54. *=============================================================================================*/
  55. WWKeyboardClass::WWKeyboardClass(void)
  56. {
  57. _Kbd = this;
  58. //
  59. // Initialize the keyboard remap table for our system (note it would be bad if someone
  60. // switched keyboard modes after this happened.
  61. //
  62. memset(VKRemap, 0, 256);
  63. memset(AsciiRemap, 0, 2048);
  64. for (short lp = 31; lp < 255; lp ++) {
  65. !----- Conflict 1 - Base File: C:\TEMP\KEYBOARD.CPP( *** PARENT *** )
  66. !----- File 1: D:\WIN32LIB\KEYBOARD\KEYBOARD.CPP( *** Primary *** )
  67. int vk_key = VkKeyScan((unsigned char)lp);
  68. !----- File 2: I:\WIN32LIB\KEYBOARD\KEYBOARD.CPP( *** Secondary *** )
  69. int vk_key = (int)VkKeyScan((TCHAR)lp);
  70. !----- End Conflict
  71. if (vk_key > 0 && vk_key < 2048) {
  72. AsciiRemap[vk_key] = (unsigned char)lp;
  73. VKRemap[lp] = (unsigned char)(vk_key & 0xFF);
  74. }
  75. }
  76. //
  77. // Build a remap table of the different keys which are affected by the caps lock and
  78. // the num lock.
  79. //
  80. memset(ToggleKeys, 0, 256);
  81. for (lp = 0; lp < 255; lp++ ) {
  82. if (isalpha(lp) && isupper(lp)) {
  83. ToggleKeys[lp] = 1;
  84. }
  85. if (lp >= VK_NUMPAD0 && lp <= VK_DIVIDE) {
  86. ToggleKeys[lp] = 2;
  87. }
  88. }
  89. //
  90. // Our buffer should start devoid of keys.
  91. //
  92. memset(Buffer, 0, 256);
  93. Head = 0;
  94. Tail = 0;
  95. //
  96. // There should be no starting queued mouse events for us to have to worry
  97. // about.
  98. //
  99. MouseQX = 0;
  100. MouseQY = 0;
  101. MState = 0;
  102. Conditional = 0;
  103. CurrentCursor = NULL;
  104. }
  105. /***********************************************************************************************
  106. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  107. * *
  108. * INPUT: none *
  109. * *
  110. * OUTPUT: int - the key value that was pulled from buffer (includes bits) * *
  111. * *
  112. * WARNINGS: If the key was a mouse event MouseQX and MouseQY will be updated *
  113. * *
  114. * HISTORY: *
  115. * 10/17/1995 PWG : Created. *
  116. *=============================================================================================*/
  117. int WWKeyboardClass::Buff_Get(void)
  118. {
  119. while (!Check()) {} // wait for key in buffer
  120. int temp = Buffer[Head]; // get key out of the buffer
  121. int newhead = Head; // save off head for manipulation
  122. if (Is_Mouse_Key(temp)) { // if key is a mouse then
  123. MouseQX = Buffer[Head+1]; // get the x and y pos
  124. MouseQY = Buffer[Head+2]; // from the buffer
  125. newhead += 3; // adjust head forward
  126. } else {
  127. newhead += 1; // adjust head forward
  128. }
  129. newhead &= 255;
  130. Head = newhead;
  131. return(temp);
  132. }
  133. BOOL WWKeyboardClass::Is_Mouse_Key(int key)
  134. {
  135. key &= 0xFF;
  136. return (key == VK_LBUTTON || key == VK_MBUTTON || key == VK_RBUTTON);
  137. }
  138. /***********************************************************************************************
  139. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  140. * *
  141. * INPUT: *
  142. * *
  143. * OUTPUT: *
  144. * *
  145. * WARNINGS: *
  146. * *
  147. * HISTORY: *
  148. * 10/16/1995 PWG : Created. *
  149. *=============================================================================================*/
  150. BOOL WWKeyboardClass::Check(void)
  151. {
  152. Message_Loop();
  153. unsigned short temp; // store temp holding spot for key
  154. if (Head == Tail) return(FALSE); // if no keys in buff then get out
  155. temp = Buffer[Head]; // get key out of the buffer
  156. return(temp); // send it back to main program
  157. }
  158. /***********************************************************************************************
  159. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  160. * *
  161. * INPUT: none *
  162. * *
  163. * OUTPUT: int - the meta key taken from the buffer. *
  164. * *
  165. * WARNINGS: This routine will not return until a keypress is received *
  166. * *
  167. * HISTORY: *
  168. * 10/16/1995 PWG : Created. *
  169. *=============================================================================================*/
  170. int WWKeyboardClass::Get(void)
  171. {
  172. int temp,bits; // store temp holding spot for key
  173. while (!Check()) {} // wait for key in buffer
  174. temp = Buff_Get(); // get key from the buffer
  175. bits = temp & 0xFF00; // save of keyboard bits
  176. if (!(bits & WWKEY_VK_BIT)) { // if its not a virtual key
  177. temp = AsciiRemap[temp&0x1FF] | bits; // convert to ascii equivalent
  178. }
  179. return(temp); // return the key that we pulled out
  180. }
  181. /***********************************************************************************************
  182. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  183. * *
  184. * INPUT: int - the key to insert into the buffer *
  185. * *
  186. * OUTPUT: bool - true if key is sucessfuly inserted. *
  187. * *
  188. * WARNINGS: none *
  189. * *
  190. * HISTORY: *
  191. * 10/16/1995 PWG : Created. *
  192. *=============================================================================================*/
  193. BOOL WWKeyboardClass::Put(int key)
  194. {
  195. int temp = (Tail + 1) & 255;
  196. if (temp != Head)
  197. {
  198. Buffer[Tail] = (short)key;
  199. //
  200. // Critical Line
  201. //
  202. Tail = temp;
  203. return(TRUE);
  204. }
  205. return(FALSE);
  206. }
  207. /***********************************************************************************************
  208. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  209. * *
  210. * INPUT: *
  211. * *
  212. * OUTPUT: *
  213. * *
  214. * WARNINGS: *
  215. * *
  216. * HISTORY: *
  217. * 10/16/1995 PWG : Created. *
  218. *=============================================================================================*/
  219. WWKeyboardClass::Put_Key_Message(UINT vk_key, BOOL release, BOOL dbl)
  220. {
  221. int bits = 0;
  222. //
  223. // Get the status of all of the different keyboard modifiers. Note, only pay attention
  224. // to numlock and caps lock if we are dealing with a key that is affected by them.
  225. //
  226. int shift = (GetKeyState(VK_SHIFT) & 0xFF00) != 0;
  227. int ctrl = (GetKeyState(VK_CONTROL) & 0xFF00) != 0;
  228. int alt = (GetKeyState(VK_MENU) & 0xFF00) != 0;
  229. int caps = ((GetKeyState(VK_CAPITAL) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 1);
  230. int nums = ((GetKeyState(VK_NUMLOCK) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 2);
  231. //
  232. // Set the proper bits for whatever the key we got is.
  233. //
  234. if (shift || caps || nums) {
  235. bits |= WWKEY_SHIFT_BIT;
  236. }
  237. if (ctrl) {
  238. bits |= WWKEY_CTRL_BIT;
  239. }
  240. if (alt) {
  241. bits |= WWKEY_ALT_BIT;
  242. }
  243. if (!AsciiRemap[vk_key|bits]) {
  244. bits |= WWKEY_VK_BIT;
  245. }
  246. if (release) {
  247. bits |= WWKEY_RLS_BIT;
  248. }
  249. if (dbl) {
  250. bits |= WWKEY_DBL_BIT;
  251. }
  252. //
  253. // Finally use the put command to enter the key into the keyboard
  254. // system.
  255. //
  256. return(Put(vk_key|bits));
  257. }
  258. void WWKeyboardClass::Clear(void)
  259. {
  260. Head = Tail;
  261. }
  262. int WWKeyboardClass::To_ASCII(int key)
  263. {
  264. return(key);
  265. }
  266. WWKeyboardClass::Down(int key)
  267. {
  268. return(GetAsyncKeyState(key&0xFF));
  269. }
  270. VOID WWKeyboardClass::Split(int &key, int &shift, int &ctrl, int &alt, int &rls, int &dbl)
  271. {
  272. shift = (key & WWKEY_SHIFT_BIT) != 0;
  273. ctrl = (key & WWKEY_CTRL_BIT) != 0;
  274. alt = (key & WWKEY_ALT_BIT) != 0;
  275. rls = (key & WWKEY_RLS_BIT) != 0;
  276. dbl = (key & WWKEY_DBL_BIT) != 0;
  277. key = (key & 0xFF);
  278. }
  279. extern "C" {
  280. void __cdecl Stop_Execution (void);
  281. }
  282. #pragma off(unreferenced)
  283. void WWKeyboardClass::Message_Handler(HWND , UINT message, UINT wParam, LONG lParam)
  284. {
  285. switch (message) {
  286. case WM_SYSKEYDOWN:
  287. case WM_KEYDOWN:
  288. if ( wParam==VK_SCROLL ){
  289. Stop_Execution();
  290. } else {
  291. Put_Key_Message(wParam);
  292. }
  293. break;
  294. case WM_SYSKEYUP:
  295. case WM_KEYUP:
  296. Put_Key_Message(wParam, TRUE);
  297. break;
  298. case WM_LBUTTONDOWN:
  299. Put_Key_Message(VK_LBUTTON);
  300. Put(LOWORD(lParam));
  301. Put(HIWORD(lParam));
  302. break;
  303. case WM_LBUTTONUP:
  304. Put_Key_Message(VK_LBUTTON, TRUE);
  305. Put(LOWORD(lParam));
  306. Put(HIWORD(lParam));
  307. break;
  308. case WM_LBUTTONDBLCLK:
  309. Put_Key_Message(VK_LBUTTON, TRUE, TRUE);
  310. Put(LOWORD(lParam));
  311. Put(HIWORD(lParam));
  312. break;
  313. case WM_MBUTTONDOWN:
  314. Put_Key_Message(VK_MBUTTON);
  315. Put(LOWORD(lParam));
  316. Put(HIWORD(lParam));
  317. break;
  318. case WM_MBUTTONUP:
  319. Put_Key_Message(VK_MBUTTON, TRUE);
  320. Put(LOWORD(lParam));
  321. Put(HIWORD(lParam));
  322. break;
  323. case WM_MBUTTONDBLCLK:
  324. Put_Key_Message(VK_MBUTTON, TRUE, TRUE);
  325. Put(LOWORD(lParam));
  326. Put(HIWORD(lParam));
  327. break;
  328. case WM_RBUTTONDOWN:
  329. Put_Key_Message(VK_RBUTTON);
  330. Put(LOWORD(lParam));
  331. Put(HIWORD(lParam));
  332. break;
  333. case WM_RBUTTONUP:
  334. Put_Key_Message(VK_RBUTTON, TRUE);
  335. Put(LOWORD(lParam));
  336. Put(HIWORD(lParam));
  337. break;
  338. case WM_RBUTTONDBLCLK:
  339. Put_Key_Message(VK_RBUTTON, TRUE, TRUE);
  340. Put(LOWORD(lParam));
  341. Put(HIWORD(lParam));
  342. break;
  343. case WM_MOUSEMOVE:
  344. if (CurrentCursor)
  345. SetCursor(CurrentCursor);
  346. break;
  347. }
  348. }
  349. #pragma on(unreferenced)
  350. extern BOOL GameInFocus;
  351. void Message_Loop(void)
  352. {
  353. MSG msg;
  354. do {
  355. while (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE )) {
  356. if( !GetMessage( &msg, NULL, 0, 0 ) ){
  357. return;
  358. }
  359. TranslateMessage(&msg);
  360. DispatchMessage(&msg);
  361. }
  362. } while (!GameInFocus);
  363. }
  364. /***************************************************************************
  365. * CHECK_KEY -- compatability routine for old 32 bit library *
  366. * *
  367. * This routine checks to see if there is a key in the keyboard buffer *
  368. * and returns it to the sender if there is. It does not remove the key *
  369. * from the buffer. *
  370. * *
  371. * INPUT: none *
  372. * *
  373. * OUTPUT: The key that was pressed. *
  374. * *
  375. * WARNINGS: You must declare a WWKeyboardClass object before calling *
  376. * this routine. *
  377. * *
  378. * HISTORY: *
  379. * 10/26/1995 : Created. *
  380. *=========================================================================*/
  381. int Check_Key(void)
  382. {
  383. if (!_Kbd) return(KA_NONE);
  384. return(_Kbd->Check() & ~WWKEY_SHIFT_BIT);
  385. }
  386. void Clear_KeyBuffer(void)
  387. {
  388. if (!_Kbd) return;
  389. _Kbd->Clear();
  390. }
  391. int Check_Key_Num(void)
  392. {
  393. if (!_Kbd) return(KN_NONE);
  394. int key = _Kbd->Check();
  395. int flags = key & 0xFF00;
  396. key = key & 0x00FF;
  397. if (isupper(key)) {
  398. key = tolower(key);
  399. flags |= WWKEY_SHIFT_BIT;
  400. }
  401. return(key | flags);
  402. }
  403. int Get_Key_Num(void)
  404. {
  405. if (!_Kbd) return(KN_NONE);
  406. int key = _Kbd->Get();
  407. int flags = key & 0xFF00;
  408. key = key & 0x00FF;
  409. if (isupper(key)) {
  410. key = tolower(key);
  411. flags |= WWKEY_SHIFT_BIT;
  412. }
  413. return(key | flags);
  414. }
  415. int KN_To_KA(int key)
  416. {
  417. if (!(key & WWKEY_VK_BIT)) {
  418. int flags = key & 0xFF00;
  419. key = key & 0x00FF;
  420. if (flags & WWKEY_SHIFT_BIT) {
  421. key = toupper(key);
  422. flags &= ~WWKEY_SHIFT_BIT;
  423. }
  424. }
  425. return(key);
  426. }
  427. int Key_Down(int key)
  428. {
  429. if (!_Kbd) return(FALSE);
  430. return(_Kbd->Down(key));
  431. }
  432. int Get_Key(void)
  433. {
  434. if (!_Kbd) return(KN_NONE);
  435. return(_Kbd->Get() & ~WWKEY_SHIFT_BIT);
  436. }