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
impl HotKey
Sourcepub fn new(mods: impl Into<Option<RawMods>>, key: impl IntoKey) -> Self
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
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}
Trait Implementations§
impl Eq for HotKey
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
Source§fn round_into(self) -> U
fn round_into(self) -> U
Performs the conversion.