ReactOS  0.4.13-dev-100-gc8611ae
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 }
BOOL WINAPI InitializeCriticalSectionAndSpinCount(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount)
Definition: synch.c:711
#define TRUE
Definition: types.h:120
int char_avail
Definition: ungetch.c:15
#define free
Definition: debug_ros.c:5
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1571
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
BOOL WINAPI PeekConsoleInput(IN HANDLE hConsoleInput, OUT PINPUT_RECORD lpBuffer, IN DWORD nLength, OUT LPDWORD lpNumberOfEventsRead)
Definition: console.c:215
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1608
#define KEY_EVENT
Definition: wincon.h:122
smooth NULL
Definition: ftsmooth.c:416
#define STD_INPUT_HANDLE
Definition: winbase.h:264
static CRITICAL_SECTION CriticalSection
Definition: kbhit.c:14
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hConsoleInput, LPDWORD lpNumberOfEvents)
Definition: console.c:1637
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:75
#define malloc
Definition: debug_ros.c:4
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_ EVENT_TYPE EventType
Definition: exfuncs.h:165
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
volatile BOOL CriticalSectionInitialized
Definition: kbhit.c:15

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().