Events
In raw mode the terminal hands you a bare byte
stream. A keypress might be one byte (a), the click of a mouse might be a
dozen, and an arrow key arrives as a little escape sequence. uncurses turns that
stream into typed events so you match on KeyPress instead of decoding bytes
by hand. Because uncurses raw mode disables ISIG, keys like Ctrl-C and Ctrl-Z
arrive as key events instead of signals.
From bytes to events
The path from a keystroke to something you can match on has three stops.
flowchart TB
accTitle: From input bytes to events
accDescr {
Input bytes pass from the tty through the decoder.
The decoder returns complete events to the application event loop.
}
bytes["Raw bytes from the tty"] --> decoder["Decoder: parse escape sequences"]
decoder --> event["Event: KeyPress, MouseClick, Resize, ..."]
event --> loop["Your event loop"]Escape sequences can straddle reads, so the decoder buffers partial input and returns whole events. You never see the half-parsed middle.
What an event can be
Events cover everything the terminal reports, not just keys:
- Input:
KeyPress,KeyRelease, and the mouse family (MouseClick,MouseRelease,MouseWheel,MouseMove). - Desktop exchanges: drag and drop messages carry offered formats and data.
See
Event::DragDrop. - Glyph registration: Glyph Protocol replies report support, coverage, and
registration results. See
Event::Glyph. - Lifecycle:
Resizewhen the window changes,FocusInandFocusOut,Visibilitywhen the view becomes observable or hidden, and bracketed-paste events:PasteStart,PasteChunk, andPasteEnd. - Replies: answers to questions you asked the terminal, like
CursorPosition,BackgroundColor,PrimaryDeviceAttributes, orColorScheme. A selected probe writes queries through a writer. Their replies arrive on the same stream as user input. See Querying the terminal. - Unknown: anything the decoder recognizes the shape of but not the meaning,
handed back as an
Unknownevent with its sequence kind and bytes. Long unknown strings, such as a large OSC reply, stream throughStringStart,StringChunk, andStringEnd.
KeyPress carries both presses and repeats. Its Key::repeat field identifies
repeats reported by the terminal. See keyboard input for repeat filters and release reports.
Key::code identifies the logical key. Its public location field distinguishes
standard, left, right, and keypad positions when the input identifies them.
See key locations
for bindings and examples.
Reading through Program
Most apps read events from Program, because it
owns the terminal session and the event source.
use uncurses::event::{Event, KeyCode};
use uncurses::program::Program;
fn main() -> std::io::Result<()> {
let mut program = Program::stdio()?;
program.init()?;
loop {
match program.read_event()? {
Event::KeyPress(key) if key.code == KeyCode::Char('q') => break,
Event::KeyPress(key) => println!("pressed {:?}", key.code),
_ => {}
}
}
program.finish()
}read_event() blocks until the next event. poll_event(timeout) waits up to a
timeout and reports whether something is ready, so you can interleave events
with timers or other work. try_read_event() takes an already-decoded event
without reading more input.
Reads return each event without changes. Replies to your requests, such as a pixel size or a cursor position, arrive as ordinary events, and your loop keeps the values it needs. Capability replies go to a record that you own; see Capabilities.
Cursor and Primary DA replies carry no request ID. Keep one such request in flight at a time, so the next reply of that kind belongs to it. The querying guide shows the pattern.
The lower-level event source
An EventSource wraps an input handle and owns the decoder. Use it directly
when you are managing the terminal yourself, or when you need to share one
decoder with async code through Program::event_source().
Waking a blocked read
A read call blocks, which is a problem if another thread needs to stop the
loop. Every source can hand out a Waker: call it from anywhere, and the
blocked read returns early so your loop can notice a shutdown flag and exit
cleanly. No signals, no polling spin.
Async, when you want it
If you would rather await events than block a thread, use
Program::event_stream() behind the async feature. It returns a
futures_core::Stream over the program’s shared decoder, so events fit into a
select! alongside your other futures. It yields the same events as
read_event(). See the
EventStream guide for the full
async pattern.
Input is one half of an interactive program; drawing into a
surface is the other. The program’s
Screen is the pure renderer you paint, and the
program is where input and rendering meet.
