Skip to main content

Module event

Module event 

Source
Expand description

Terminal events and event-stream decoding.

This module owns the core Event enum together with the internal decoder that parses raw terminal bytes into events, the platform-specific EventSource that drives the decoder from a tty, and the key/mouse types that events carry.

§The decode pipeline

Input arrives as a raw byte stream. The source reads bytes (waking on a self-pipe so another thread can interrupt a blocking read), feeds them to the decoder, and hands back fully-formed Event values. Escape sequences may straddle reads, so the decoder buffers partial input and a short timeout disambiguates a lone Esc key from the start of a CSI/SS3 sequence.

  tty input        EventSource              Decoder            caller
  ─────────        ───────────              ───────            ──────
  bytes  ───────▶  read + buffer  ───────▶  scan sequences ─▶  Event
    │                   ▲                        │
    │                   └── Esc-timeout ◀────────┘ (Esc key vs CSI/SS3?)
    └── self-pipe wake ─┘  (interrupt a blocking read from another thread)

Build an EventSource over a terminal’s input half and read typed events in a loop. Keys parse from strings and compare by canonical chord, so matching a shortcut is plain equality.

use uncurses::event::{Event, EventSource, Key};
use uncurses::terminal::Terminal;

let mut term = Terminal::stdio();
term.make_raw()?;
let mut events = EventSource::new(term.input())?;

let quit: Key = "ctrl+c".parse().unwrap();
loop {
    match events.read()? {
        Event::KeyPress(ref k) if *k == quit => break,
        Event::KeyPress(k) => { let _ = k.code; }
        Event::Resize(ws) => { let _ = (ws.col, ws.row); }
        _ => {}
    }
}
term.restore()

§Queries

To ask the terminal a question (its background color, cell size, device attributes, and so on), write the request bytes from the ansi module to the output and read the matching reply event back through the same source. The Screen facade wraps this in request_* methods whose replies surface as ordinary events, never swallowing the user’s keystrokes in between.

§Async

With the async feature, EventStream reads the same events through a futures_core::Stream, so the loop becomes while let Some(ev) = stream.next().await.

Structs§

EventSource
Wakeable event source backed by a platform readiness primitive.
EventStream
A thread-backed [futures_core::Stream] of io::Result<Event> over a shared EventSource.
Key
A key event.
KeyModifiers
Keyboard modifier flags.
Mouse
Mouse-event payload with position, button, and modifier state.
Waker
Cloneable handle that interrupts an in-progress EventSource::poll or EventSource::read.

Enums§

ClipboardSelection
Which system clipboard selection an OSC 52 event refers to.
ColorScheme
Reported terminal color scheme (DEC mode 2031).
Event
A terminal event.
KeyCode
Logical identity of a key, before modifiers are considered.
ModifyOtherKeysMode
Decoded modifyOtherKeys mode (CSI > 4 ; n m).
MouseButton
Mouse button or wheel direction associated with a Mouse event.
ParseKeyError
Error produced when parsing a Key or KeyCode from a binding string.

Constants§

DEFAULT_ESC_TIMEOUT
Default escape-sequence timeout.
DEFAULT_PASTE_IDLE_TIMEOUT
Default bracketed-paste idle timeout.

Traits§

Input
Platform-specific capabilities required from a Unix event input handle.

Functions§

mouse_pixel_to_cell
Convert an SGR-Pixel mouse payload to cell coordinates.