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

Helper macro for creating key handlers.

Details

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

In addition to the above, this macro also:

  • implicitly introduces the |event| closure parameter
  • adds the catch-all _ => () case to the match expression

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.update(|text| text.push(c)),
  KeyEvent { code: Char(c), .. } => text.update(|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.update(|text| text.push(c)),
      KeyEvent { code: Char(c), .. } => text.update(|text| text.pop()),
      _ => (),
    };
  };
};