Home > Software engineering >  ReadConsoleInput read Windows console messages
ReadConsoleInput read Windows console messages

Time:11-17


# include
# include



VOID ErrorExit (LPSTR);
VOID KeyEventProc (KEY_EVENT_RECORD);
VOID MouseEventProc (MOUSE_EVENT_RECORD);
VOID ResizeEventProc (WINDOW_BUFFER_SIZE_RECORD);


Int main (VOID) {
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 ");//Save the current input mode, to be restored on the exit.
if (! GetConsoleMode (hStdin, & fdwSaveOldMode) ErrorExit (" GetConsoleMode ");//Enable the window and mouse input events.
FdwMode=ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (! SetConsoleMode (hStdin fdwMode)) ErrorExit (" SetConsoleMode ");//Loop to read and handle the input events.
While (counter++ <=100) {//Wait for events.
if (! ReadConsoleInput (hStdin,//input buffer handle
IrInBuf,//buffer to read into
In 128,//the size of the read buffer
& cNumRead))//the number of records read
ReadConsoleInput ErrorExit (" ");//Dispatch the events to the appropriate handler.

for (i=0; I The switch (irInBuf [I] EventType) {
Case KEY_EVENT://the rid_device_info_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) {fprintf (stderr, "% s \ n", lpszMessage); ExitProcess (0); }
VOID KeyEventProc (KEY_EVENT_RECORD ker) {
Printf (" Key event: ");
If (ker. BKeyDown) printf (" key pressed \ n ");
The else printf (" key released \ n ");
}

VOID MouseEventProc (MOUSE_EVENT_RECORD killing) {
# # ifndef MOUSE_HWHEELED
# define MOUSE_HWHEELED 0 x0008 SDF
# endif
Printf (" Mouse event: ");
The switch (killing dwEventFlags) {
Case 0: printf (" the button press \ n "); break;
Case DOUBLE_CLICK: printf (" double click \ n "); break;
Case MOUSE_HWHEELED: printf (" horizontal mouse wheel \ n "); break;
Case MOUSE_MOVED: printf (" mouse version \ n "); break;
Case MOUSE_WHEELED: printf (" vertical mouse wheel \ n "); break;
Default: printf (" unknown \ n "); break; }
}

VOID ResizeEventProc (WINDOW_BUFFER_SIZE_RECORD WBSR) {printf (" the Resize event \ n "); }
  • Related