pub trait UseTerminalEvents: Sealed {
// Required methods
fn use_terminal_events<F>(&mut self, f: F)
where F: FnMut(TerminalEvent) + Send + 'static;
fn use_local_terminal_events<F>(&mut self, f: F)
where F: FnMut(TerminalEvent) + Send + 'static;
}Expand description
UseTerminalEvents is a hook that allows you to listen for user input such as key strokes.
§Example
const AREA_WIDTH: u32 = 80;
const AREA_HEIGHT: u32 = 11;
const FACE: &str = "👾";
#[component]
fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
let mut system = hooks.use_context_mut::<SystemContext>();
let mut x = hooks.use_state(|| 0);
let mut y = hooks.use_state(|| 0);
let mut should_exit = hooks.use_state(|| false);
hooks.use_terminal_events({
move |event| match event {
TerminalEvent::Key(KeyEvent { code, kind, .. }) if kind != KeyEventKind::Release => {
match code {
KeyCode::Char('q') => should_exit.set(true),
KeyCode::Up => y.set((y.get() as i32 - 1).max(0) as _),
KeyCode::Down => y.set((y.get() + 1).min(AREA_HEIGHT - 1)),
KeyCode::Left => x.set((x.get() as i32 - 1).max(0) as _),
KeyCode::Right => x.set((x.get() + 1).min(AREA_WIDTH - FACE.width() as u32)),
_ => {}
}
}
_ => {}
}
});
if should_exit.get() {
system.exit();
}
element! {
View(
flex_direction: FlexDirection::Column,
padding: 2,
align_items: AlignItems::Center
) {
Text(content: "Use arrow keys to move. Press \"q\" to exit.")
View(
border_style: BorderStyle::Round,
border_color: Color::Green,
height: AREA_HEIGHT + 2,
width: AREA_WIDTH + 2,
) {
#(if should_exit.get() {
element! {
View(
width: 100pct,
height: 100pct,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
) {
Text(content: format!("Goodbye! {}", FACE))
}
}
} else {
element! {
View(
padding_left: x.get(),
padding_top: y.get(),
) {
Text(content: FACE)
}
}
})
}
}
}
}Required Methods§
Sourcefn use_terminal_events<F>(&mut self, f: F)
fn use_terminal_events<F>(&mut self, f: F)
Defines a callback to be invoked whenever a terminal event occurs.
This hook will be called for all terminal events, including those that occur outside of the
component. If you only want to listen for events within the component, use
Self::use_local_terminal_events instead.
Sourcefn use_local_terminal_events<F>(&mut self, f: F)
fn use_local_terminal_events<F>(&mut self, f: F)
Defines a callback to be invoked whenever a terminal event occurs within a component.
Unlike Self::use_terminal_events, this hook will not be called for events such as mouse
events that occur outside of the component. Furthermore, coordinates will be translated to
component-local coordinates.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.