vkeys/
vkeys.rs

1use win_hotkeys::HotkeyManager;
2use win_hotkeys::VKey;
3use windows::Win32::UI::Input::KeyboardAndMouse::VK_A;
4
5fn main() {
6    // Create HotkeyManager
7    let mut hkm = HotkeyManager::<()>::new();
8
9    let vk_a1 = VKey::A;
10    let vk_a2 = VKey::from_keyname("a").unwrap();
11    let vk_a3 = VKey::from_vk_code(0x41);
12    let vk_a4 = VKey::from_vk_code(VK_A.0);
13
14    // Create custom keycode equivalent to A
15    let vk_a5 = VKey::CustomKeyCode(0x41);
16
17    assert_eq!(vk_a1, vk_a2);
18    assert_eq!(vk_a1, vk_a3);
19    assert_eq!(vk_a1, vk_a4);
20    assert_eq!(vk_a1, vk_a5);
21
22    // NOTE
23    // When matching `CustomKeyCodes` you must include a guard if statement
24    match vk_a5 {
25        VKey::A => {
26            // This will never show up
27            println!("CustomKeyCode(0x41) matches against VKey::A");
28        }
29        _ => {
30            println!("You didn't use the match statement correctly!");
31        }
32    }
33
34    // Instead match like this
35    match vk_a5 {
36        _ if VKey::A == vk_a5 => {
37            // This will match
38            println!("CustomKeyCode(0x41) matches against VKey::A");
39        }
40        _ => {}
41    }
42
43    hkm.register_hotkey(vk_a5, &[], || {
44        println!("You pressed A");
45    })
46    .unwrap();
47
48    hkm.event_loop();
49}