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.


state: KeyStatekey: Keymodifiers: Modifiersmatched: boolvalue: Option<T>


Create a new shortcut matcher.

Create a new matcher from an event.

Only state, key and modifiers are stored. The other attributes are discarded.

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.
// 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.

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.

.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.

Execute the function is no keyboard shortcut matched.

Note that the passed function is exectued on both keydown and keyup unlike the shortcuts which only run on keydown.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.