pub struct HotkeyManager<T> { /* private fields */ }Expand description
Manages the lifecycle of hotkeys, including their registration, unregistration, and execution.
The HotkeyManager listens for keyboard events and triggers the corresponding
hotkey callbacks when events match registered
hotkeys.
§Type Parameters
T: The return type of the hotkey callbacks.
Implementations§
Source§impl<T> HotkeyManager<T>
impl<T> HotkeyManager<T>
Sourcepub fn new() -> HotkeyManager<T>
pub fn new() -> HotkeyManager<T>
Examples found in repository?
8fn main() {
9 let mut hkm = HotkeyManager::new();
10
11 hkm.register_hotkey(VKey::P, &[VKey::Control], || {
12 show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
13 thread::spawn(|| {
14 thread::sleep(time::Duration::from_secs(25 * 60));
15 show_popup("Pomodoro Timer", "Time's up! Take a break.");
16 });
17 })
18 .unwrap();
19
20 hkm.register_hotkey(VKey::S, &[VKey::Control], || {
21 show_popup("Pomodoro Timer", "Pomodoro stopped!");
22 })
23 .unwrap();
24
25 hkm.event_loop();
26}More examples
4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8 let trigger_key = VKey::A;
9 let mod_key = VKey::Control;
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey CTRL + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16 hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17 println!("Hotkey CTRL + V was pressed");
18 })
19 .unwrap();
20
21 // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22 // allowing only registered pause hotkeys to be processed.
23 let trigger_key = VKey::P;
24 let modifiers = &[VKey::Control, VKey::Shift];
25 hkm.register_pause_hotkey(trigger_key, modifiers, || {
26 println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27 })
28 .unwrap();
29
30 hkm.event_loop();
31}5fn main() {
6 // Create HotkeyManager
7 let mut hkm = HotkeyManager::<()>::new();
8
9 let vk_a1 = VKey::A;
10 let vk_a2 = VKey::from_keyname("a").unwrap();
11 let vk_a3 = VKey::from_vk_code(0x41);
12 let vk_a4 = VKey::from_vk_code(VK_A.0);
13
14 // Create custom keycode equivalent to A
15 let vk_a5 = VKey::CustomKeyCode(0x41);
16
17 assert_eq!(vk_a1, vk_a2);
18 assert_eq!(vk_a1, vk_a3);
19 assert_eq!(vk_a1, vk_a4);
20 assert_eq!(vk_a1, vk_a5);
21
22 // NOTE
23 // When matching `CustomKeyCodes` you must include a guard if statement
24 match vk_a5 {
25 VKey::A => {
26 // This will never show up
27 println!("CustomKeyCode(0x41) matches against VKey::A");
28 }
29 _ => {
30 println!("You didn't use the match statement correctly!");
31 }
32 }
33
34 // Instead match like this
35 match vk_a5 {
36 _ if VKey::A == vk_a5 => {
37 // This will match
38 println!("CustomKeyCode(0x41) matches against VKey::A");
39 }
40 _ => {}
41 }
42
43 hkm.register_hotkey(vk_a5, &[], || {
44 println!("You pressed A");
45 })
46 .unwrap();
47
48 hkm.event_loop();
49}6fn main() {
7 // Create the manager
8 let mut hkm = HotkeyManager::new();
9
10 // Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
11 hkm.register_hotkey(VKey::A, &[VKey::Control], || {
12 println!("Hotkey CTRL + A was pressed");
13 })
14 .unwrap();
15
16 // Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
17 let interrupt_handle = hkm.interrupt_handle();
18
19 // Get a pause handle that can be used to programmatically pause the processing of hotkeys from
20 // any thread. Note: registered pause hotkeys will still be processed.
21 let pause_handle = hkm.pause_handle();
22
23 // Create a second thread that will pause/interrupt hkm
24 spawn(move || {
25 sleep(Duration::from_secs(3));
26
27 println!("Pausing hotkeys for 3 seconds");
28 pause_handle.toggle();
29 sleep(Duration::from_secs(3));
30
31 println!("Unpausing hotkeys");
32 pause_handle.toggle();
33
34 sleep(Duration::from_secs(3));
35 interrupt_handle.interrupt();
36 });
37
38 // Run the event handler in a blocking loop. This will block until interrupted and execute the
39 // set callbacks when registered hotkeys are detected
40 hkm.event_loop();
41
42 println!("Event Loop interrupted");
43}12fn main() {
13 // The HotkeyManager is generic over the return type of the callback functions.
14 let mut hkm = HotkeyManager::new();
15 let modifiers = &[VKey::LWin, VKey::Shift];
16
17 // Register WIN + SHIFT + 1 for app command 1
18 hkm.register_hotkey(VKey::Vk1, modifiers, || {
19 println!("Pressed WIN + SHIFT + 1");
20 AppCommand::AppCommand1
21 })
22 .unwrap();
23
24 // Register WIN + SHIFT + 2 for app command 2
25 hkm.register_hotkey(VKey::Vk2, modifiers, || {
26 println!("Pressed WIN + SHIFT + 2");
27 AppCommand::AppCommand2
28 })
29 .unwrap();
30
31 // Register WIN + 3 for app command EXIT
32 hkm.register_hotkey(VKey::Vk3, modifiers, || {
33 println!("Pressed WIN + SHIFT + 3");
34 AppCommand::Exit
35 })
36 .unwrap();
37
38 // Register channel to receive events from the hkm event loop
39 let (tx, rx) = unbounded();
40 hkm.register_channel(tx);
41
42 // Run HotkeyManager in background thread
43 let handle = hkm.interrupt_handle();
44 thread::spawn(move || {
45 hkm.event_loop();
46 });
47
48 // App Logic
49 loop {
50 let command = rx.recv().unwrap();
51
52 match command {
53 AppCommand::AppCommand1 => {
54 println!("Do thing 1");
55 }
56 AppCommand::AppCommand2 => {
57 println!("Do thing 2");
58 }
59 AppCommand::Exit => {
60 println!("Exiting...");
61 handle.interrupt();
62 break;
63 }
64 }
65 }
66}4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8 let trigger_key = VKey::from_keyname("a").unwrap();
9 let mod_key = VKey::from_keyname("alt").unwrap();
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey ALT + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16 let trigger_key = VKey::from_keyname("b").unwrap();
17 let modifiers = &[VKey::from_vk_code(0x87)];
18 hkm.register_hotkey(trigger_key, modifiers, || {
19 println!("Hotkey F24 + B was pressed");
20 })
21 .unwrap();
22
23 // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24 hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25 println!("Hotkey WIN + ALT + C was pressed");
26 })
27 .unwrap();
28
29 // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30 let hotkey_id = hkm
31 .register_hotkey(
32 VKey::from_vk_code(0x44),
33 &[VKey::from_vk_code(0xA4)],
34 || {
35 println!("Hotkey ALT + D was pressed");
36 },
37 )
38 .unwrap();
39
40 // Unregister hotkey with ID
41 hkm.unregister_hotkey(hotkey_id);
42
43 // Run the event handler in a blocking loop. This will block forever and execute the set
44 // callbacks when the registered hotkeys are detected
45 hkm.event_loop();
46}Sourcepub fn register_hotkey(
&mut self,
trigger_key: VKey,
mod_keys: &[VKey],
callback: impl Fn() -> T + Send + 'static,
) -> Result<i32, WHKError>
pub fn register_hotkey( &mut self, trigger_key: VKey, mod_keys: &[VKey], callback: impl Fn() -> T + Send + 'static, ) -> Result<i32, WHKError>
Registers a new hotkey.
Examples found in repository?
8fn main() {
9 let mut hkm = HotkeyManager::new();
10
11 hkm.register_hotkey(VKey::P, &[VKey::Control], || {
12 show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
13 thread::spawn(|| {
14 thread::sleep(time::Duration::from_secs(25 * 60));
15 show_popup("Pomodoro Timer", "Time's up! Take a break.");
16 });
17 })
18 .unwrap();
19
20 hkm.register_hotkey(VKey::S, &[VKey::Control], || {
21 show_popup("Pomodoro Timer", "Pomodoro stopped!");
22 })
23 .unwrap();
24
25 hkm.event_loop();
26}More examples
4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8 let trigger_key = VKey::A;
9 let mod_key = VKey::Control;
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey CTRL + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16 hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17 println!("Hotkey CTRL + V was pressed");
18 })
19 .unwrap();
20
21 // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22 // allowing only registered pause hotkeys to be processed.
23 let trigger_key = VKey::P;
24 let modifiers = &[VKey::Control, VKey::Shift];
25 hkm.register_pause_hotkey(trigger_key, modifiers, || {
26 println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27 })
28 .unwrap();
29
30 hkm.event_loop();
31}5fn main() {
6 // Create HotkeyManager
7 let mut hkm = HotkeyManager::<()>::new();
8
9 let vk_a1 = VKey::A;
10 let vk_a2 = VKey::from_keyname("a").unwrap();
11 let vk_a3 = VKey::from_vk_code(0x41);
12 let vk_a4 = VKey::from_vk_code(VK_A.0);
13
14 // Create custom keycode equivalent to A
15 let vk_a5 = VKey::CustomKeyCode(0x41);
16
17 assert_eq!(vk_a1, vk_a2);
18 assert_eq!(vk_a1, vk_a3);
19 assert_eq!(vk_a1, vk_a4);
20 assert_eq!(vk_a1, vk_a5);
21
22 // NOTE
23 // When matching `CustomKeyCodes` you must include a guard if statement
24 match vk_a5 {
25 VKey::A => {
26 // This will never show up
27 println!("CustomKeyCode(0x41) matches against VKey::A");
28 }
29 _ => {
30 println!("You didn't use the match statement correctly!");
31 }
32 }
33
34 // Instead match like this
35 match vk_a5 {
36 _ if VKey::A == vk_a5 => {
37 // This will match
38 println!("CustomKeyCode(0x41) matches against VKey::A");
39 }
40 _ => {}
41 }
42
43 hkm.register_hotkey(vk_a5, &[], || {
44 println!("You pressed A");
45 })
46 .unwrap();
47
48 hkm.event_loop();
49}6fn main() {
7 // Create the manager
8 let mut hkm = HotkeyManager::new();
9
10 // Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
11 hkm.register_hotkey(VKey::A, &[VKey::Control], || {
12 println!("Hotkey CTRL + A was pressed");
13 })
14 .unwrap();
15
16 // Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
17 let interrupt_handle = hkm.interrupt_handle();
18
19 // Get a pause handle that can be used to programmatically pause the processing of hotkeys from
20 // any thread. Note: registered pause hotkeys will still be processed.
21 let pause_handle = hkm.pause_handle();
22
23 // Create a second thread that will pause/interrupt hkm
24 spawn(move || {
25 sleep(Duration::from_secs(3));
26
27 println!("Pausing hotkeys for 3 seconds");
28 pause_handle.toggle();
29 sleep(Duration::from_secs(3));
30
31 println!("Unpausing hotkeys");
32 pause_handle.toggle();
33
34 sleep(Duration::from_secs(3));
35 interrupt_handle.interrupt();
36 });
37
38 // Run the event handler in a blocking loop. This will block until interrupted and execute the
39 // set callbacks when registered hotkeys are detected
40 hkm.event_loop();
41
42 println!("Event Loop interrupted");
43}12fn main() {
13 // The HotkeyManager is generic over the return type of the callback functions.
14 let mut hkm = HotkeyManager::new();
15 let modifiers = &[VKey::LWin, VKey::Shift];
16
17 // Register WIN + SHIFT + 1 for app command 1
18 hkm.register_hotkey(VKey::Vk1, modifiers, || {
19 println!("Pressed WIN + SHIFT + 1");
20 AppCommand::AppCommand1
21 })
22 .unwrap();
23
24 // Register WIN + SHIFT + 2 for app command 2
25 hkm.register_hotkey(VKey::Vk2, modifiers, || {
26 println!("Pressed WIN + SHIFT + 2");
27 AppCommand::AppCommand2
28 })
29 .unwrap();
30
31 // Register WIN + 3 for app command EXIT
32 hkm.register_hotkey(VKey::Vk3, modifiers, || {
33 println!("Pressed WIN + SHIFT + 3");
34 AppCommand::Exit
35 })
36 .unwrap();
37
38 // Register channel to receive events from the hkm event loop
39 let (tx, rx) = unbounded();
40 hkm.register_channel(tx);
41
42 // Run HotkeyManager in background thread
43 let handle = hkm.interrupt_handle();
44 thread::spawn(move || {
45 hkm.event_loop();
46 });
47
48 // App Logic
49 loop {
50 let command = rx.recv().unwrap();
51
52 match command {
53 AppCommand::AppCommand1 => {
54 println!("Do thing 1");
55 }
56 AppCommand::AppCommand2 => {
57 println!("Do thing 2");
58 }
59 AppCommand::Exit => {
60 println!("Exiting...");
61 handle.interrupt();
62 break;
63 }
64 }
65 }
66}4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8 let trigger_key = VKey::from_keyname("a").unwrap();
9 let mod_key = VKey::from_keyname("alt").unwrap();
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey ALT + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16 let trigger_key = VKey::from_keyname("b").unwrap();
17 let modifiers = &[VKey::from_vk_code(0x87)];
18 hkm.register_hotkey(trigger_key, modifiers, || {
19 println!("Hotkey F24 + B was pressed");
20 })
21 .unwrap();
22
23 // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24 hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25 println!("Hotkey WIN + ALT + C was pressed");
26 })
27 .unwrap();
28
29 // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30 let hotkey_id = hkm
31 .register_hotkey(
32 VKey::from_vk_code(0x44),
33 &[VKey::from_vk_code(0xA4)],
34 || {
35 println!("Hotkey ALT + D was pressed");
36 },
37 )
38 .unwrap();
39
40 // Unregister hotkey with ID
41 hkm.unregister_hotkey(hotkey_id);
42
43 // Run the event handler in a blocking loop. This will block forever and execute the set
44 // callbacks when the registered hotkeys are detected
45 hkm.event_loop();
46}Sourcepub fn unregister_hotkey(&mut self, hotkey_id: i32)
pub fn unregister_hotkey(&mut self, hotkey_id: i32)
Unregisters a hotkey by its unique id.
Examples found in repository?
4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8 let trigger_key = VKey::from_keyname("a").unwrap();
9 let mod_key = VKey::from_keyname("alt").unwrap();
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey ALT + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16 let trigger_key = VKey::from_keyname("b").unwrap();
17 let modifiers = &[VKey::from_vk_code(0x87)];
18 hkm.register_hotkey(trigger_key, modifiers, || {
19 println!("Hotkey F24 + B was pressed");
20 })
21 .unwrap();
22
23 // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24 hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25 println!("Hotkey WIN + ALT + C was pressed");
26 })
27 .unwrap();
28
29 // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30 let hotkey_id = hkm
31 .register_hotkey(
32 VKey::from_vk_code(0x44),
33 &[VKey::from_vk_code(0xA4)],
34 || {
35 println!("Hotkey ALT + D was pressed");
36 },
37 )
38 .unwrap();
39
40 // Unregister hotkey with ID
41 hkm.unregister_hotkey(hotkey_id);
42
43 // Run the event handler in a blocking loop. This will block forever and execute the set
44 // callbacks when the registered hotkeys are detected
45 hkm.event_loop();
46}Sourcepub fn unregister_all(&mut self)
pub fn unregister_all(&mut self)
Unregisters all hotkeys.
Sourcepub fn register_pause_hotkey(
&mut self,
trigger_key: VKey,
mod_keys: &[VKey],
callback: impl Fn() -> T + Send + 'static,
) -> Result<i32, WHKError>
pub fn register_pause_hotkey( &mut self, trigger_key: VKey, mod_keys: &[VKey], callback: impl Fn() -> T + Send + 'static, ) -> Result<i32, WHKError>
Registers a hotkey that will toggle the paused state of the
HotkeyManager. When paused, only registered pause hotkeys
will be allowed to trigger.
Examples found in repository?
4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8 let trigger_key = VKey::A;
9 let mod_key = VKey::Control;
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey CTRL + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16 hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17 println!("Hotkey CTRL + V was pressed");
18 })
19 .unwrap();
20
21 // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22 // allowing only registered pause hotkeys to be processed.
23 let trigger_key = VKey::P;
24 let modifiers = &[VKey::Control, VKey::Shift];
25 hkm.register_pause_hotkey(trigger_key, modifiers, || {
26 println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27 })
28 .unwrap();
29
30 hkm.event_loop();
31}Sourcepub fn register_channel(&mut self, channel: Sender<T>)
pub fn register_channel(&mut self, channel: Sender<T>)
Registers a channel for callback results to be sent into.
Examples found in repository?
12fn main() {
13 // The HotkeyManager is generic over the return type of the callback functions.
14 let mut hkm = HotkeyManager::new();
15 let modifiers = &[VKey::LWin, VKey::Shift];
16
17 // Register WIN + SHIFT + 1 for app command 1
18 hkm.register_hotkey(VKey::Vk1, modifiers, || {
19 println!("Pressed WIN + SHIFT + 1");
20 AppCommand::AppCommand1
21 })
22 .unwrap();
23
24 // Register WIN + SHIFT + 2 for app command 2
25 hkm.register_hotkey(VKey::Vk2, modifiers, || {
26 println!("Pressed WIN + SHIFT + 2");
27 AppCommand::AppCommand2
28 })
29 .unwrap();
30
31 // Register WIN + 3 for app command EXIT
32 hkm.register_hotkey(VKey::Vk3, modifiers, || {
33 println!("Pressed WIN + SHIFT + 3");
34 AppCommand::Exit
35 })
36 .unwrap();
37
38 // Register channel to receive events from the hkm event loop
39 let (tx, rx) = unbounded();
40 hkm.register_channel(tx);
41
42 // Run HotkeyManager in background thread
43 let handle = hkm.interrupt_handle();
44 thread::spawn(move || {
45 hkm.event_loop();
46 });
47
48 // App Logic
49 loop {
50 let command = rx.recv().unwrap();
51
52 match command {
53 AppCommand::AppCommand1 => {
54 println!("Do thing 1");
55 }
56 AppCommand::AppCommand2 => {
57 println!("Do thing 2");
58 }
59 AppCommand::Exit => {
60 println!("Exiting...");
61 handle.interrupt();
62 break;
63 }
64 }
65 }
66}Sourcepub fn event_loop(&mut self)
pub fn event_loop(&mut self)
Runs the main event loop to listen for keyboard events.
This method blocks and processes keyboard events until interrupted. It matches events against registered hotkeys and executes the corresponding callbacks.
Examples found in repository?
8fn main() {
9 let mut hkm = HotkeyManager::new();
10
11 hkm.register_hotkey(VKey::P, &[VKey::Control], || {
12 show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
13 thread::spawn(|| {
14 thread::sleep(time::Duration::from_secs(25 * 60));
15 show_popup("Pomodoro Timer", "Time's up! Take a break.");
16 });
17 })
18 .unwrap();
19
20 hkm.register_hotkey(VKey::S, &[VKey::Control], || {
21 show_popup("Pomodoro Timer", "Pomodoro stopped!");
22 })
23 .unwrap();
24
25 hkm.event_loop();
26}More examples
4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8 let trigger_key = VKey::A;
9 let mod_key = VKey::Control;
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey CTRL + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16 hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17 println!("Hotkey CTRL + V was pressed");
18 })
19 .unwrap();
20
21 // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22 // allowing only registered pause hotkeys to be processed.
23 let trigger_key = VKey::P;
24 let modifiers = &[VKey::Control, VKey::Shift];
25 hkm.register_pause_hotkey(trigger_key, modifiers, || {
26 println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27 })
28 .unwrap();
29
30 hkm.event_loop();
31}5fn main() {
6 // Create HotkeyManager
7 let mut hkm = HotkeyManager::<()>::new();
8
9 let vk_a1 = VKey::A;
10 let vk_a2 = VKey::from_keyname("a").unwrap();
11 let vk_a3 = VKey::from_vk_code(0x41);
12 let vk_a4 = VKey::from_vk_code(VK_A.0);
13
14 // Create custom keycode equivalent to A
15 let vk_a5 = VKey::CustomKeyCode(0x41);
16
17 assert_eq!(vk_a1, vk_a2);
18 assert_eq!(vk_a1, vk_a3);
19 assert_eq!(vk_a1, vk_a4);
20 assert_eq!(vk_a1, vk_a5);
21
22 // NOTE
23 // When matching `CustomKeyCodes` you must include a guard if statement
24 match vk_a5 {
25 VKey::A => {
26 // This will never show up
27 println!("CustomKeyCode(0x41) matches against VKey::A");
28 }
29 _ => {
30 println!("You didn't use the match statement correctly!");
31 }
32 }
33
34 // Instead match like this
35 match vk_a5 {
36 _ if VKey::A == vk_a5 => {
37 // This will match
38 println!("CustomKeyCode(0x41) matches against VKey::A");
39 }
40 _ => {}
41 }
42
43 hkm.register_hotkey(vk_a5, &[], || {
44 println!("You pressed A");
45 })
46 .unwrap();
47
48 hkm.event_loop();
49}6fn main() {
7 // Create the manager
8 let mut hkm = HotkeyManager::new();
9
10 // Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
11 hkm.register_hotkey(VKey::A, &[VKey::Control], || {
12 println!("Hotkey CTRL + A was pressed");
13 })
14 .unwrap();
15
16 // Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
17 let interrupt_handle = hkm.interrupt_handle();
18
19 // Get a pause handle that can be used to programmatically pause the processing of hotkeys from
20 // any thread. Note: registered pause hotkeys will still be processed.
21 let pause_handle = hkm.pause_handle();
22
23 // Create a second thread that will pause/interrupt hkm
24 spawn(move || {
25 sleep(Duration::from_secs(3));
26
27 println!("Pausing hotkeys for 3 seconds");
28 pause_handle.toggle();
29 sleep(Duration::from_secs(3));
30
31 println!("Unpausing hotkeys");
32 pause_handle.toggle();
33
34 sleep(Duration::from_secs(3));
35 interrupt_handle.interrupt();
36 });
37
38 // Run the event handler in a blocking loop. This will block until interrupted and execute the
39 // set callbacks when registered hotkeys are detected
40 hkm.event_loop();
41
42 println!("Event Loop interrupted");
43}12fn main() {
13 // The HotkeyManager is generic over the return type of the callback functions.
14 let mut hkm = HotkeyManager::new();
15 let modifiers = &[VKey::LWin, VKey::Shift];
16
17 // Register WIN + SHIFT + 1 for app command 1
18 hkm.register_hotkey(VKey::Vk1, modifiers, || {
19 println!("Pressed WIN + SHIFT + 1");
20 AppCommand::AppCommand1
21 })
22 .unwrap();
23
24 // Register WIN + SHIFT + 2 for app command 2
25 hkm.register_hotkey(VKey::Vk2, modifiers, || {
26 println!("Pressed WIN + SHIFT + 2");
27 AppCommand::AppCommand2
28 })
29 .unwrap();
30
31 // Register WIN + 3 for app command EXIT
32 hkm.register_hotkey(VKey::Vk3, modifiers, || {
33 println!("Pressed WIN + SHIFT + 3");
34 AppCommand::Exit
35 })
36 .unwrap();
37
38 // Register channel to receive events from the hkm event loop
39 let (tx, rx) = unbounded();
40 hkm.register_channel(tx);
41
42 // Run HotkeyManager in background thread
43 let handle = hkm.interrupt_handle();
44 thread::spawn(move || {
45 hkm.event_loop();
46 });
47
48 // App Logic
49 loop {
50 let command = rx.recv().unwrap();
51
52 match command {
53 AppCommand::AppCommand1 => {
54 println!("Do thing 1");
55 }
56 AppCommand::AppCommand2 => {
57 println!("Do thing 2");
58 }
59 AppCommand::Exit => {
60 println!("Exiting...");
61 handle.interrupt();
62 break;
63 }
64 }
65 }
66}4fn main() {
5 let mut hkm = HotkeyManager::new();
6
7 // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8 let trigger_key = VKey::from_keyname("a").unwrap();
9 let mod_key = VKey::from_keyname("alt").unwrap();
10 hkm.register_hotkey(trigger_key, &[mod_key], || {
11 println!("Hotkey ALT + A was pressed");
12 })
13 .unwrap();
14
15 // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16 let trigger_key = VKey::from_keyname("b").unwrap();
17 let modifiers = &[VKey::from_vk_code(0x87)];
18 hkm.register_hotkey(trigger_key, modifiers, || {
19 println!("Hotkey F24 + B was pressed");
20 })
21 .unwrap();
22
23 // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24 hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25 println!("Hotkey WIN + ALT + C was pressed");
26 })
27 .unwrap();
28
29 // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30 let hotkey_id = hkm
31 .register_hotkey(
32 VKey::from_vk_code(0x44),
33 &[VKey::from_vk_code(0xA4)],
34 || {
35 println!("Hotkey ALT + D was pressed");
36 },
37 )
38 .unwrap();
39
40 // Unregister hotkey with ID
41 hkm.unregister_hotkey(hotkey_id);
42
43 // Run the event handler in a blocking loop. This will block forever and execute the set
44 // callbacks when the registered hotkeys are detected
45 hkm.event_loop();
46}Sourcepub fn interrupt_handle(&self) -> InterruptHandle
pub fn interrupt_handle(&self) -> InterruptHandle
Signals the HotkeyManager to interrupt its event loop.
Examples found in repository?
6fn main() {
7 // Create the manager
8 let mut hkm = HotkeyManager::new();
9
10 // Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
11 hkm.register_hotkey(VKey::A, &[VKey::Control], || {
12 println!("Hotkey CTRL + A was pressed");
13 })
14 .unwrap();
15
16 // Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
17 let interrupt_handle = hkm.interrupt_handle();
18
19 // Get a pause handle that can be used to programmatically pause the processing of hotkeys from
20 // any thread. Note: registered pause hotkeys will still be processed.
21 let pause_handle = hkm.pause_handle();
22
23 // Create a second thread that will pause/interrupt hkm
24 spawn(move || {
25 sleep(Duration::from_secs(3));
26
27 println!("Pausing hotkeys for 3 seconds");
28 pause_handle.toggle();
29 sleep(Duration::from_secs(3));
30
31 println!("Unpausing hotkeys");
32 pause_handle.toggle();
33
34 sleep(Duration::from_secs(3));
35 interrupt_handle.interrupt();
36 });
37
38 // Run the event handler in a blocking loop. This will block until interrupted and execute the
39 // set callbacks when registered hotkeys are detected
40 hkm.event_loop();
41
42 println!("Event Loop interrupted");
43}More examples
12fn main() {
13 // The HotkeyManager is generic over the return type of the callback functions.
14 let mut hkm = HotkeyManager::new();
15 let modifiers = &[VKey::LWin, VKey::Shift];
16
17 // Register WIN + SHIFT + 1 for app command 1
18 hkm.register_hotkey(VKey::Vk1, modifiers, || {
19 println!("Pressed WIN + SHIFT + 1");
20 AppCommand::AppCommand1
21 })
22 .unwrap();
23
24 // Register WIN + SHIFT + 2 for app command 2
25 hkm.register_hotkey(VKey::Vk2, modifiers, || {
26 println!("Pressed WIN + SHIFT + 2");
27 AppCommand::AppCommand2
28 })
29 .unwrap();
30
31 // Register WIN + 3 for app command EXIT
32 hkm.register_hotkey(VKey::Vk3, modifiers, || {
33 println!("Pressed WIN + SHIFT + 3");
34 AppCommand::Exit
35 })
36 .unwrap();
37
38 // Register channel to receive events from the hkm event loop
39 let (tx, rx) = unbounded();
40 hkm.register_channel(tx);
41
42 // Run HotkeyManager in background thread
43 let handle = hkm.interrupt_handle();
44 thread::spawn(move || {
45 hkm.event_loop();
46 });
47
48 // App Logic
49 loop {
50 let command = rx.recv().unwrap();
51
52 match command {
53 AppCommand::AppCommand1 => {
54 println!("Do thing 1");
55 }
56 AppCommand::AppCommand2 => {
57 println!("Do thing 2");
58 }
59 AppCommand::Exit => {
60 println!("Exiting...");
61 handle.interrupt();
62 break;
63 }
64 }
65 }
66}Sourcepub fn pause_handle(&self) -> PauseHandle
pub fn pause_handle(&self) -> PauseHandle
Signals the HotkeyManager to pause processing of hotkeys.
Examples found in repository?
6fn main() {
7 // Create the manager
8 let mut hkm = HotkeyManager::new();
9
10 // Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
11 hkm.register_hotkey(VKey::A, &[VKey::Control], || {
12 println!("Hotkey CTRL + A was pressed");
13 })
14 .unwrap();
15
16 // Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
17 let interrupt_handle = hkm.interrupt_handle();
18
19 // Get a pause handle that can be used to programmatically pause the processing of hotkeys from
20 // any thread. Note: registered pause hotkeys will still be processed.
21 let pause_handle = hkm.pause_handle();
22
23 // Create a second thread that will pause/interrupt hkm
24 spawn(move || {
25 sleep(Duration::from_secs(3));
26
27 println!("Pausing hotkeys for 3 seconds");
28 pause_handle.toggle();
29 sleep(Duration::from_secs(3));
30
31 println!("Unpausing hotkeys");
32 pause_handle.toggle();
33
34 sleep(Duration::from_secs(3));
35 interrupt_handle.interrupt();
36 });
37
38 // Run the event handler in a blocking loop. This will block until interrupted and execute the
39 // set callbacks when registered hotkeys are detected
40 hkm.event_loop();
41
42 println!("Event Loop interrupted");
43}