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>

source

pub fn new( state: KeyState, key: Key, modifiers: Modifiers, ) -> ShortcutMatcher<T>

Create a new shortcut matcher.

source

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.

source

pub fn shortcut<K, F>( self, modifiers: Modifiers, key: K, f: F, ) -> ShortcutMatcher<T>
where K: MatchKey, F: FnOnce() -> 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);
source

pub fn optional_shortcut<K, F>( self, enabled: bool, modifiers: Modifiers, key: K, f: F, ) -> ShortcutMatcher<T>
where K: MatchKey, F: FnOnce() -> 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.

source

pub fn otherwise<F>(self, f: F) -> Option<T>
where F: FnOnce() -> T,

Execute the function is no keyboard shortcut matched.

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

Auto Trait Implementations§

§

impl<T> Freeze for ShortcutMatcher<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ShortcutMatcher<T>
where T: RefUnwindSafe,

§

impl<T> Send for ShortcutMatcher<T>
where T: Send,

§

impl<T> Sync for ShortcutMatcher<T>
where T: Sync,

§

impl<T> Unpin for ShortcutMatcher<T>
where T: Unpin,

§

impl<T> UnwindSafe for ShortcutMatcher<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.