Struct HotKey

Source
pub struct HotKey { /* private fields */ }
Expand description

A description of a keyboard shortcut.

This type is only intended to be used to describe shortcuts, and recognize them when they arrive.

§Examples

SysMods matches the Command key on macOS and Ctrl elsewhere:

use druid_shell::{HotKey, KbKey, KeyEvent, RawMods, SysMods};

let hotkey = HotKey::new(SysMods::Cmd, "a");

#[cfg(target_os = "macos")]
assert!(hotkey.matches(KeyEvent::for_test(RawMods::Meta, "a")));

#[cfg(target_os = "windows")]
assert!(hotkey.matches(KeyEvent::for_test(RawMods::Ctrl, "a")));

None matches only the key without modifiers:

use druid_shell::{HotKey, KbKey, KeyEvent, RawMods, SysMods};

let hotkey = HotKey::new(None, KbKey::ArrowLeft);

assert!(hotkey.matches(KeyEvent::for_test(RawMods::None, KbKey::ArrowLeft)));
assert!(!hotkey.matches(KeyEvent::for_test(RawMods::Ctrl, KbKey::ArrowLeft)));

Implementations§

Source§

impl HotKey

Source

pub fn new(mods: impl Into<Option<RawMods>>, key: impl IntoKey) -> Self

Create a new hotkey.

The first argument describes the keyboard modifiers. This can be None, or an instance of either SysMods, or RawMods. SysMods unify the ‘Command’ key on macOS with the ‘Ctrl’ key on other platforms.

The second argument describes the non-modifier key. This can be either a &str or a KbKey; the former is merely a convenient shorthand for KbKey::Character().

§Examples
use druid_shell::{HotKey, KbKey, RawMods, SysMods};

let select_all = HotKey::new(SysMods::Cmd, "a");
let esc = HotKey::new(None, KbKey::Escape);
let macos_fullscreen = HotKey::new(RawMods::CtrlMeta, "f");
Examples found in repository?
examples/quit.rs (line 77)
69fn main() {
70    tracing_subscriber::fmt().init();
71    let app = Application::new().unwrap();
72
73    let mut file_menu = Menu::new();
74    file_menu.add_item(
75        0x100,
76        "E&xit",
77        Some(&HotKey::new(SysMods::Cmd, "q")),
78        None,
79        true,
80    );
81
82    let mut menubar = Menu::new();
83    menubar.add_dropdown(file_menu, "Application", true);
84
85    let mut builder = WindowBuilder::new(app.clone());
86    builder.set_handler(Box::<QuitState>::default());
87    builder.set_title("Quit example");
88    builder.set_menu(menubar);
89
90    let window = builder.build().unwrap();
91    window.show();
92
93    app.run(None);
94}
More examples
Hide additional examples
examples/shello.rs (line 142)
136fn main() {
137    tracing_subscriber::fmt().init();
138    let mut file_menu = Menu::new();
139    file_menu.add_item(
140        0x100,
141        "E&xit",
142        Some(&HotKey::new(SysMods::Cmd, "q")),
143        None,
144        true,
145    );
146    file_menu.add_item(
147        0x101,
148        "O&pen",
149        Some(&HotKey::new(SysMods::Cmd, "o")),
150        None,
151        true,
152    );
153    file_menu.add_item(
154        0x102,
155        "S&ave",
156        Some(&HotKey::new(SysMods::Cmd, "s")),
157        None,
158        true,
159    );
160    let mut menubar = Menu::new();
161    menubar.add_dropdown(Menu::new(), "Application", true);
162    menubar.add_dropdown(file_menu, "&File", true);
163
164    let app = Application::new().unwrap();
165    let mut builder = WindowBuilder::new(app.clone());
166    builder.set_handler(Box::<HelloState>::default());
167    builder.set_title("Hello example");
168    builder.set_menu(menubar);
169
170    let window = builder.build().unwrap();
171    window.show();
172
173    app.run(None);
174}
Source

pub fn matches(&self, event: impl Borrow<KeyEvent>) -> bool

Returns true if this KeyEvent matches this HotKey.

Trait Implementations§

Source§

impl Clone for HotKey

Source§

fn clone(&self) -> HotKey

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HotKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for HotKey

Source§

fn eq(&self, other: &HotKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for HotKey

Source§

impl StructuralPartialEq for HotKey

Auto Trait Implementations§

§

impl Freeze for HotKey

§

impl RefUnwindSafe for HotKey

§

impl Send for HotKey

§

impl Sync for HotKey

§

impl Unpin for HotKey

§

impl UnwindSafe for HotKey

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> RoundFrom<T> for T

Source§

fn round_from(x: T) -> T

Performs the conversion.
Source§

impl<T, U> RoundInto<U> for T
where U: RoundFrom<T>,

Source§

fn round_into(self) -> U

Performs the conversion.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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

Source§

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more