ReactOS 0.4.15-dev-5664-g3bf4ef6
kbhit.c File Reference
#include <precomp.h>
Include dependency graph for kbhit.c:

Go to the source code of this file.

Functions

int _kbhit (void)
 

Variables

static CRITICAL_SECTION CriticalSection
 
volatile BOOL CriticalSectionInitialized =FALSE
 

Function Documentation

◆ _kbhit()

int _kbhit ( void  )

Definition at line 21 of file kbhit.c.

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}
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
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 long DWORD
Definition: ntddk_ex.h:95
volatile BOOL CriticalSectionInitialized
Definition: kbhit.c:15
static CRITICAL_SECTION CriticalSection
Definition: kbhit.c:14
_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:267
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

Referenced by gl_getcx(), and main().

Variable Documentation

◆ CriticalSection

CRITICAL_SECTION CriticalSection
static

Definition at line 14 of file kbhit.c.

Referenced by _kbhit().

◆ CriticalSectionInitialized

volatile BOOL CriticalSectionInitialized =FALSE

Definition at line 15 of file kbhit.c.

Referenced by _kbhit().