Macro intuitive::on_key

source ·
on_key!() { /* proc-macro */ }
Expand description

Helper macro for creating key handlers.

Details

This macro is used to simplify a common pattern constructing a event::KeyHandler where:

In addition to the above, this macro also:

Usage

An example usage looks like the following:

let text = use_state(String::new);

let on_key = on_key! { [text]
  KeyEvent { code: Char(c), .. } => text.mutate(|text| text.push(c)),
  KeyEvent { code: Char(c), .. } => text.mutate(|text| text.pop()),
};

and expands to the following:

let text = use_state(String::new);

let on_key = {
  let text = text.clone();

  move |event| {
    use intuitive::event::{self, KeyEvent, KeyCode::*};

    match event {
      KeyEvent { code: Char(c), .. } => text.mutate(|text| text.push(c)),
      KeyEvent { code: Char(c), .. } => text.mutate(|text| text.pop()),
      _ => (),
    };
  };
};

Helper macro for creating key handlers.

See the documentation in the intuitive crate for details.