no_repeat/no_repeat.rs
1use windows_hotkeys::{
2 keys::{ModKey, VKey},
3 HotkeyManagerImpl,
4};
5
6fn main() {
7 // Create a HotkeyManager.
8 // By default, the hotkey registration will add the NoRepeat modifier. This causes the callback
9 // to only be triggered once, when the combination is held down.
10 let mut hkm = windows_hotkeys::threadsafe::HotkeyManager::new();
11
12 // Disable automatically applying the NoRepeat modifier. After this call, all registrations
13 // will trigger repeatedly when the hotkey is held down. This behavior can be manually changed
14 // for each registration by adding the `ModKey::NoRepeat` modifier.
15 hkm.set_no_repeat(false);
16
17 // Register a system-wide hotkey with the main key `A` and the modifier key `ALT`
18 //
19 // NOTE: This will trigger multiple times when the combination is held down
20 hkm.register(VKey::A, &[ModKey::Alt], || {
21 println!("Hotkey ALT + A was pressed");
22 })
23 .unwrap();
24
25 // Register a system-wide hotkey with the main key `B` and multiple modifier keys
26 // (`CTRL` + `ALT`)
27 //
28 // NOTE: This will only be triggered once and not repeatedly when being held down, since the
29 // NoRepeat modifier is added manually.
30 hkm.register(
31 VKey::B,
32 &[ModKey::Ctrl, ModKey::Alt, ModKey::NoRepeat],
33 || {
34 println!("Hotkey CTRL + ALT + B was pressed");
35 },
36 )
37 .unwrap();
38
39 // Register a system-wide hotkey for `ALT` + `B` with extra keys `Left` + `Right`. This will
40 // trigger only if the `Left` + `Right` keys are also pressed during `ALT` + `B`. So just
41 // pressing `ALT` + `B` alone won't execute the closure
42 //
43 // NOTE: This will trigger multiple times when the combination is held down
44 hkm.register_extrakeys(VKey::B, &[ModKey::Alt], &[VKey::Left, VKey::Right], || {
45 println!("Hotkey ALT + B + Left + Right was pressed");
46 })
47 .unwrap();
48
49 // Run the event handler in a blocking loop. This will block forever and execute the set
50 // callbacks when registered hotkeys are detected
51 hkm.event_loop();
52}