InterruptHandle

Struct InterruptHandle 

Source
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

Source

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
Hide additional 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

Source§

fn clone(&self) -> InterruptHandle

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for InterruptHandle

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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