HotkeyManager

Struct HotkeyManager 

Source
pub struct HotkeyManager<T> { /* private fields */ }
Expand description

Manages the lifecycle of hotkeys, including their registration, unregistration, and execution.

The HotkeyManager listens for keyboard events and triggers the corresponding hotkey callbacks when events match registered hotkeys.

§Type Parameters

  • T: The return type of the hotkey callbacks.

Implementations§

Source§

impl<T> HotkeyManager<T>

Source

pub fn new() -> HotkeyManager<T>

Examples found in repository?
examples/pomodoro_timer.rs (line 9)
8fn main() {
9    let mut hkm = HotkeyManager::new();
10
11    hkm.register_hotkey(VKey::P, &[VKey::Control], || {
12        show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
13        thread::spawn(|| {
14            thread::sleep(time::Duration::from_secs(25 * 60));
15            show_popup("Pomodoro Timer", "Time's up! Take a break.");
16        });
17    })
18    .unwrap();
19
20    hkm.register_hotkey(VKey::S, &[VKey::Control], || {
21        show_popup("Pomodoro Timer", "Pomodoro stopped!");
22    })
23    .unwrap();
24
25    hkm.event_loop();
26}
More examples
Hide additional examples
examples/pause.rs (line 5)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8    let trigger_key = VKey::A;
9    let mod_key = VKey::Control;
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey CTRL + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16    hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17        println!("Hotkey CTRL + V was pressed");
18    })
19    .unwrap();
20
21    // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22    // allowing only registered pause hotkeys to be processed.
23    let trigger_key = VKey::P;
24    let modifiers = &[VKey::Control, VKey::Shift];
25    hkm.register_pause_hotkey(trigger_key, modifiers, || {
26        println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27    })
28    .unwrap();
29
30    hkm.event_loop();
31}
examples/vkeys.rs (line 7)
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}
examples/handles.rs (line 8)
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}
examples/app_command.rs (line 14)
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}
examples/simple.rs (line 5)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8    let trigger_key = VKey::from_keyname("a").unwrap();
9    let mod_key = VKey::from_keyname("alt").unwrap();
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey ALT + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16    let trigger_key = VKey::from_keyname("b").unwrap();
17    let modifiers = &[VKey::from_vk_code(0x87)];
18    hkm.register_hotkey(trigger_key, modifiers, || {
19        println!("Hotkey F24 + B was pressed");
20    })
21    .unwrap();
22
23    // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24    hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25        println!("Hotkey WIN + ALT + C was pressed");
26    })
27    .unwrap();
28
29    // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30    let hotkey_id = hkm
31        .register_hotkey(
32            VKey::from_vk_code(0x44),
33            &[VKey::from_vk_code(0xA4)],
34            || {
35                println!("Hotkey ALT + D was pressed");
36            },
37        )
38        .unwrap();
39
40    // Unregister hotkey with ID
41    hkm.unregister_hotkey(hotkey_id);
42
43    // Run the event handler in a blocking loop. This will block forever and execute the set
44    // callbacks when the registered hotkeys are detected
45    hkm.event_loop();
46}
Source

