app_command/
app_command.rs

1use crossbeam_channel::unbounded;
2use std::thread;
3use win_hotkeys::HotkeyManager;
4use win_hotkeys::VKey;
5
6enum AppCommand {
7    AppCommand1,
8    AppCommand2,
9    Exit,
10}
11
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}