17.3.8 The Input Buffer

The particular set of keyboard, mouse, and window resize event messages that are stored in the input buffer depends on the current console input modes. Only the processed input mode that affects the key events placed in the input buffer. If enabled, processed mode filters the Ctrl-C combination and passes it to the functions registered with SetConsoleCtrlHandler. If mouse aware mode is enabled, mouse events are placed in the input buffer whenever the pointer is within the console window and the user moves the mouse or one of the mouse buttons is pressed or released. If window aware mode is enabled, an event is placed in the input buffer whenever the user changes the size of the screen buffer.. The modes that affect the input buffer can be queried using GetConsoleMode and set using SetConsoleMode . The different event types are placed into the single input buffer so that the application will receive the events in the order in which they occur. Except for the modes mentioned above, the effects of the other console modes (echo, wrap at EOL, and the line-editing features of the processed input and output modes) are not seen when using the Console functions. These modes are intended primarily for controlling the way ReadFile reads from the input buffer or writes to the screen buffer, and the way WriteFile writes to the screen buffer.

Applications may read from the input buffer by using ReadConsoleInput, which blocks until at least one event is available to be read. Then all available events are transferred to the caller's buffer until either no more events are available, or the caller's buffer is filled. Unread events remain in the input buffer for the next read call. The total number of events read is indicated to the caller.

You may also read from the input buffer without removing events by using PeekConsoleInput which copies available events into the caller's buffer until all events have been read or the caller's buffer is full. If no events are available, the call returns immediately. The total number of events peeked at is indicated.

GetNumberOfConsoleInputEvents can be used to determine the number of events in the input buffer. Or you could use a handle to console input as an object to wait for in either WaitForSingleObject or WaitForMultipleObjects. The input handle becomes Signalled when the input buffer becomes not empty. The handle is reset to Not-Signalled when the input buffer becomes empty.

WriteConsoleInput and FlushConsoleInputBuffer can be used if the input handle was opened with write access. WriteConsoleInput places input records into the input buffer behind any pending events in the buffer. The input buffer grows dynamically, if necessary, to hold as many events as are written. FlushConsoleInputBuffer empties the input buffer.

The console places events of any type into the input buffer in the form of input records. Input records have the following structure:

typedef struct _INPUT_RECORD {

WORD EventType;

union {

KEY_EVENT_RECORD KeyEvent;

MOUSE_EVENT_RECORD MouseEvent;

WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;

} Event;

} INPUT_RECORD, *PINPUT_RECORD;

The EventType field of an input record indicates whether the event contains a keyboard, mouse, or buffer size event.