pub fn register_hotkey( &mut self, trigger_key: VKey, mod_keys: &[VKey], callback: impl Fn() -> T + Send + 'static, ) -> Result<i32, WHKError>

Registers a new hotkey.

Examples found in repository?
examples/pomodoro_timer.rs (lines 11-17)
8fn main() {
9    let mut hkm = HotkeyManager::new();
10
11    hkm.register_hotkey(VKey::P, &[VKey::Control], || {
12        show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
13        thread::spawn(|| {
14            thread::sleep(time::Duration::from_secs(25 * 60));
15            show_popup("Pomodoro Timer", "Time's up! Take a break.");
16        });
17    })
18    .unwrap();
19
20    hkm.register_hotkey(VKey::S, &[VKey::Control], || {
21        show_popup("Pomodoro Timer", "Pomodoro stopped!");
22    })
23    .unwrap();
24
25    hkm.event_loop();
26}
More examples
Hide additional examples
examples/pause.rs (lines 10-12)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8    let trigger_key = VKey::A;
9    let mod_key = VKey::Control;
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey CTRL + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16    hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17        println!("Hotkey CTRL + V was pressed");
18    })
19    .unwrap();
20
21    // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22    // allowing only registered pause hotkeys to be processed.
23    let trigger_key = VKey::P;
24    let modifiers = &[VKey::Control, VKey::Shift];
25    hkm.register_pause_hotkey(trigger_key, modifiers, || {
26        println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27    })
28    .unwrap();
29
30    hkm.event_loop();
31}
examples/vkeys.rs (lines 43-45)
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}
examples/handles.rs (lines 11-13)
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}
examples/app_command.rs (lines 18-21)
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}
examples/simple.rs (lines 10-12)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8    let trigger_key = VKey::from_keyname("a").unwrap();
9    let mod_key = VKey::from_keyname("alt").unwrap();
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey ALT + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16    let trigger_key = VKey::from_keyname("b").unwrap();
17    let modifiers = &[VKey::from_vk_code(0x87)];
18    hkm.register_hotkey(trigger_key, modifiers, || {
19        println!("Hotkey F24 + B was pressed");
20    })
21    .unwrap();
22
23    // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24    hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25        println!("Hotkey WIN + ALT + C was pressed");
26    })
27    .unwrap();
28
29    // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30    let hotkey_id = hkm
31        .register_hotkey(
32            VKey::from_vk_code(0x44),
33            &[VKey::from_vk_code(0xA4)],
34            || {
35                println!("Hotkey ALT + D was pressed");
36            },
37        )
38        .unwrap();
39
40    // Unregister hotkey with ID
41    hkm.unregister_hotkey(hotkey_id);
42
43    // Run the event handler in a blocking loop. This will block forever and execute the set
44    // callbacks when the registered hotkeys are detected
45    hkm.event_loop();
46}
Source

pub fn unregister_hotkey(&mut self, hotkey_id: i32)

Unregisters a hotkey by its unique id.

Examples found in repository?
examples/simple.rs (line 41)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8    let trigger_key = VKey::from_keyname("a").unwrap();
9    let mod_key = VKey::from_keyname("alt").unwrap();
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey ALT + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16    let trigger_key = VKey::from_keyname("b").unwrap();
17    let modifiers = &[VKey::from_vk_code(0x87)];
18    hkm.register_hotkey(trigger_key, modifiers, || {
19        println!("Hotkey F24 + B was pressed");
20    })
21    .unwrap();
22
23    // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24    hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25        println!("Hotkey WIN + ALT + C was pressed");
26    })
27    .unwrap();
28
29    // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30    let hotkey_id = hkm
31        .register_hotkey(
32            VKey::from_vk_code(0x44),
33            &[VKey::from_vk_code(0xA4)],
34            || {
35                println!("Hotkey ALT + D was pressed");
36            },
37        )
38        .unwrap();
39
40    // Unregister hotkey with ID
41    hkm.unregister_hotkey(hotkey_id);
42
43    // Run the event handler in a blocking loop. This will block forever and execute the set
44    // callbacks when the registered hotkeys are detected
45    hkm.event_loop();
46}
Source

pub fn unregister_all(&mut self)

Unregisters all hotkeys.

Source

pub fn register_pause_hotkey( &mut self, trigger_key: VKey, mod_keys: &[VKey], callback: impl Fn() -> T + Send + 'static, ) -> Result<i32, WHKError>

Registers a hotkey that will toggle the paused state of the HotkeyManager. When paused, only registered pause hotkeys will be allowed to trigger.

Examples found in repository?
examples/pause.rs (lines 25-27)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8    let trigger_key = VKey::A;
9    let mod_key = VKey::Control;
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey CTRL + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16    hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17        println!("Hotkey CTRL + V was pressed");
18    })
19    .unwrap();
20
21    // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22    // allowing only registered pause hotkeys to be processed.
23    let trigger_key = VKey::P;
24    let modifiers = &[VKey::Control, VKey::Shift];
25    hkm.register_pause_hotkey(trigger_key, modifiers, || {
26        println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27    })
28    .unwrap();
29
30    hkm.event_loop();
31}
Source

pub fn register_channel(&mut self, channel: Sender<T>)

Registers a channel for callback results to be sent into.

Examples found in repository?
examples/app_command.rs (line 40)
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}
Source

