2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/msvcrt/conio/kbhit.c
5 * PURPOSE: Checks for keyboard hits
6 * PROGRAMERS: Ariadne, Russell
9 * 27/9/08: An almost 100% working version of _kbhit()
14 static CRITICAL_SECTION CriticalSection
;
15 volatile BOOL CriticalSectionInitialized
=FALSE
;
23 PINPUT_RECORD InputRecord
= NULL
;
26 DWORD RecordIndex
= 0;
27 DWORD BufferIndex
= 0;
28 HANDLE StdInputHandle
= 0;
29 DWORD ConsoleInputMode
= 0;
31 /* Attempt some thread safety */
32 if (!CriticalSectionInitialized
)
34 InitializeCriticalSectionAndSpinCount(&CriticalSection
, 0x80000400);
35 CriticalSectionInitialized
= TRUE
;
38 EnterCriticalSection(&CriticalSection
);
42 LeaveCriticalSection(&CriticalSection
);
46 StdInputHandle
= GetStdHandle(STD_INPUT_HANDLE
);
48 /* Turn off processed input so we get key modifiers as well */
49 GetConsoleMode(StdInputHandle
, &ConsoleInputMode
);
51 SetConsoleMode(StdInputHandle
, ConsoleInputMode
& ~ENABLE_PROCESSED_INPUT
);
53 /* Start the process */
54 if (!GetNumberOfConsoleInputEvents(StdInputHandle
, &EventsRead
))
56 LeaveCriticalSection(&CriticalSection
);
62 LeaveCriticalSection(&CriticalSection
);
66 if (!(InputRecord
= (PINPUT_RECORD
)malloc(EventsRead
* sizeof(INPUT_RECORD
))))
68 LeaveCriticalSection(&CriticalSection
);
72 if (!PeekConsoleInput(StdInputHandle
, InputRecord
, EventsRead
, &NumberRead
))
75 LeaveCriticalSection(&CriticalSection
);
79 for (RecordIndex
= 0; RecordIndex
< NumberRead
; RecordIndex
++)
81 if (InputRecord
[RecordIndex
].EventType
== KEY_EVENT
&&
82 InputRecord
[RecordIndex
].Event
.KeyEvent
.bKeyDown
)
91 /* Restore console input mode */
92 SetConsoleMode(StdInputHandle
, ConsoleInputMode
);
94 LeaveCriticalSection(&CriticalSection
);