pub struct InterruptHandle { /* private fields */ }Expand description
A handle for signaling the HotkeyManager to stop its event loop.
The InterruptHandle is used to gracefully interrupt the event loop by sending
a control signal. This allows the HotkeyManager to clean up resources and stop
processing keyboard events.
Implementations§
Source§impl InterruptHandle
impl InterruptHandle
Sourcepub fn interrupt(&self)
pub fn interrupt(&self)
Interrupts the HotkeyManager’s event loop.
This method sets an internal flag to indicate that the interruption has been requested. then sends a dummy keyboard event to the event loop to force it to check the flag.
Examples found in repository?
examples/handles.rs (line 35)
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
examples/app_command.rs (line 61)
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}Trait Implementations§
Source§impl Clone for InterruptHandle
impl Clone for InterruptHandle
Source§fn clone(&self) -> InterruptHandle
fn clone(&self) -> InterruptHandle
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for InterruptHandle
impl RefUnwindSafe for InterruptHandle
impl Send for InterruptHandle
impl Sync for InterruptHandle
impl Unpin for InterruptHandle
impl UnwindSafe for InterruptHandle
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