Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

729 lines
24 KiB

  1. /***
  2. *getch.c - contains _getch(), _getche(), _ungetch() and kbhit for Win32
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines the "direct console" functions listed above.
  8. *
  9. * NOTE: The real-mode DOS versions of these functions read from
  10. * standard input and are therefore redirected when standard input
  11. * is redirected. However, these versions ALWAYS read from the console,
  12. * even when standard input is redirected.
  13. *
  14. *Revision History:
  15. * 06-08-89 PHG Module created, based on asm version
  16. * 03-12-90 GJF Made calling type _CALLTYPE1, added #include
  17. * <cruntime.h> and fixed the copyright. Also, cleaned
  18. * up the formatting a bit.
  19. * 06-05-90 SBM Recoded as pure 32-bit, using new file handle state bits
  20. * 07-24-90 SBM Removed '32' from API names
  21. * 10-03-90 GJF New-style function declarators.
  22. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  23. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  24. * 01-16-91 GJF ANSI naming.
  25. * 01-25-91 SRW Get/SetConsoleMode parameters changed (_WIN32_)
  26. * 02-18-91 SRW Get/SetConsoleMode required read/write access (_WIN32_)
  27. * 02-19-91 SRW Adapt to OpenFile/CreateFile changes (_WIN32_)
  28. * 02-25-91 MHL Adapt to ReadFile/WriteFile changes (_WIN32_)
  29. * 07-26-91 GJF Took out init. stuff and cleaned up the error
  30. * handling [_WIN32_].
  31. * 12-11-92 GJF Initialize character buffer to 0 (int ch = 0;) and
  32. * use ReadConsole instead of ReadFile.
  33. * 03-20-93 GJF Completely rewritten, Cruiser support purged, _kbhit
  34. * (also rewritten) and _ungetch also merged in.
  35. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  36. * Change GetExtendedKeyCode to _getextendedkeycode
  37. * 05-10-93 GJF Fixed bug in _kbhit, pointer passed to free no
  38. * longer pointed to the allocated block.
  39. * 01-28-94 CFW Let's also look at the RIGHT_CTRL_PRESSED.
  40. * 09-06-94 CFW Replace MTHREAD with _MT.
  41. * 12-03-94 SKS Clean up OS/2 references
  42. * 01-13-95 GJF getche() should not echo a pushback character.
  43. * 01-10-95 CFW Debug CRT allocs.
  44. * 06-23-95 GJF Use _alloca instead of malloc/free. Also, make we
  45. * really need the event buffer before allocating it.
  46. * 10-05-95 SKS Remove two // commented-out lines
  47. * 12-08-95 SKS _coninph is now initialized on demand.
  48. * 02-07-98 GJF Changes for Win64: _coninph is now an intptr_t.
  49. * 12-10-99 GB Added support for recovery from stack overflow around
  50. * _alloca().
  51. * 04-25-00 GB exposed getextendedkeycode for use by _getwch()
  52. * 03-13-01 PML Fix heap leak if _alloca fails (vs7#224864)
  53. *
  54. *******************************************************************************/
  55. #include <cruntime.h>
  56. #include <oscalls.h>
  57. #include <conio.h>
  58. #include <internal.h>
  59. #include <mtdll.h>
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <dbgint.h>
  63. #include <malloc.h>
  64. typedef struct {
  65. unsigned char LeadChar;
  66. unsigned char SecondChar;
  67. } CharPair;
  68. typedef struct {
  69. unsigned short ScanCode;
  70. CharPair RegChars;
  71. CharPair ShiftChars;
  72. CharPair CtrlChars;
  73. CharPair AltChars;
  74. } EnhKeyVals;
  75. typedef struct {
  76. CharPair RegChars;
  77. CharPair ShiftChars;
  78. CharPair CtrlChars;
  79. CharPair AltChars;
  80. } NormKeyVals;
  81. /*
  82. * Table of key values for enhanced keys
  83. */
  84. const static EnhKeyVals EnhancedKeys[] = {
  85. { 28, { 13, 0 }, { 13, 0 }, { 10, 0 }, { 0, 166 } },
  86. { 53, { 47, 0 }, { 63, 0 }, { 0, 149 }, { 0, 164 } },
  87. { 71, { 224, 71 }, { 224, 71 }, { 224, 119 }, { 0, 151 } },
  88. { 72, { 224, 72 }, { 224, 72 }, { 224, 141 }, { 0, 152 } },
  89. { 73, { 224, 73 }, { 224, 73 }, { 224, 134 }, { 0, 153 } },
  90. { 75, { 224, 75 }, { 224, 75 }, { 224, 115 }, { 0, 155 } },
  91. { 77, { 224, 77 }, { 224, 77 }, { 224, 116 }, { 0, 157 } },
  92. { 79, { 224, 79 }, { 224, 79 }, { 224, 117 }, { 0, 159 } },
  93. { 80, { 224, 80 }, { 224, 80 }, { 224, 145 }, { 0, 160 } },
  94. { 81, { 224, 81 }, { 224, 81 }, { 224, 118 }, { 0, 161 } },
  95. { 82, { 224, 82 }, { 224, 82 }, { 224, 146 }, { 0, 162 } },
  96. { 83, { 224, 83 }, { 224, 83 }, { 224, 147 }, { 0, 163 } }
  97. };
  98. /*
  99. * macro for the number of elements of in EnhancedKeys[]
  100. */
  101. #define NUM_EKA_ELTS ( sizeof( EnhancedKeys ) / sizeof( EnhKeyVals ) )
  102. /*
  103. * Table of key values for normal keys. Note that the table is padded so
  104. * that the key scan code serves as an index into the table.
  105. */
  106. const static NormKeyVals NormalKeys[] = {
  107. /* padding */
  108. { /* 0 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  109. { /* 1 */ { 27, 0 }, { 27, 0 }, { 27, 0 }, { 0, 1 } },
  110. { /* 2 */ { 49, 0 }, { 33, 0 }, { 0, 0 }, { 0, 120 } },
  111. { /* 3 */ { 50, 0 }, { 64, 0 }, { 0, 3 }, { 0, 121 } },
  112. { /* 4 */ { 51, 0 }, { 35, 0 }, { 0, 0 }, { 0, 122 } },
  113. { /* 5 */ { 52, 0 }, { 36, 0 }, { 0, 0 }, { 0, 123 } },
  114. { /* 6 */ { 53, 0 }, { 37, 0 }, { 0, 0 }, { 0, 124 } },
  115. { /* 7 */ { 54, 0 }, { 94, 0 }, { 30, 0 }, { 0, 125 } },
  116. { /* 8 */ { 55, 0 }, { 38, 0 }, { 0, 0 }, { 0, 126 } },
  117. { /* 9 */ { 56, 0 }, { 42, 0 }, { 0, 0 }, { 0, 127 } },
  118. { /* 10 */ { 57, 0 }, { 40, 0 }, { 0, 0 }, { 0, 128 } },
  119. { /* 11 */ { 48, 0 }, { 41, 0 }, { 0, 0 }, { 0, 129 } },
  120. { /* 12 */ { 45, 0 }, { 95, 0 }, { 31, 0 }, { 0, 130 } },
  121. { /* 13 */ { 61, 0 }, { 43, 0 }, { 0, 0 }, { 0, 131 } },
  122. { /* 14 */ { 8, 0 }, { 8, 0 }, { 127, 0 }, { 0, 14 } },
  123. { /* 15 */ { 9, 0 }, { 0, 15 }, { 0, 148 }, { 0, 15 } },
  124. { /* 16 */ { 113, 0 }, { 81, 0 }, { 17, 0 }, { 0, 16 } },
  125. { /* 17 */ { 119, 0 }, { 87, 0 }, { 23, 0 }, { 0, 17 } },
  126. { /* 18 */ { 101, 0 }, { 69, 0 }, { 5, 0 }, { 0, 18 } },
  127. { /* 19 */ { 114, 0 }, { 82, 0 }, { 18, 0 }, { 0, 19 } },
  128. { /* 20 */ { 116, 0 }, { 84, 0 }, { 20, 0 }, { 0, 20 } },
  129. { /* 21 */ { 121, 0 }, { 89, 0 }, { 25, 0 }, { 0, 21 } },
  130. { /* 22 */ { 117, 0 }, { 85, 0 }, { 21, 0 }, { 0, 22 } },
  131. { /* 23 */ { 105, 0 }, { 73, 0 }, { 9, 0 }, { 0, 23 } },
  132. { /* 24 */ { 111, 0 }, { 79, 0 }, { 15, 0 }, { 0, 24 } },
  133. { /* 25 */ { 112, 0 }, { 80, 0 }, { 16, 0 }, { 0, 25 } },
  134. { /* 26 */ { 91, 0 }, { 123, 0 }, { 27, 0 }, { 0, 26 } },
  135. { /* 27 */ { 93, 0 }, { 125, 0 }, { 29, 0 }, { 0, 27 } },
  136. { /* 28 */ { 13, 0 }, { 13, 0 }, { 10, 0 }, { 0, 28 } },
  137. /* padding */
  138. { /* 29 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  139. { /* 30 */ { 97, 0 }, { 65, 0 }, { 1, 0 }, { 0, 30 } },
  140. { /* 31 */ { 115, 0 }, { 83, 0 }, { 19, 0 }, { 0, 31 } },
  141. { /* 32 */ { 100, 0 }, { 68, 0 }, { 4, 0 }, { 0, 32 } },
  142. { /* 33 */ { 102, 0 }, { 70, 0 }, { 6, 0 }, { 0, 33 } },
  143. { /* 34 */ { 103, 0 }, { 71, 0 }, { 7, 0 }, { 0, 34 } },
  144. { /* 35 */ { 104, 0 }, { 72, 0 }, { 8, 0 }, { 0, 35 } },
  145. { /* 36 */ { 106, 0 }, { 74, 0 }, { 10, 0 }, { 0, 36 } },
  146. { /* 37 */ { 107, 0 }, { 75, 0 }, { 11, 0 }, { 0, 37 } },
  147. { /* 38 */ { 108, 0 }, { 76, 0 }, { 12, 0 }, { 0, 38 } },
  148. { /* 39 */ { 59, 0 }, { 58, 0 }, { 0, 0 }, { 0, 39 } },
  149. { /* 40 */ { 39, 0 }, { 34, 0 }, { 0, 0 }, { 0, 40 } },
  150. { /* 41 */ { 96, 0 }, { 126, 0 }, { 0, 0 }, { 0, 41 } },
  151. /* padding */
  152. { /* 42 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  153. { /* 43 */ { 92, 0 }, { 124, 0 }, { 28, 0 }, { 0, 0 } },
  154. { /* 44 */ { 122, 0 }, { 90, 0 }, { 26, 0 }, { 0, 44 } },
  155. { /* 45 */ { 120, 0 }, { 88, 0 }, { 24, 0 }, { 0, 45 } },
  156. { /* 46 */ { 99, 0 }, { 67, 0 }, { 3, 0 }, { 0, 46 } },
  157. { /* 47 */ { 118, 0 }, { 86, 0 }, { 22, 0 }, { 0, 47 } },
  158. { /* 48 */ { 98, 0 }, { 66, 0 }, { 2, 0 }, { 0, 48 } },
  159. { /* 49 */ { 110, 0 }, { 78, 0 }, { 14, 0 }, { 0, 49 } },
  160. { /* 50 */ { 109, 0 }, { 77, 0 }, { 13, 0 }, { 0, 50 } },
  161. { /* 51 */ { 44, 0 }, { 60, 0 }, { 0, 0 }, { 0, 51 } },
  162. { /* 52 */ { 46, 0 }, { 62, 0 }, { 0, 0 }, { 0, 52 } },
  163. { /* 53 */ { 47, 0 }, { 63, 0 }, { 0, 0 }, { 0, 53 } },
  164. /* padding */
  165. { /* 54 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  166. { /* 55 */ { 42, 0 }, { 0, 0 }, { 114, 0 }, { 0, 0 } },
  167. /* padding */
  168. { /* 56 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  169. { /* 57 */ { 32, 0 }, { 32, 0 }, { 32, 0 }, { 32, 0 } },
  170. /* padding */
  171. { /* 58 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  172. { /* 59 */ { 0, 59 }, { 0, 84 }, { 0, 94 }, { 0, 104 } },
  173. { /* 60 */ { 0, 60 }, { 0, 85 }, { 0, 95 }, { 0, 105 } },
  174. { /* 61 */ { 0, 61 }, { 0, 86 }, { 0, 96 }, { 0, 106 } },
  175. { /* 62 */ { 0, 62 }, { 0, 87 }, { 0, 97 }, { 0, 107 } },
  176. { /* 63 */ { 0, 63 }, { 0, 88 }, { 0, 98 }, { 0, 108 } },
  177. { /* 64 */ { 0, 64 }, { 0, 89 }, { 0, 99 }, { 0, 109 } },
  178. { /* 65 */ { 0, 65 }, { 0, 90 }, { 0, 100 }, { 0, 110 } },
  179. { /* 66 */ { 0, 66 }, { 0, 91 }, { 0, 101 }, { 0, 111 } },
  180. { /* 67 */ { 0, 67 }, { 0, 92 }, { 0, 102 }, { 0, 112 } },
  181. { /* 68 */ { 0, 68 }, { 0, 93 }, { 0, 103 }, { 0, 113 } },
  182. /* padding */
  183. { /* 69 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  184. { /* 70 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  185. { /* 71 */ { 0, 71 }, { 55, 0 }, { 0, 119 }, { 0, 0 } },
  186. { /* 72 */ { 0, 72 }, { 56, 0 }, { 0, 141 }, { 0, 0 } },
  187. { /* 73 */ { 0, 73 }, { 57, 0 }, { 0, 132 }, { 0, 0 } },
  188. { /* 74 */ { 0, 0 }, { 45, 0 }, { 0, 0 }, { 0, 0 } },
  189. { /* 75 */ { 0, 75 }, { 52, 0 }, { 0, 115 }, { 0, 0 } },
  190. { /* 76 */ { 0, 0 }, { 53, 0 }, { 0, 0 }, { 0, 0 } },
  191. { /* 77 */ { 0, 77 }, { 54, 0 }, { 0, 116 }, { 0, 0 } },
  192. { /* 78 */ { 0, 0 }, { 43, 0 }, { 0, 0 }, { 0, 0 } },
  193. { /* 79 */ { 0, 79 }, { 49, 0 }, { 0, 117 }, { 0, 0 } },
  194. { /* 80 */ { 0, 80 }, { 50, 0 }, { 0, 145 }, { 0, 0 } },
  195. { /* 81 */ { 0, 81 }, { 51, 0 }, { 0, 118 }, { 0, 0 } },
  196. { /* 82 */ { 0, 82 }, { 48, 0 }, { 0, 146 }, { 0, 0 } },
  197. { /* 83 */ { 0, 83 }, { 46, 0 }, { 0, 147 }, { 0, 0 } },
  198. /* padding */
  199. { /* 84 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  200. { /* 85 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  201. { /* 86 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
  202. { /* 87 */ { 224, 133 }, { 224, 135 }, { 224, 137 }, { 224, 139 } },
  203. { /* 88 */ { 224, 134 }, { 224, 136 }, { 224, 138 }, { 224, 140 } }
  204. };
  205. /*
  206. * This is the one character push-back buffer used by _getch(), _getche()
  207. * and _ungetch().
  208. */
  209. static int chbuf = EOF;
  210. /*
  211. * Declaration for console handle
  212. */
  213. extern intptr_t _coninpfh;
  214. /*
  215. * Function that looks up the extended key code for a given event.
  216. */
  217. const CharPair * __cdecl _getextendedkeycode(KEY_EVENT_RECORD *);
  218. /***
  219. *int _getch(), _getche() - read one char. from console (without and with echo)
  220. *
  221. *Purpose:
  222. * If the "_ungetch()" push-back buffer is not empty (empty==-1) Then
  223. * Mark it empty (-1) and RETURN the value that was in it
  224. * Read a character using ReadConsole in RAW mode
  225. * Return the Character Code
  226. * _getche(): Same as _getch() except that the character value returned
  227. * is echoed (via "_putch()")
  228. *
  229. *Entry:
  230. * None, reads from console.
  231. *
  232. *Exit:
  233. * If an error is returned from the API
  234. * Then EOF
  235. * Otherwise
  236. * next byte from console
  237. * Static variable "chbuf" may be altered
  238. *
  239. *Exceptions:
  240. *
  241. *******************************************************************************/
  242. #ifdef _MT
  243. int __cdecl _getch (
  244. void
  245. )
  246. {
  247. int ch;
  248. _mlock(_CONIO_LOCK); /* secure the console lock */
  249. ch = _getch_lk(); /* input the character */
  250. _munlock(_CONIO_LOCK); /* release the console lock */
  251. return ch;
  252. }
  253. int __cdecl _getche (
  254. void
  255. )
  256. {
  257. int ch;
  258. _mlock(_CONIO_LOCK); /* secure the console lock */
  259. ch = _getche_lk(); /* input and echo the character */
  260. _munlock(_CONIO_LOCK); /* unlock the console */
  261. return ch;
  262. }
  263. #endif /* _MT */
  264. #ifdef _MT
  265. int __cdecl _getch_lk (
  266. #else
  267. int __cdecl _getch (
  268. #endif
  269. void
  270. )
  271. {
  272. INPUT_RECORD ConInpRec;
  273. DWORD NumRead;
  274. const CharPair *pCP;
  275. int ch = 0; /* single character buffer */
  276. DWORD oldstate;
  277. /*
  278. * check pushback buffer (chbuf) a for character
  279. */
  280. if ( chbuf != EOF ) {
  281. /*
  282. * something there, clear buffer and return the character.
  283. */
  284. ch = (unsigned char)(chbuf & 0xFF);
  285. chbuf = EOF;
  286. return ch;
  287. }
  288. if (_coninpfh == -1)
  289. return EOF;
  290. /*
  291. * _coninpfh, the handle to the console input, is created the first
  292. * time that either _getch() or _cgets() or _kbhit() is called.
  293. */
  294. if ( _coninpfh == -2 )
  295. __initconin();
  296. /*
  297. * Switch to raw mode (no line input, no echo input)
  298. */
  299. GetConsoleMode( (HANDLE)_coninpfh, &oldstate );
  300. SetConsoleMode( (HANDLE)_coninpfh, 0L );
  301. for ( ; ; ) {
  302. /*
  303. * Get a console input event.
  304. */
  305. if ( !ReadConsoleInput( (HANDLE)_coninpfh,
  306. &ConInpRec,
  307. 1L,
  308. &NumRead )
  309. || (NumRead == 0L) )
  310. {
  311. ch = EOF;
  312. break;
  313. }
  314. /*
  315. * Look for, and decipher, key events.
  316. */
  317. if ( (ConInpRec.EventType == KEY_EVENT) &&
  318. ConInpRec.Event.KeyEvent.bKeyDown ) {
  319. /*
  320. * Easy case: if uChar.AsciiChar is non-zero, just stuff it
  321. * into ch and quit.
  322. */
  323. if ( ch = (unsigned char)ConInpRec.Event.KeyEvent.uChar.AsciiChar )
  324. break;
  325. /*
  326. * Hard case: either an extended code or an event which should
  327. * not be recognized. let _getextendedkeycode() do the work...
  328. */
  329. if ( pCP = _getextendedkeycode( &(ConInpRec.Event.KeyEvent) ) ) {
  330. ch = pCP->LeadChar;
  331. chbuf = pCP->SecondChar;
  332. break;
  333. }
  334. }
  335. }
  336. /*
  337. * Restore previous console mode.
  338. */
  339. SetConsoleMode( (HANDLE)_coninpfh, oldstate );
  340. return ch;
  341. }
  342. /*
  343. * getche is just getch followed by a putch if no error occurred
  344. */
  345. #ifdef _MT
  346. int __cdecl _getche_lk (
  347. #else
  348. int __cdecl _getche (
  349. #endif
  350. void
  351. )
  352. {
  353. int ch; /* character read */
  354. /*
  355. * check pushback buffer (chbuf) a for character. if found, return
  356. * it without echoing.
  357. */
  358. if ( chbuf != EOF ) {
  359. /*
  360. * something there, clear buffer and return the character.
  361. */
  362. ch = (unsigned char)(chbuf & 0xFF);
  363. chbuf = EOF;
  364. return ch;
  365. }
  366. ch = _getch_lk(); /* read character */
  367. if (ch != EOF) {
  368. if (_putch_lk(ch) != EOF) {
  369. return ch; /* if no error, return char */
  370. }
  371. }
  372. return EOF; /* get or put failed, return EOF */
  373. }
  374. /***
  375. *int _kbhit() - return flag if a keystroke is already waiting to be read
  376. *
  377. *Purpose:
  378. * If the "_ungetch()" push-back buffer is not empty (empty==-1) Then
  379. * Return TRUE
  380. * Otherwise get the Keyboard Status (via DOSQUERYFHSTATE)
  381. *
  382. *Entry:
  383. * None, tests console.
  384. *
  385. *Exit:
  386. * returns 0 if no key waiting
  387. * = !0 if key waiting
  388. *
  389. *Exceptions:
  390. *
  391. *******************************************************************************/
  392. #ifdef _MT
  393. int __cdecl _kbhit_lk(void);
  394. int __cdecl _kbhit (
  395. void
  396. )
  397. {
  398. int retval;
  399. _mlock(_CONIO_LOCK); /* secure the console lock */
  400. retval = _kbhit_lk(); /* determine if a key is waiting */
  401. _munlock(_CONIO_LOCK); /* release the console lock */
  402. return retval;
  403. }
  404. int __cdecl _kbhit_lk (
  405. #else
  406. int __cdecl _kbhit (
  407. #endif
  408. void
  409. )
  410. {
  411. PINPUT_RECORD pIRBuf;
  412. DWORD NumPending;
  413. DWORD NumPeeked;
  414. int malloc_flag = 0;
  415. int ret = FALSE;
  416. /*
  417. * if a character has been pushed back, return TRUE
  418. */
  419. if ( chbuf != -1 )
  420. return TRUE;
  421. /*
  422. * _coninpfh, the handle to the console input, is created the first
  423. * time that either _getch() or _cgets() or _kbhit() is called.
  424. */
  425. if ( _coninpfh == -2 )
  426. __initconin();
  427. /*
  428. * Peek all pending console events
  429. */
  430. if ( (_coninpfh == -1) ||
  431. !GetNumberOfConsoleInputEvents((HANDLE)_coninpfh, &NumPending) ||
  432. (NumPending == 0))
  433. {
  434. return FALSE;
  435. }
  436. __try {
  437. pIRBuf = (PINPUT_RECORD)_alloca( NumPending * sizeof(INPUT_RECORD));
  438. }
  439. __except (EXCEPTION_EXECUTE_HANDLER) {
  440. _resetstkoflw();
  441. pIRBuf = NULL;
  442. }
  443. if ( pIRBuf == NULL )
  444. {
  445. pIRBuf = (PINPUT_RECORD)_malloc_crt( NumPending *
  446. sizeof(INPUT_RECORD));
  447. if ( pIRBuf == NULL )
  448. return FALSE;
  449. malloc_flag = 1;
  450. }
  451. if ( PeekConsoleInput( (HANDLE)_coninpfh,
  452. pIRBuf,
  453. NumPending,
  454. &NumPeeked ) &&
  455. (NumPeeked != 0L) &&
  456. (NumPeeked <= NumPending) )
  457. {
  458. /*
  459. * Scan all of the peeked events to determine if any is a key event
  460. * which should be recognized.
  461. */
  462. for ( ; NumPeeked > 0 ; NumPeeked--, pIRBuf++ ) {
  463. if ( (pIRBuf->EventType == KEY_EVENT) &&
  464. (pIRBuf->Event.KeyEvent.bKeyDown) &&
  465. ( pIRBuf->Event.KeyEvent.uChar.AsciiChar ||
  466. _getextendedkeycode( &(pIRBuf->Event.KeyEvent) ) ) )
  467. {
  468. /*
  469. * Key event corresponding to an ASCII character or an
  470. * extended code. In either case, success!
  471. */
  472. ret = TRUE;
  473. }
  474. }
  475. }
  476. if ( malloc_flag )
  477. _free_crt( pIRBuf );
  478. return ret;
  479. }
  480. /***
  481. *int _ungetch(c) - push back one character for "_getch()" or "_getche()"
  482. *
  483. *Purpose:
  484. * If the Push-back buffer "chbuf" is -1 Then
  485. * Set "chbuf" to the argument and return the argument
  486. * Else
  487. * Return EOF to indicate an error
  488. *
  489. *Entry:
  490. * int c - Character to be pushed back
  491. *
  492. *Exit:
  493. * If successful
  494. * returns character that was pushed back
  495. * Else if error
  496. * returns EOF
  497. *
  498. *Exceptions:
  499. *
  500. *******************************************************************************/
  501. #ifdef _MT
  502. int __cdecl _ungetch (
  503. int c
  504. )
  505. {
  506. int retval;
  507. _mlock(_CONIO_LOCK); /* lock the console */
  508. retval = _ungetch_lk(c); /* pushback character */
  509. _munlock(_CONIO_LOCK); /* unlock the console */
  510. return retval;
  511. }
  512. int __cdecl _ungetch_lk (
  513. #else
  514. int __cdecl _ungetch (
  515. #endif
  516. int c
  517. )
  518. {
  519. /*
  520. * Fail if the char is EOF or the pushback buffer is non-empty
  521. */
  522. if ( (c == EOF) || (chbuf != EOF) )
  523. return EOF;
  524. chbuf = (c & 0xFF);
  525. return chbuf;
  526. }
  527. /***
  528. * static CharPair * _getextendedkeycode(pKE) - return extended code (if any)
  529. * for key event.
  530. *
  531. *Purpose:
  532. * Core function for _getch (and getche) and essential to _kbhit. This
  533. * is the function that determines whether or not a key event NOT
  534. * accompanied by an ASCII character has an extended code and returns
  535. * that code.
  536. *
  537. *Entry:
  538. * None, tests console.
  539. *
  540. *Exit:
  541. * if successful, returns a pointer to a CharPair value holding the lead
  542. * and second characters of the extended code.
  543. *
  544. * if unsuccesful, returns NULL
  545. *
  546. *Exceptions:
  547. *
  548. *******************************************************************************/
  549. const CharPair * __cdecl _getextendedkeycode (
  550. KEY_EVENT_RECORD *pKE
  551. )
  552. {
  553. DWORD CKS; /* hold dwControlKeyState value */
  554. const CharPair *pCP; /* pointer to CharPair containing extended
  555. code */
  556. int i;
  557. if ( (CKS = pKE->dwControlKeyState) & ENHANCED_KEY ) {
  558. /*
  559. * Find the appropriate entry in EnhancedKeys[]
  560. */
  561. for ( pCP = NULL, i = 0 ; i < NUM_EKA_ELTS ; i++ ) {
  562. if ( EnhancedKeys[i].ScanCode == pKE->wVirtualScanCode ) {
  563. if ( CKS & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) )
  564. pCP = &(EnhancedKeys[i].AltChars);
  565. else if ( CKS & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) )
  566. pCP = &(EnhancedKeys[i].CtrlChars);
  567. else if ( CKS & SHIFT_PRESSED)
  568. pCP = &(EnhancedKeys[i].ShiftChars);
  569. else
  570. pCP = &(EnhancedKeys[i].RegChars);
  571. break;
  572. }
  573. }
  574. }
  575. else {
  576. /*
  577. * Regular key or a keyboard event which shouldn't be recognized.
  578. * Determine which by getting the proper field of the proper
  579. * entry in NormalKeys[], and examining the extended code.
  580. */
  581. if ( CKS & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) )
  582. pCP = &(NormalKeys[pKE->wVirtualScanCode].AltChars);
  583. else if ( CKS & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) )
  584. pCP = &(NormalKeys[pKE->wVirtualScanCode].CtrlChars);
  585. else if ( CKS & SHIFT_PRESSED)
  586. pCP = &(NormalKeys[pKE->wVirtualScanCode].ShiftChars);
  587. else
  588. pCP = &(NormalKeys[pKE->wVirtualScanCode].RegChars);
  589. if ( ((pCP->LeadChar != 0) && (pCP->LeadChar != 224)) ||
  590. (pCP->SecondChar == 0) )
  591. /*
  592. * Must be a keyboard event which should not be recognized
  593. * (e.g., shift key was pressed)
  594. */
  595. pCP = NULL;
  596. }
  597. return(pCP);
  598. }