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/interrupt.rs (line 22)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
// Create the manager
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
hkm.register_hotkey(VKey::A, &[VKey::Control], || {
println!("Hotkey CTRL + A was pressed");
})
.unwrap();
// Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
let handle = hkm.interrupt_handle();
// Create a second thread that will stop the event loop after 5 seconds
spawn(move || {
sleep(Duration::from_secs(5));
handle.interrupt();
});
// Run the event handler in a blocking loop. This will block until interrupted and execute the
// set callbacks when registered hotkeys are detected
hkm.event_loop();
println!("Event Loop interrupted");
}More examples
examples/app_command.rs (line 61)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() {
// The HotkeyManager is generic over the return type of the callback functions.
let mut hkm = HotkeyManager::new();
let modifiers = &[VKey::LWin, VKey::Shift];
// Register WIN + SHIFT + 1 for app command 1
hkm.register_hotkey(VKey::Vk1, modifiers, || {
println!("Pressed WIN + SHIFT + 1");
AppCommand::AppCommand1
})
.unwrap();
// Register WIN + SHIFT + 2 for app command 2
hkm.register_hotkey(VKey::Vk2, modifiers, || {
println!("Pressed WIN + SHIFT + 2");
AppCommand::AppCommand2
})
.unwrap();
// Register WIN + 3 for app command EXIT
hkm.register_hotkey(VKey::Vk3, modifiers, || {
println!("Pressed WIN + SHIFT + 3");
AppCommand::Exit
})
.unwrap();
// Register channel to receive events from the hkm event loop
let (tx, rx) = unbounded();
hkm.register_channel(tx);
// Run HotkeyManager in background thread
let handle = hkm.interrupt_handle();
thread::spawn(move || {
hkm.event_loop();
});
// App Logic
loop {
let command = rx.recv().unwrap();
match command {
AppCommand::AppCommand1 => {
println!("Do thing 1");
}
AppCommand::AppCommand2 => {
println!("Do thing 2");
}
AppCommand::Exit => {
println!("Exiting...");
handle.interrupt();
break;
}
}
}
}Trait Implementations§
impl Send for InterruptHandle
impl Sync for InterruptHandle
Auto Trait Implementations§
impl Freeze for InterruptHandle
impl RefUnwindSafe 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