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

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