Struct keyboard_types::ShortcutMatcher[][src]

pub struct ShortcutMatcher<T> { /* fields omitted */ }

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.

Implementations

impl<T> ShortcutMatcher<T>[src]

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

Create a new shortcut matcher.

pub fn from_event(key_event: KeyboardEvent) -> ShortcutMatcher<T>[src]

Create a new matcher from an event.

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

pub fn shortcut<K, F>(
    self,
    modifiers: Modifiers,
    key: K,
    f: F
) -> ShortcutMatcher<T> where
    K: MatchKey,
    F: FnOnce() -> T, 
[src]

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);

pub fn optional_shortcut<K, F>(
    self,
    enabled: bool,
    modifiers: Modifiers,
    key: K,
    f: F
) -> ShortcutMatcher<T> where
    K: MatchKey,
    F: FnOnce() -> T, 
[src]

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.

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

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.