ReactOS  0.4.14-dev-98-gb0d4763
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 
21 int _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 
BOOL WINAPI InitializeCriticalSectionAndSpinCount(OUT LPCRITICAL_SECTION lpCriticalSection, IN DWORD dwSpinCount)
Definition: synch.c:765
#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)
int _kbhit(void)
Definition: kbhit.c:21
BOOL WINAPI PeekConsoleInput(IN HANDLE hConsoleInput, OUT PINPUT_RECORD lpBuffer, IN DWORD nLength, OUT LPDWORD lpNumberOfEventsRead)
Definition: console.c:215
unsigned int BOOL
Definition: ntddk_ex.h:94
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