pub fn event_loop(&mut self)

Runs the main event loop to listen for keyboard events.

This method blocks and processes keyboard events until interrupted. It matches events against registered hotkeys and executes the corresponding callbacks.

Examples found in repository?
examples/pomodoro_timer.rs (line 25)
8fn main() {
9    let mut hkm = HotkeyManager::new();
10
11    hkm.register_hotkey(VKey::P, &[VKey::Control], || {
12        show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
13        thread::spawn(|| {
14            thread::sleep(time::Duration::from_secs(25 * 60));
15            show_popup("Pomodoro Timer", "Time's up! Take a break.");
16        });
17    })
18    .unwrap();
19
20    hkm.register_hotkey(VKey::S, &[VKey::Control], || {
21        show_popup("Pomodoro Timer", "Pomodoro stopped!");
22    })
23    .unwrap();
24
25    hkm.event_loop();
26}
More examples
Hide additional examples
examples/pause.rs (line 30)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'CTRL'
8    let trigger_key = VKey::A;
9    let mod_key = VKey::Control;
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey CTRL + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'V' and the modifier key 'CTRL'
16    hkm.register_hotkey(VKey::V, &[VKey::Control], || {
17        println!("Hotkey CTRL + V was pressed");
18    })
19    .unwrap();
20
21    // Register pause hotkey. This hotkey will toggle the pause state of win-hotkeys,
22    // allowing only registered pause hotkeys to be processed.
23    let trigger_key = VKey::P;
24    let modifiers = &[VKey::Control, VKey::Shift];
25    hkm.register_pause_hotkey(trigger_key, modifiers, || {
26        println!("Hotkey CTRL + Shift + P toggles pause state for win-hotkeys!");
27    })
28    .unwrap();
29
30    hkm.event_loop();
31}
examples/vkeys.rs (line 48)
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}
examples/handles.rs (line 40)
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}
examples/app_command.rs (line 45)
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}
examples/simple.rs (line 45)
4fn main() {
5    let mut hkm = HotkeyManager::new();
6
7    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
8    let trigger_key = VKey::from_keyname("a").unwrap();
9    let mod_key = VKey::from_keyname("alt").unwrap();
10    hkm.register_hotkey(trigger_key, &[mod_key], || {
11        println!("Hotkey ALT + A was pressed");
12    })
13    .unwrap();
14
15    // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
16    let trigger_key = VKey::from_keyname("b").unwrap();
17    let modifiers = &[VKey::from_vk_code(0x87)];
18    hkm.register_hotkey(trigger_key, modifiers, || {
19        println!("Hotkey F24 + B was pressed");
20    })
21    .unwrap();
22
23    // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
24    hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
25        println!("Hotkey WIN + ALT + C was pressed");
26    })
27    .unwrap();
28
29    // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
30    let hotkey_id = hkm
31        .register_hotkey(
32            VKey::from_vk_code(0x44),
33            &[VKey::from_vk_code(0xA4)],
34            || {
35                println!("Hotkey ALT + D was pressed");
36            },
37        )
38        .unwrap();
39
40    // Unregister hotkey with ID
41    hkm.unregister_hotkey(hotkey_id);
42
43    // Run the event handler in a blocking loop. This will block forever and execute the set
44    // callbacks when the registered hotkeys are detected
45    hkm.event_loop();
46}
Source

pub fn interrupt_handle(&self) -> InterruptHandle

Signals the HotkeyManager to interrupt its event loop.

Examples found in repository?
examples/handles.rs (line 17)
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}
More examples
Hide additional examples
examples/app_command.rs (line 43)
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}
Source

pub fn pause_handle(&self) -> PauseHandle

Signals the HotkeyManager to pause processing of hotkeys.

Examples found in repository?
examples/handles.rs (line 21)
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}

Trait Implementations§

Source§

impl<T> Default for HotkeyManager<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for HotkeyManager<T>

§

impl<T> !RefUnwindSafe for HotkeyManager<T>

§

impl<T> Send for HotkeyManager<T>
where T: Send,

§

impl<T> !Sync for HotkeyManager<T>

§

impl<T> Unpin for HotkeyManager<T>

§

impl<T> !UnwindSafe for HotkeyManager<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.