Back to Projects
TUIX v0.3Beta

Last Updated: 2026-05-20

Input Handling

TUIX captures keyboard and mouse input on a dedicated background thread and delivers events to widgets via snapshots. In v0.3, input handling is automatic in the default builder pipeline.

Architecture

The input system runs as a background thread started by input.listen(). It reads raw platform events and enqueues them into two circular buffers: one for keyboard events and one for mouse events. Each buffer holds up to 128 events. In standard v0.3 usage, the frame loop and builder callbacks consume snapshots automatically.

Input Snapshots

A snapshot is a point-in-time capture of the input state. It contains the current terminal dimensions and pointers to keyboard and mouse event data.

# Automatic path (v0.3 default)
while running:
    engine.main_loop()

Snapshots are still the bridge between the input thread and widget logic. You can call input.get_snapshot() manually when needed, but the default v0.3 builder flow handles this for you.

Keyboard Events

Keyboard events contain key code, scan code, modifier flags, press/repeat state, timestamp, and up to 8 bytes of UTF-8 text.

FieldTypeDescription
btnintKey button identifier
codeintKey code
scancodeintHardware scan code
modifiersintModifier flags (Shift, Ctrl, Alt)
pressedint1 if key is down, 0 if up
repeatint1 if this is a key repeat event
timestampdoubleEvent timestamp
has_eventint1 if an event is present
textchar[8]UTF-8 text generated by the key

Mouse Events

Mouse events capture position, button, and event type. The event system supports press, release, hover, drag, scrolling, and double-click.

Event TypeValueDescription
TUIX_MOUSE_NONE0No event
TUIX_MOUSE_PRESS1Button pressed
TUIX_MOUSE_RELEASE2Button released
TUIX_MOUSE_HOVER3Mouse moved (no button)
TUIX_MOUSE_DRAG4Mouse moved with button held
TUIX_MOUSE_SCROLL_UP5Scroll wheel up
TUIX_MOUSE_SCROLL_DOWN6Scroll wheel down
TUIX_MOUSE_DOUBLE_CLICK7Double-click detected
TUIX_MOUSE_HSCROLL_LEFT8Horizontal scroll left
TUIX_MOUSE_HSCROLL_RIGHT9Horizontal scroll right
ButtonValue
TUIX_BTN_LEFT0
TUIX_BTN_MIDDLE1
TUIX_BTN_RIGHT2
TUIX_BTN_X13
TUIX_BTN_X24

Platform Implementation

On Windows, the input thread uses ReadConsoleInputW to receive native KEY_EVENT_RECORD and MOUSE_EVENT_RECORD structures. A button state machine tracks press/release edges for accurate click detection. ENABLE_VIRTUAL_TERMINAL_INPUT is intentionally not used to avoid a Windows bug where SGR-encoded mouse events are misinterpreted as keyboard input.

On POSIX systems (Linux, macOS), the terminal is set to raw mode and a single pthread reads from stdin. Mouse events are parsed from SGR escape sequences. The terminal mode is restored when input.stop() is called.

Usage Pattern

from tuix.core import engine, input

input.listen()
while running:
    engine.main_loop()
input.stop()
Manual Compatibility PathIf you need explicit control, manual snapshot feeding APIs still exist (objects.tuix_choice_feed_input and objects.tuix_input_feed_input), but they are no longer required for normal v0.3 apps.
Focus Routing (v0.3)Use scenes.set_focus(scene_name, uid) to choose which interactive widget receives keyboard focus, and scenes.set_previous_focus(scene_name) to return to the prior target.