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
;
18 * FIXME Initial keyboard char not detected on first punch
25 PINPUT_RECORD InputRecord
= NULL
;
28 DWORD RecordIndex
= 0;
29 DWORD BufferIndex
= 0;
30 HANDLE StdInputHandle
= 0;
31 DWORD ConsoleInputMode
= 0;
33 /* Attempt some thread safety */
34 if (!CriticalSectionInitialized
)
36 InitializeCriticalSectionAndSpinCount(&CriticalSection
, 0x80000400);
37 CriticalSectionInitialized
= TRUE
;
40 EnterCriticalSection(&CriticalSection
);
44 LeaveCriticalSection(&CriticalSection
);
48 StdInputHandle
= GetStdHandle(STD_INPUT_HANDLE
);
50 /* Turn off processed input so we get key modifiers as well */
51 GetConsoleMode(StdInputHandle
, &ConsoleInputMode
);
53 SetConsoleMode(StdInputHandle
, ConsoleInputMode
& ~ENABLE_PROCESSED_INPUT
);
55 /* Start the process */
56 if (!GetNumberOfConsoleInputEvents(StdInputHandle
, &EventsRead
))
58 LeaveCriticalSection(&CriticalSection
);
64 LeaveCriticalSection(&CriticalSection
);
68 if (!(InputRecord
= (PINPUT_RECORD
)malloc(EventsRead
* sizeof(INPUT_RECORD
))))
70 LeaveCriticalSection(&CriticalSection
);
74 if (!ReadConsoleInput(StdInputHandle
, InputRecord
, EventsRead
, &NumberRead
))
77 LeaveCriticalSection(&CriticalSection
);
81 for (RecordIndex
= 0; RecordIndex
< NumberRead
; RecordIndex
++)
83 if (InputRecord
[RecordIndex
].EventType
== KEY_EVENT
&&
84 InputRecord
[RecordIndex
].Event
.KeyEvent
.bKeyDown
)
93 /* Restore console input mode */
94 SetConsoleMode(StdInputHandle
, ConsoleInputMode
);
96 LeaveCriticalSection(&CriticalSection
);