macro_rules! ctrl {
(@code $($tokens:tt)*) => { ... };
(@modif [$($mods:tt),*] $($tokens:tt)*) => { ... };
(@bindings [$($mods:tt),*] $($tokens:tt)*) => { ... };
($($tokens:tt)*) => { ... };
}Expand description
Macro for pattern matching KeyEvents with KeyMod::CONTROL
This macro essentially turns this:
match key_event {
ctrl!(KeyCode::Backspace) => {
//...
}
_ => {
//...
}
}Into this:
match key_event {
KeyEvent {
code: KeyCode::Backspace,
modifiers: KeyMod::CONTROL,
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
} => {
//...
}
_ => {
//...
}
}This is very useful for Modes, which require matching on a
large number of different KeyEvent patterns in order to decide
what to do.
You can also use this with more complex patterns:
match key_event {
ctrl!(alt!(KeyCode::Char('a' | 'b' | 'c') | KeyCode::BackTab)) => {
//...
}
_ => {
//...
}
}For the other two modifiers with this convenience, see alt!
and shift!. There is also event!, which has no modifiers,
and it cannot be nested with the other ones, for obvious
reasons.