handles/
handles.rs

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