ReactOS 0.4.16-dev-340-g0540c21
kbhit.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/sdk/crt/conio/kbhit.c
5 * PURPOSE: Checks for keyboard hits
6 * PROGRAMERS: Ariadne, Russell
7 * UPDATE HISTORY:
8 * 28/12/98: Created
9 * 27/9/08: An almost 100% working version of _kbhit()
10 */
11
12#include <precomp.h>
13
16
17/*
18 * @implemented
19 */
20
21int _kbhit(void)
22{
23 PINPUT_RECORD InputRecord = NULL;
24 DWORD NumberRead = 0;
25 DWORD EventsRead = 0;
26 DWORD RecordIndex = 0;
27 DWORD BufferIndex = 0;
28 HANDLE StdInputHandle = 0;
29 DWORD ConsoleInputMode = 0;
30
31 /* Attempt some thread safety */
33 {
36 }
37
39
40 if (char_avail)
41 {
43 return 1;
44 }
45
46 StdInputHandle = GetStdHandle(STD_INPUT_HANDLE);
47
48 /* Turn off processed input so we get key modifiers as well */
49 GetConsoleMode(StdInputHandle, &ConsoleInputMode);
50
51 SetConsoleMode(StdInputHandle, ConsoleInputMode & ~ENABLE_PROCESSED_INPUT);
52
53 /* Start the process */
54 if (!GetNumberOfConsoleInputEvents(StdInputHandle, &EventsRead))
55 {
57 return 0;
58 }
59
60 if (!EventsRead)
61 {
63 return 0;
64 }
65
66 if (!(InputRecord = (PINPUT_RECORD)malloc(EventsRead * sizeof(INPUT_RECORD))))
67 {
69 return 0;
70 }
71
72 if (!PeekConsoleInput(StdInputHandle, InputRecord, EventsRead, &NumberRead))
73 {
74 free(InputRecord);
76 return 0;
77 }
78
79 for (RecordIndex = 0; RecordIndex < NumberRead; RecordIndex++)
80 {
81 if (InputRecord[RecordIndex].EventType == KEY_EVENT &&
82 InputRecord[RecordIndex].Event.KeyEvent.bKeyDown)
83 {
84 BufferIndex = 1;
85 break;
86 }
87 }
88
89 free(InputRecord);
90
91 /* Restore console input mode */
92 SetConsoleMode(StdInputHandle, ConsoleInputMode);
93
95
96 return BufferIndex;
97}
98
99
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1569
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1606
BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hConsoleInput, LPDWORD lpNumberOfEvents)
Definition: console.c:1635
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
volatile BOOL CriticalSectionInitialized
Definition: kbhit.c:15
static CRITICAL_SECTION CriticalSection
Definition: kbhit.c:14
int _kbhit(void)
Definition: kbhit.c:21
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_ EVENT_TYPE EventType
Definition: exfuncs.h:167
BOOL WINAPI InitializeCriticalSectionAndSpinCount(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount)
Definition: synch.c:765
int char_avail
Definition: ungetch.c:15
#define STD_INPUT_HANDLE
Definition: winbase.h:293
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define KEY_EVENT
Definition: wincon.h:128
#define PeekConsoleInput
Definition: wincon.h:776
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:78