interrupt/
interrupt.rs

1
2
3
4
5
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
use std::thread::{sleep, spawn};
use std::time::Duration;
use win_hotkeys::HotkeyManager;
use win_hotkeys::VKey;

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");
}