eve.aio.windows.console

Async console input primitives for Windows.

This module provides Windows console input support for TUI applications. It uses a background thread to read console input events and delivers them to the event loop via callbacks.

The console input reader supports:

  • Key press/release events with virtual key codes and characters
  • Mouse events — clicks, movement, scrolling
  • Window resize events
  • Focus gain/loss events

Example:

auto loop = EventLoop.create();
auto console = ConsoleInput.create();

console.onEvent = (ref ConsoleInput c, scope const(ConsoleInputEvent)[] events) {
  foreach (ref evt; events) {
     if (evt.type == ConsoleEventType.KEY && evt.key.keyDown) {
        // Handle key press
     }
  }
};

console.start(loop);
loop.run();
console.stop();
console.dispose();

Types 9

enumConsoleEventType : ubyte

Console input event types.

Represents the category of console input event received from Windows.

KEYKeyboard key press or release event.
MOUSEMouse button, movement, or scroll event.
RESIZEConsole window resize event.
FOCUSConsole window focus gain or loss event.
MENUMenu event (typically ignored).
structKeyEvent

Key event data.

Contains information about a keyboard event including the key code, character produced, and modifier key state.

Fields
bool keyDown`true` for key press, `false` for key release.
ushort repeatCountNumber of times the key was repeated.
ushort virtualKeyCodeVirtual key code (VK_* constants).
ushort virtualScanCodeHardware scan code.
dchar characterUnicode character produced by the key press.
uint controlKeyStateModifier key state (CTRL, ALT, SHIFT flags).
private 0x0008 LEFT_CTRL_PRESSEDLeft Ctrl key pressed flag.
private 0x0004 RIGHT_CTRL_PRESSEDRight Ctrl key pressed flag.
private 0x0002 LEFT_ALT_PRESSEDLeft Alt key pressed flag.
private 0x0001 RIGHT_ALT_PRESSEDRight Alt key pressed flag.
private 0x0010 SHIFT_PRESSEDShift key pressed flag.
Methods
bool ctrlPressed() const pure @safe nothrow @nogcCheck if the Ctrl key is pressed.
bool altPressed() const pure @safe nothrow @nogcCheck if the Alt key is pressed.
bool shiftPressed() const pure @safe nothrow @nogcCheck if the Shift key is pressed.

Mouse event data.

Contains information about mouse position, button state, and event type.

Fields
short xX coordinate in console character cells.
short yY coordinate in console character cells.
uint buttonStateButton state flags.
uint controlKeyStateModifier key state (CTRL, ALT, SHIFT flags).
uint eventFlagsEvent flags indicating type of mouse event.
private 0x0001 MOUSE_MOVEDMouse moved flag.
private 0x0002 DOUBLE_CLICKDouble click flag.
private 0x0004 MOUSE_WHEELEDMouse wheel vertical scroll flag.
private 0x0008 MOUSE_HWHEELEDMouse wheel horizontal scroll flag.
private 0x0001 FROM_LEFT_1ST_BUTTON_PRESSEDLeft button pressed flag.
private 0x0002 RIGHTMOST_BUTTON_PRESSEDRight button pressed flag.
Methods
bool isMove() const pure @safe nothrow @nogcCheck if this is a mouse move event.
bool isDoubleClick() const pure @safe nothrow @nogcCheck if this is a double-click event.
bool isMouseWheel() const pure @safe nothrow @nogcCheck if this is a mouse wheel event.
bool leftButtonDown() const pure @safe nothrow @nogcCheck if the left mouse button is pressed.
bool rightButtonDown() const pure @safe nothrow @nogcCheck if the right mouse button is pressed.

Window resize event.

Contains the new console window dimensions in character cells.

Fields
short widthNew window width in character cells.
short heightNew window height in character cells.

Focus event.

Indicates whether the console window has gained or lost focus.

Fields
bool focused`true` when focus is gained, `false` when lost.

Union of all console input event types.

This struct represents a single console input event, with a type discriminator and a union of event-specific data.

Fields
ConsoleEventType typeThe type of this event.
private ubyte[3] _paddingPadding for alignment.
aliasConsoleInputCallback = void delegate( ref ConsoleInput console, scope const(ConsoleInputEvent)[] events ) @safe nothrow

Callback type for console input events.

Called when console input events are available. The events array is only valid for the duration of the callback.

/** Async console input reader for Windows.

Provides non-blocking console input for TUI applications by using a background thread to read INPUT_RECORDs and delivering them via the event loop callback mechanism.

The reader spawns a worker thread that blocks on ReadConsoleInput and signals the event loop when events are available. This allows the main thread to process console input alongside other async I/O.

Fields
private ConsoleInputState _state
Methods
ConsoleInput create() @trusted nothrowCreate a detached console input reader.
void onEvent(ConsoleInputCallback callback) @property @trusted nothrowRegister a callback for console input events.
ConsoleState state() @property const @trusted nothrow @nogcGet the current state of the console input reader.
bool isRunning() @property const @trusted nothrow @nogcCheck if the console reader is currently running.
StartResult start(ref EventLoop loop) @trusted nothrowStart the console input reader.
void stop() @trusted nothrowStop the console input reader.
void dispose() @trusted nothrowDispose of all resources.
const(ConsoleInputEvent)[] pendingEvents() scope @trusted nothrowGet pending events from the internal buffer.
private classConsoleInputState
Fields
HANDLE _consoleHandle
DWORD _originalMode
bool _modeChanged
ConsoleState _consoleState
EventLoop * _loop
Token _wakeupToken
Thread _worker
HANDLE _stopEvent
bool _shouldStop
Mutex _bufferMutex
ConsoleInputEvent[MAX_EVENTS] _eventBuffer
size_t _eventCount
Methods
StartResult start(ref EventLoop loop) @trusted nothrow
void stop() @trusted nothrow
void dispose() @trusted nothrow
void restoreConsoleMode() @trusted nothrow
void handleWakeup(ref EventLoop loop, Token token) @trusted nothrow
const(ConsoleInputEvent)[] pendingEvents() scope @trusted nothrow
void clearEvents() @trusted nothrow
void workerLoop() @trusted nothrow
void processRecords(INPUT_RECORD[] records) @trusted nothrow
Constructors

Functions 4

private fnint errno() ref @trusted nothrow @nogc
fnbool convertInputRecord(ref const INPUT_RECORD record, ref ConsoleInputEvent event) @trusted nothrow @nogcConvert a Windows INPUT_RECORD to a ConsoleInputEvent.
fnbool isPrintableKey(ushort vk) pure @safe nothrow @nogcCheck if a virtual key code represents a printable character.
fnconst(char)[] virtualKeyName(ushort vk) pure @safe nothrow @nogcGet a human-readable name for a virtual key code.

Variables 3

private varint _errno

Thread-local errno for Windows compatibility.

private enumvarMAX_EVENTS = 64

Maximum number of events buffered per read.

private enumvarENABLE_QUICK_EDIT_MODE = 0x0040

Quick Edit mode flag (intercepts mouse clicks for text selection). Must be disabled to receive mouse button events.