Struct keyboard_types::ShortcutMatcher
source · pub struct ShortcutMatcher<T> {
state: KeyState,
key: Key,
modifiers: Modifiers,
matched: bool,
value: Option<T>,
}
Expand description
Match keyboard shortcuts and excute actions.
Every shortcut consists of a list of modifier keys pressed and a single non-modifier key pressed.
The Control + C shortcut requires the user to hold down the Control modifier key. When the C key is pressed the action (usually copy) is triggered. The event is consumed so other matchers don’t also act on the shortcut. This is also true for the release of the C key as else only key release events would be forwarded.
ASCII letters are compared ignoring case. Only takes the shift, control, alt and meta modifiers into account. If other modifiers beside those expected are found the shortcut is not matched.
Fields§
§state: KeyState
§key: Key
§modifiers: Modifiers
§matched: bool
§value: Option<T>
Implementations§
source§impl<T> ShortcutMatcher<T>
impl<T> ShortcutMatcher<T>
sourcepub fn new(
state: KeyState,
key: Key,
modifiers: Modifiers,
) -> ShortcutMatcher<T>
pub fn new( state: KeyState, key: Key, modifiers: Modifiers, ) -> ShortcutMatcher<T>
Create a new shortcut matcher.
sourcepub fn from_event(key_event: KeyboardEvent) -> ShortcutMatcher<T>
pub fn from_event(key_event: KeyboardEvent) -> ShortcutMatcher<T>
Create a new matcher from an event.
Only state, key and modifiers are stored. The other attributes are discarded.
sourcepub fn shortcut<K, F>(
self,
modifiers: Modifiers,
key: K,
f: F,
) -> ShortcutMatcher<T>
pub fn shortcut<K, F>( self, modifiers: Modifiers, key: K, f: F, ) -> ShortcutMatcher<T>
Test a keyboard shortcut.
If the modifiers are active and the key is pressed execute the provided function.
// Create a matcher from a keyboard event.
// Shortcuts are tested in-order.
ShortcutMatcher::from_event(event)
// Do something if the Tab key is pressed.
.shortcut(Modifiers::empty(), Key::Tab, do_something)
// If Shift + Tab are pressed do something.
// This is executed because the previous shortcut requires modifiers to be empty.
.shortcut(Modifiers::SHIFT, Key::Tab, do_something)
// Instead of named keys letters and other characters can be used.
.shortcut(Modifiers::CONTROL, 'L', do_something)
// Multiple modifiers are combined with bitwise OR (`|`) to form a new mask.
.shortcut(Modifiers::CONTROL | Modifiers::SHIFT, 'X', do_something)
// If none of the previous shortcuts matched forward the event.
.otherwise(forward_event);
sourcepub fn optional_shortcut<K, F>(
self,
enabled: bool,
modifiers: Modifiers,
key: K,
f: F,
) -> ShortcutMatcher<T>
pub fn optional_shortcut<K, F>( self, enabled: bool, modifiers: Modifiers, key: K, f: F, ) -> ShortcutMatcher<T>
Only test a shortcut if the enabled flag is set.
If the enabled
flag is true behaves the same as
shortcut
otherwise does nothing.
This is especially useful for platform specific shortcuts.
ShortcutMatcher::from_event(event)
.shortcut(Modifiers::CONTROL, 'c', copy)
.optional_shortcut(cfg!(target_os="macos"), Modifiers::META, 'q', quit)
.shortcut(Modifiers::CONTROL, 'w', quit);
In the example the app supports the copy action on all platforms and can be closed with Control + W everywhere but additionally with Command + Q on Mac OS.