macro_rules! shift {
(@code $($tokens:tt)*) => { ... };
(@modif [$($mods:tt),*] $($tokens:tt)*) => { ... };
(@bindings [$($mods:tt),*] $($tokens:tt)*) => { ... };
($($tokens:tt)*) => { ... };
}Expand description
Macro for pattern matching KeyEvents with KeyMod::SHIFT
This macro essentially turns this:
match key_event {
shift!(KeyCode::Enter) => {
//...
}
_ => {
//...
}
}Into this:
match key_event {
KeyEvent {
code: KeyCode::Enter,
modifiers: KeyMod::SHIFT,
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!(shift!(KeyCode::PageDown | KeyCode::PageUp)) => {
//...
}
_ => {
//...
}
}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.
§A note on shift! specifically
Unlike ctrl! and alt!, the KeyMod::SHIFT modifier is
not always emitted, since some keys, like 'B' can automatically
be inferred to be shifted, so the shift modifier is not included
with the KeyEvent. Given that, for now, you should avoid using
this macro with KeyCode::Char, since it will probably never
match.
In the future, I might try to rectify this, by detecting when a
key is “supposed” to be shifted, and then manually sending a
modified KeyEvent, which includes the shift modifier, so
pattern matching can be more reliable.