Repository navigation
Input and Events
Read events with lt_poll_event (blocking) or lt_peek_event(ev, timeout_ms). Both fill a struct lt_event and return LT_OK, LT_ERR_NO_EVENT when nothing is available, or a real error (LT_ERR_READ, LT_ERR_POLL) on genuine I/O failure — failures are never masked as "no event".
struct lt_event {
uint8_t type; // LT_EVENT_KEY | LT_EVENT_RESIZE | LT_EVENT_MOUSE
uint8_t mod; // OR of LT_MOD_* modifiers
uint16_t key; // a key code (named key, or mouse button); 0 if a character
lt_uchar ch; // Unicode codepoint for a text key; 0 if a named key
int32_t w, h; // new size, for LT_EVENT_RESIZE
int32_t x, y; // cell coordinates, for LT_EVENT_MOUSE
uint8_t action; // LT_KEY_PRESS | LT_KEY_REPEAT | LT_KEY_RELEASE
};Which fields are meaningful depends on type:
type |
meaningful fields |
|---|---|
LT_EVENT_KEY |
key or ch (one is set, the other 0), mod, action
|
LT_EVENT_MOUSE |
key (button), x, y, mod
|
LT_EVENT_RESIZE |
w, h
|
For a key event, a printable character arrives in ch (and key == 0); a named key arrives in key (and ch == 0).
action is LT_KEY_PRESS (1), LT_KEY_REPEAT (2), or LT_KEY_RELEASE (3). Every key event sets it; it is LT_KEY_PRESS except when a kitty-capable terminal (POSIX) or the Win32 console (Windows) reports repeat/release (see Input modes and Windows input model). Release events are suppressed by default — a caller that ignores the field sees each keystroke once (presses and auto-repeats). OR LT_INPUT_RELEASE into the input mode to receive them.
Named keys (high-range codes): LT_KEY_F1…LT_KEY_F12, LT_KEY_INSERT, LT_KEY_DELETE, LT_KEY_HOME, LT_KEY_END, LT_KEY_PGUP, LT_KEY_PGDN, LT_KEY_ARROW_UP/DOWN/LEFT/RIGHT, LT_KEY_BACK_TAB, and LT_KEY_ENTER/LT_KEY_TAB/LT_KEY_BACKSPACE/LT_KEY_BACKSPACE2/LT_KEY_ESC/LT_KEY_SPACE. These resolve consistently across terminals and encodings (legacy control byte or kitty CSI-u).
Bare-modifier keys (modern model only — emitted when a modifier is pressed on its own, on kitty-capable terminals or on Windows): LT_KEY_LEFT_SHIFT, LT_KEY_RIGHT_SHIFT, LT_KEY_LEFT_CTRL, LT_KEY_RIGHT_CTRL, LT_KEY_LEFT_ALT, LT_KEY_RIGHT_ALT, LT_KEY_LEFT_SUPER, LT_KEY_RIGHT_SUPER. Reported with ch == 0.
LT_KEY_CAPS_LOCK (modern model only) is reported when Caps Lock is pressed on a kitty-capable terminal or on Windows (ch == 0); legacy POSIX terminals send no event for it.
Ctrl+letter depends on the model (see Input modes): the modern default reports it as the letter in ch with LT_MOD_CTRL (e.g. Ctrl+A → ch='a', mod=LT_MOD_CTRL); compat mode (LT_INPUT_COMPAT) reports the termbox2 control-byte code in key — LT_KEY_CTRL_A…LT_KEY_CTRL_Z plus the symbol variants, some sharing a value with named keys (CTRL_I == TAB, CTRL_M == ENTER, CTRL_[ == ESC, CTRL_H == BACKSPACE). On Windows the modern-model disambiguation is best-effort — see Windows input model.
mod is an OR of:
| Flag | Meaning |
|---|---|
LT_MOD_ALT |
Alt held |
LT_MOD_CTRL |
Ctrl held |
LT_MOD_SHIFT |
Shift held |
LT_MOD_MOTION |
mouse drag (motion) — mouse events only |
How many keys carry modifiers depends on the platform. On a kitty-capable terminal (the modern model negotiates the protocol automatically) or on Windows (the console reports a control-key state) Shift/Alt/Ctrl arrive on every key, including Shift+letter — and ch carries the layout-translated character the keystroke typed (Shift+j → ch='J' + LT_MOD_SHIFT; Caps Lock likewise), the same on every platform. On a legacy POSIX terminal only CSI-encoded keys (arrows, F-keys, nav) carry modifier suffixes; letters can't (the terminal folds Shift into an uppercase character and Ctrl into a control byte). LT_MOD_MOTION is mouse-only.
lt_set_input_mode(mode) takes an OR of LT_INPUT_* flags and returns the resulting mode. LT_INPUT_CURRENT queries without changing.
libterm defaults to the modern model. On POSIX, lt_init negotiates the kitty keyboard protocol (best-effort, non-blocking): on a capable terminal you then get modifiers on every key, the typed character in ch (layout-translated: Shift+j → 'J'), bare-modifier keys, and action = press/repeat (releases only with LT_INPUT_RELEASE); on any other terminal it silently falls back to broad legacy decoding. On Windows there is no protocol to negotiate — the Win32 console supplies the same rich form directly (see Windows input model). Control bytes are normalized into the rich form (Ctrl+A → ch='a' + LT_MOD_CTRL), so events are as uniform as the wire allows.
| Flag | Effect |
|---|---|
| (default — no flag) | Modern model: kitty negotiated, modifiers on all keys, normalized control bytes, action reported. |
LT_INPUT_COMPAT |
Legacy termbox2 semantics: standalone control bytes reported in key (Ctrl+A → key=0x01), Shift+letter as an uppercase ch with no modifier, kitty not negotiated. Use for drop-in termbox2 code — see Porting from termbox2. |
LT_INPUT_MOUSE |
Enable mouse reporting (POSIX: SGR 1006 handshake; Windows: console mouse input) — orthogonal, OR it with either model. |
LT_INPUT_RELEASE |
Deliver key-release events (action == LT_KEY_RELEASE). Off by default: a caller that switches on key/ch without checking action would otherwise handle every keystroke twice on terminals that report releases. Orthogonal — OR it in to receive them. |
LT_INPUT_ESC / LT_INPUT_ALT
|
Only meaningful within LT_INPUT_COMPAT: ESC (the compat default) reports a lone ESC and an Alt-combo as two events; ALT folds an Alt-combo into one event with LT_MOD_ALT. In the modern model Alt is always a mod bit, so these are moot. |
The inherent ceiling: on a legacy POSIX terminal Shift+letter, bare modifiers, and key-release simply aren't on the wire — libterm can't recover what the terminal never sent. The modern model gets full fidelity on kitty-capable terminals (and on Windows) and degrades gracefully elsewhere.
On Windows libterm reads the console directly with ReadConsoleInputW — there is no kitty protocol and no escape-sequence decoding, but the same lt_event surface is produced as on a kitty-capable POSIX terminal:
-
LT_MOD_CTRL/LT_MOD_ALT/LT_MOD_SHIFTarrive on every key, from the console's control-key state. - Bare-modifier keys (
LT_KEY_LEFT_SHIFT…LT_KEY_RIGHT_SUPER) andLT_KEY_CAPS_LOCKare reported when a modifier or lock is pressed on its own. -
ev.actionreportsLT_KEY_PRESS/LT_KEY_REPEAT/LT_KEY_RELEASE(key-up and auto-repeat). As on POSIX, releases are delivered only withLT_INPUT_RELEASE. -
LT_KEY_BACK_TABfor Shift+Tab; mouse and resize events as on POSIX.
Ctrl+letter is disambiguated best-effort. The modern model reconstructs the letter from the console virtual-key — so Ctrl+I → ch='i' + LT_MOD_CTRL, distinct from Tab — when the console reports the letter key. The classic console (conhost) does. ConPTY / Windows Terminal pre-collapse Ctrl+I→Tab and Ctrl+J→Enter at the virtual-key level, where the letter is already gone before libterm sees it and cannot be recovered — the same limitation as a legacy POSIX terminal. LT_INPUT_COMPAT keeps the termbox2 control-byte form (Ctrl+A → key=0x01) everywhere.
lt_get_fds is POSIX-only (console handles aren't pollable fds); on Windows it returns LT_ERR_UNSUPPORTED_TERM, so the own-event-loop pattern below does not apply — block in lt_poll_event / lt_peek_event instead.
After lt_set_input_mode(LT_INPUT_MOUSE), clicks, drags, and wheel motion arrive as LT_EVENT_MOUSE. The button is in key:
LT_KEY_MOUSE_LEFT, LT_KEY_MOUSE_RIGHT, LT_KEY_MOUSE_MIDDLE, LT_KEY_MOUSE_RELEASE, LT_KEY_MOUSE_WHEEL_UP, LT_KEY_MOUSE_WHEEL_DOWN.
x / y are 0-based cell coordinates; mod carries Shift/Alt/Ctrl and LT_MOD_MOTION for drags.
lt_set_input_mode(LT_INPUT_MOUSE);
struct lt_event ev;
while (lt_poll_event(&ev) == LT_OK) {
if (ev.type == LT_EVENT_MOUSE && ev.key == LT_KEY_MOUSE_LEFT)
lt_printf(0, 0, LT_DEFAULT, LT_DEFAULT, "click at %d,%d ", ev.x, ev.y);
else if (ev.type == LT_EVENT_KEY && ev.ch == 'q')
break;
lt_present();
}A terminal resize (POSIX SIGWINCH; Windows console buffer event) is delivered as an LT_EVENT_RESIZE with the new w/h. libterm has already resized its cell buffers by the time you receive it; redraw and present.
if (ev.type == LT_EVENT_RESIZE) {
lt_clear();
/* redraw using lt_width() / lt_height() */
lt_present();
}Instead of blocking in lt_poll_event, fetch the fds and select on them yourself:
int tty, resize;
lt_get_fds(&tty, &resize);
/* add tty and resize to your fd_set; when either is readable: */
struct lt_event ev;
while (lt_peek_event(&ev, 0) == LT_OK) {
/* handle ev */
}