Reading Input Buffer Events Example
The ReadConsoleInput() function can be used to directly access a console's input buffer. When a console is created, mouse input is enabled and window input is disabled. To ensure that the process receives all types of events, this example uses the SetConsoleMode() function to enable window and mouse input. Then it goes into a loop that reads and handles 100 console input events. For example, the message Keyboard event is displayed when the user presses a key and the message Mouse event is displayed when the user interacts with the mouse.
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
#include <windows.h>
#include <stdio.h>
// Prototypes
void ErrorExit(LPSTR);
void KeyEventProc(KEY_EVENT_RECORD);
void MouseEventProc(MOUSE_EVENT_RECORD);
void ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
int wmain(int argc, WCHAR **argv)
{
HANDLE hStdin;
DWORD cNumRead, fdwMode, fdwSaveOldMode, i;
INPUT_RECORD irInBuf[128];
int counter=0;
// Get the standard input handle.
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
ErrorExit(GetStdHandle());
else
wprintf(LGetStdHandle() is OK!\n);
// Save the current input mode, to be restored on exit.
if (!GetConsoleMode(hStdin, &fdwSaveOldMode) )
ErrorExit(GetConsoleMode());
else
wprintf(LGetConsoleMode() is OK!\n);
// Enable the window and mouse input events.
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (!SetConsoleMode(hStdin, fdwMode))
ErrorExit(SetConsoleMode());
else
wprintf(LSetConsoleMode() is OK!\n);
// Loop to read and handle the input events.
while (counter++ <= 30)
{
// Wait for the events
if (!ReadConsoleInput(
hStdin, // input buffer handle
irInBuf, // buffer to read into
128, // size of read buffer
&cNumRead) ) // number of records read
ErrorExit(ReadConsoleInput());
else
wprintf(LReadConsoleInput() is OK!\n);
// Dispatch the events to the appropriate handler
for (i = 0; i < cNumRead; i++)
{
switch(irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input
KeyEventProc(irInBuf[i].Event.KeyEvent);
break;
case MOUSE_EVENT: // mouse input
MouseEventProc(irInBuf[i].Event.MouseEvent);
break;
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
ResizeEventProc(irInBuf[i].Event.WindowBufferSizeEvent);
break;
case FOCUS_EVENT: // disregard focus events
case MENU_EVENT: // disregard menu events
break;
default:
ErrorExit(Unknown event type);
break;
}
}
}
return 0;
}
void ErrorExit (LPSTR lpszMessage)
{
fwprintf_s(stderr, L%s\n, lpszMessage);
ExitProcess(0);
}
void KeyEventProc(KEY_EVENT_RECORD ker)
{
wprintf(L Key event: );
if(ker.bKeyDown)
wprintf(Lkey pressed!\n);
else
wprintf(Lkey released!\n);
}
void MouseEventProc(MOUSE_EVENT_RECORD mer)
{
wprintf(L Mouse event: );
switch(mer.dwEventFlags)
{
case 0:
wprintf(Lbutton press!\n);
break;
case DOUBLE_CLICK:
wprintf(Ldouble click!\n);
break;
case MOUSE_HWHEELED:
wprintf(Lhorizontal mouse wheel!\n);
break;
case MOUSE_MOVED:
wprintf(Lmouse moved!\n);
break;
case MOUSE_WHEELED:
wprintf(Lvertical mouse wheel!\n);
break;
default:
wprintf(Lunknown\n);
break;
}
}
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
{
wprintf(LResize event!\n);
}
Build and run the project. Click, move and double click the mouse. The following screenshot is a sample output.