On key

on_key(f) gets the keys pressed while no text field is focused: arrows and Enter from a keyboard, a TV remote or a gamepad's D-pad. Handlers run topmost first; call cx.stop() for a key you used. Text fields take Enter through on_submit.

use dfui::widgets::{Styled, column, on_key, stateful, text};
use dfui::{Element, Key};

fn view() -> Element {
    stateful(|&n: &i32, h| {
        column([
            on_key(move |cx, k| {
                let step = match k {
                    Key::Up => -1,
                    Key::Down => 1,
                    _ => return,
                };
                cx.update(h, move |n: &mut i32| *n += step);
                cx.stop();
            })
            .into(),
            text(format!("Row {n}. Press Up or Down.")).into(),
        ])
    })
    .padding(16.0)
    .into()
}

fn main() {
    dfui::run(view);
}

Run it natively: cargo run -p dfui-site --example on_key