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>
impl<T> HotkeyManager<T>
Sourcepub fn new() -> HotkeyManager<T>
pub fn new() -> HotkeyManager<T>
Examples found in repository?
examples/pomodoro_timer.rs (line 9)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
let mut hkm = HotkeyManager::new();
hkm.register_hotkey(VKey::P, &[VKey::Control], || {
show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
thread::spawn(|| {
thread::sleep(time::Duration::from_secs(25 * 60));
show_popup("Pomodoro Timer", "Time's up! Take a break.");
});
})
.unwrap();
hkm.register_hotkey(VKey::S, &[VKey::Control], || {
show_popup("Pomodoro Timer", "Pomodoro stopped!");
})
.unwrap();
hkm.event_loop();
}More examples
examples/interrupt.rs (line 8)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
// Create the manager
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
hkm.register_hotkey(VKey::A, &[VKey::Control], || {
println!("Hotkey CTRL + A was pressed");
})
.unwrap();
// Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
let handle = hkm.interrupt_handle();
// Create a second thread that will stop the event loop after 5 seconds
spawn(move || {
sleep(Duration::from_secs(5));
handle.interrupt();
});
// Run the event handler in a blocking loop. This will block until interrupted and execute the
// set callbacks when registered hotkeys are detected
hkm.event_loop();
println!("Event Loop interrupted");
}examples/vkeys.rs (line 7)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Create HotkeyManager
let mut hkm = HotkeyManager::<()>::new();
let vk_a1 = VKey::A;
let vk_a2 = VKey::from_keyname("a").unwrap();
let vk_a3 = VKey::from_vk_code(0x41);
let vk_a4 = VKey::from_vk_code(VK_A.0);
// Create custom keycode equivalent to A
let vk_a5 = VKey::CustomKeyCode(0x41);
assert_eq!(vk_a1, vk_a2);
assert_eq!(vk_a1, vk_a3);
assert_eq!(vk_a1, vk_a4);
assert_eq!(vk_a1, vk_a5);
// NOTE
// When matching `CustomKeyCodes` you must include a guard if statement
match vk_a5 {
VKey::A => {
// This will never show up
println!("CustomKeyCode(0x41) matches against VKey::A");
}
_ => {
println!("You didn't use the match statement correctly!");
}
}
// Instead match like this
match vk_a5 {
_ if VKey::A == vk_a5 => {
// This will match
println!("CustomKeyCode(0x41) matches against VKey::A");
}
_ => {}
}
hkm.register_hotkey(vk_a5, &[], || {
println!("You pressed A");
})
.unwrap();
hkm.event_loop();
}examples/app_command.rs (line 14)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() {
// The HotkeyManager is generic over the return type of the callback functions.
let mut hkm = HotkeyManager::new();
let modifiers = &[VKey::LWin, VKey::Shift];
// Register WIN + SHIFT + 1 for app command 1
hkm.register_hotkey(VKey::Vk1, modifiers, || {
println!("Pressed WIN + SHIFT + 1");
AppCommand::AppCommand1
})
.unwrap();
// Register WIN + SHIFT + 2 for app command 2
hkm.register_hotkey(VKey::Vk2, modifiers, || {
println!("Pressed WIN + SHIFT + 2");
AppCommand::AppCommand2
})
.unwrap();
// Register WIN + 3 for app command EXIT
hkm.register_hotkey(VKey::Vk3, modifiers, || {
println!("Pressed WIN + SHIFT + 3");
AppCommand::Exit
})
.unwrap();
// Register channel to receive events from the hkm event loop
let (tx, rx) = unbounded();
hkm.register_channel(tx);
// Run HotkeyManager in background thread
let handle = hkm.interrupt_handle();
thread::spawn(move || {
hkm.event_loop();
});
// App Logic
loop {
let command = rx.recv().unwrap();
match command {
AppCommand::AppCommand1 => {
println!("Do thing 1");
}
AppCommand::AppCommand2 => {
println!("Do thing 2");
}
AppCommand::Exit => {
println!("Exiting...");
handle.interrupt();
break;
}
}
}
}examples/simple.rs (line 5)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
let trigger_key = VKey::from_keyname("a").unwrap();
let mod_key = VKey::from_keyname("alt").unwrap();
hkm.register_hotkey(trigger_key, &[mod_key], || {
println!("Hotkey ALT + A was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
let trigger_key = VKey::from_keyname("b").unwrap();
let modifiers = &[VKey::from_vk_code(0x87)];
hkm.register_hotkey(trigger_key, modifiers, || {
println!("Hotkey F24 + B was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
println!("Hotkey WIN + ALT + C was pressed");
})
.unwrap();
// Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
let hotkey_id = hkm
.register_hotkey(
VKey::from_vk_code(0x44),
&[VKey::from_vk_code(0xA4)],
|| {
println!("Hotkey ALT + D was pressed");
},
)
.unwrap();
// Unregister hotkey with ID
hkm.unregister_hotkey(hotkey_id);
// Run the event handler in a blocking loop. This will block forever and execute the set
// callbacks when the registered hotkeys are detected
hkm.event_loop();
}Sourcepub fn register_hotkey(
&mut self,
trigger_key: VKey,
mod_keys: &[VKey],
callback: impl Fn() -> T + Send + 'static,
) -> Result<i32, WHKError>
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)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
let mut hkm = HotkeyManager::new();
hkm.register_hotkey(VKey::P, &[VKey::Control], || {
show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
thread::spawn(|| {
thread::sleep(time::Duration::from_secs(25 * 60));
show_popup("Pomodoro Timer", "Time's up! Take a break.");
});
})
.unwrap();
hkm.register_hotkey(VKey::S, &[VKey::Control], || {
show_popup("Pomodoro Timer", "Pomodoro stopped!");
})
.unwrap();
hkm.event_loop();
}More examples
examples/interrupt.rs (lines 11-13)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
// Create the manager
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
hkm.register_hotkey(VKey::A, &[VKey::Control], || {
println!("Hotkey CTRL + A was pressed");
})
.unwrap();
// Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
let handle = hkm.interrupt_handle();
// Create a second thread that will stop the event loop after 5 seconds
spawn(move || {
sleep(Duration::from_secs(5));
handle.interrupt();
});
// Run the event handler in a blocking loop. This will block until interrupted and execute the
// set callbacks when registered hotkeys are detected
hkm.event_loop();
println!("Event Loop interrupted");
}examples/vkeys.rs (lines 43-45)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Create HotkeyManager
let mut hkm = HotkeyManager::<()>::new();
let vk_a1 = VKey::A;
let vk_a2 = VKey::from_keyname("a").unwrap();
let vk_a3 = VKey::from_vk_code(0x41);
let vk_a4 = VKey::from_vk_code(VK_A.0);
// Create custom keycode equivalent to A
let vk_a5 = VKey::CustomKeyCode(0x41);
assert_eq!(vk_a1, vk_a2);
assert_eq!(vk_a1, vk_a3);
assert_eq!(vk_a1, vk_a4);
assert_eq!(vk_a1, vk_a5);
// NOTE
// When matching `CustomKeyCodes` you must include a guard if statement
match vk_a5 {
VKey::A => {
// This will never show up
println!("CustomKeyCode(0x41) matches against VKey::A");
}
_ => {
println!("You didn't use the match statement correctly!");
}
}
// Instead match like this
match vk_a5 {
_ if VKey::A == vk_a5 => {
// This will match
println!("CustomKeyCode(0x41) matches against VKey::A");
}
_ => {}
}
hkm.register_hotkey(vk_a5, &[], || {
println!("You pressed A");
})
.unwrap();
hkm.event_loop();
}examples/app_command.rs (lines 18-21)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() {
// The HotkeyManager is generic over the return type of the callback functions.
let mut hkm = HotkeyManager::new();
let modifiers = &[VKey::LWin, VKey::Shift];
// Register WIN + SHIFT + 1 for app command 1
hkm.register_hotkey(VKey::Vk1, modifiers, || {
println!("Pressed WIN + SHIFT + 1");
AppCommand::AppCommand1
})
.unwrap();
// Register WIN + SHIFT + 2 for app command 2
hkm.register_hotkey(VKey::Vk2, modifiers, || {
println!("Pressed WIN + SHIFT + 2");
AppCommand::AppCommand2
})
.unwrap();
// Register WIN + 3 for app command EXIT
hkm.register_hotkey(VKey::Vk3, modifiers, || {
println!("Pressed WIN + SHIFT + 3");
AppCommand::Exit
})
.unwrap();
// Register channel to receive events from the hkm event loop
let (tx, rx) = unbounded();
hkm.register_channel(tx);
// Run HotkeyManager in background thread
let handle = hkm.interrupt_handle();
thread::spawn(move || {
hkm.event_loop();
});
// App Logic
loop {
let command = rx.recv().unwrap();
match command {
AppCommand::AppCommand1 => {
println!("Do thing 1");
}
AppCommand::AppCommand2 => {
println!("Do thing 2");
}
AppCommand::Exit => {
println!("Exiting...");
handle.interrupt();
break;
}
}
}
}examples/simple.rs (lines 10-12)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
let trigger_key = VKey::from_keyname("a").unwrap();
let mod_key = VKey::from_keyname("alt").unwrap();
hkm.register_hotkey(trigger_key, &[mod_key], || {
println!("Hotkey ALT + A was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
let trigger_key = VKey::from_keyname("b").unwrap();
let modifiers = &[VKey::from_vk_code(0x87)];
hkm.register_hotkey(trigger_key, modifiers, || {
println!("Hotkey F24 + B was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
println!("Hotkey WIN + ALT + C was pressed");
})
.unwrap();
// Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
let hotkey_id = hkm
.register_hotkey(
VKey::from_vk_code(0x44),
&[VKey::from_vk_code(0xA4)],
|| {
println!("Hotkey ALT + D was pressed");
},
)
.unwrap();
// Unregister hotkey with ID
hkm.unregister_hotkey(hotkey_id);
// Run the event handler in a blocking loop. This will block forever and execute the set
// callbacks when the registered hotkeys are detected
hkm.event_loop();
}Sourcepub fn unregister_hotkey(&mut self, hotkey_id: i32)
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)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
let trigger_key = VKey::from_keyname("a").unwrap();
let mod_key = VKey::from_keyname("alt").unwrap();
hkm.register_hotkey(trigger_key, &[mod_key], || {
println!("Hotkey ALT + A was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
let trigger_key = VKey::from_keyname("b").unwrap();
let modifiers = &[VKey::from_vk_code(0x87)];
hkm.register_hotkey(trigger_key, modifiers, || {
println!("Hotkey F24 + B was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
println!("Hotkey WIN + ALT + C was pressed");
})
.unwrap();
// Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
let hotkey_id = hkm
.register_hotkey(
VKey::from_vk_code(0x44),
&[VKey::from_vk_code(0xA4)],
|| {
println!("Hotkey ALT + D was pressed");
},
)
.unwrap();
// Unregister hotkey with ID
hkm.unregister_hotkey(hotkey_id);
// Run the event handler in a blocking loop. This will block forever and execute the set
// callbacks when the registered hotkeys are detected
hkm.event_loop();
}Sourcepub fn unregister_all(&mut self)
pub fn unregister_all(&mut self)
Unregisters all hotkeys
Sourcepub fn register_channel(&mut self, channel: Sender<T>)
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)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() {
// The HotkeyManager is generic over the return type of the callback functions.
let mut hkm = HotkeyManager::new();
let modifiers = &[VKey::LWin, VKey::Shift];
// Register WIN + SHIFT + 1 for app command 1
hkm.register_hotkey(VKey::Vk1, modifiers, || {
println!("Pressed WIN + SHIFT + 1");
AppCommand::AppCommand1
})
.unwrap();
// Register WIN + SHIFT + 2 for app command 2
hkm.register_hotkey(VKey::Vk2, modifiers, || {
println!("Pressed WIN + SHIFT + 2");
AppCommand::AppCommand2
})
.unwrap();
// Register WIN + 3 for app command EXIT
hkm.register_hotkey(VKey::Vk3, modifiers, || {
println!("Pressed WIN + SHIFT + 3");
AppCommand::Exit
})
.unwrap();
// Register channel to receive events from the hkm event loop
let (tx, rx) = unbounded();
hkm.register_channel(tx);
// Run HotkeyManager in background thread
let handle = hkm.interrupt_handle();
thread::spawn(move || {
hkm.event_loop();
});
// App Logic
loop {
let command = rx.recv().unwrap();
match command {
AppCommand::AppCommand1 => {
println!("Do thing 1");
}
AppCommand::AppCommand2 => {
println!("Do thing 2");
}
AppCommand::Exit => {
println!("Exiting...");
handle.interrupt();
break;
}
}
}
}Sourcepub fn event_loop(&mut self)
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)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
let mut hkm = HotkeyManager::new();
hkm.register_hotkey(VKey::P, &[VKey::Control], || {
show_popup("Pomodoro Timer", "Pomodoro started! Focus for 25 minutes.");
thread::spawn(|| {
thread::sleep(time::Duration::from_secs(25 * 60));
show_popup("Pomodoro Timer", "Time's up! Take a break.");
});
})
.unwrap();
hkm.register_hotkey(VKey::S, &[VKey::Control], || {
show_popup("Pomodoro Timer", "Pomodoro stopped!");
})
.unwrap();
hkm.event_loop();
}More examples
examples/interrupt.rs (line 27)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
// Create the manager
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
hkm.register_hotkey(VKey::A, &[VKey::Control], || {
println!("Hotkey CTRL + A was pressed");
})
.unwrap();
// Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
let handle = hkm.interrupt_handle();
// Create a second thread that will stop the event loop after 5 seconds
spawn(move || {
sleep(Duration::from_secs(5));
handle.interrupt();
});
// Run the event handler in a blocking loop. This will block until interrupted and execute the
// set callbacks when registered hotkeys are detected
hkm.event_loop();
println!("Event Loop interrupted");
}examples/vkeys.rs (line 48)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Create HotkeyManager
let mut hkm = HotkeyManager::<()>::new();
let vk_a1 = VKey::A;
let vk_a2 = VKey::from_keyname("a").unwrap();
let vk_a3 = VKey::from_vk_code(0x41);
let vk_a4 = VKey::from_vk_code(VK_A.0);
// Create custom keycode equivalent to A
let vk_a5 = VKey::CustomKeyCode(0x41);
assert_eq!(vk_a1, vk_a2);
assert_eq!(vk_a1, vk_a3);
assert_eq!(vk_a1, vk_a4);
assert_eq!(vk_a1, vk_a5);
// NOTE
// When matching `CustomKeyCodes` you must include a guard if statement
match vk_a5 {
VKey::A => {
// This will never show up
println!("CustomKeyCode(0x41) matches against VKey::A");
}
_ => {
println!("You didn't use the match statement correctly!");
}
}
// Instead match like this
match vk_a5 {
_ if VKey::A == vk_a5 => {
// This will match
println!("CustomKeyCode(0x41) matches against VKey::A");
}
_ => {}
}
hkm.register_hotkey(vk_a5, &[], || {
println!("You pressed A");
})
.unwrap();
hkm.event_loop();
}examples/app_command.rs (line 45)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() {
// The HotkeyManager is generic over the return type of the callback functions.
let mut hkm = HotkeyManager::new();
let modifiers = &[VKey::LWin, VKey::Shift];
// Register WIN + SHIFT + 1 for app command 1
hkm.register_hotkey(VKey::Vk1, modifiers, || {
println!("Pressed WIN + SHIFT + 1");
AppCommand::AppCommand1
})
.unwrap();
// Register WIN + SHIFT + 2 for app command 2
hkm.register_hotkey(VKey::Vk2, modifiers, || {
println!("Pressed WIN + SHIFT + 2");
AppCommand::AppCommand2
})
.unwrap();
// Register WIN + 3 for app command EXIT
hkm.register_hotkey(VKey::Vk3, modifiers, || {
println!("Pressed WIN + SHIFT + 3");
AppCommand::Exit
})
.unwrap();
// Register channel to receive events from the hkm event loop
let (tx, rx) = unbounded();
hkm.register_channel(tx);
// Run HotkeyManager in background thread
let handle = hkm.interrupt_handle();
thread::spawn(move || {
hkm.event_loop();
});
// App Logic
loop {
let command = rx.recv().unwrap();
match command {
AppCommand::AppCommand1 => {
println!("Do thing 1");
}
AppCommand::AppCommand2 => {
println!("Do thing 2");
}
AppCommand::Exit => {
println!("Exiting...");
handle.interrupt();
break;
}
}
}
}examples/simple.rs (line 45)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
let trigger_key = VKey::from_keyname("a").unwrap();
let mod_key = VKey::from_keyname("alt").unwrap();
hkm.register_hotkey(trigger_key, &[mod_key], || {
println!("Hotkey ALT + A was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
let trigger_key = VKey::from_keyname("b").unwrap();
let modifiers = &[VKey::from_vk_code(0x87)];
hkm.register_hotkey(trigger_key, modifiers, || {
println!("Hotkey F24 + B was pressed");
})
.unwrap();
// Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
println!("Hotkey WIN + ALT + C was pressed");
})
.unwrap();
// Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
let hotkey_id = hkm
.register_hotkey(
VKey::from_vk_code(0x44),
&[VKey::from_vk_code(0xA4)],
|| {
println!("Hotkey ALT + D was pressed");
},
)
.unwrap();
// Unregister hotkey with ID
hkm.unregister_hotkey(hotkey_id);
// Run the event handler in a blocking loop. This will block forever and execute the set
// callbacks when the registered hotkeys are detected
hkm.event_loop();
}Sourcepub fn interrupt_handle(&self) -> InterruptHandle
pub fn interrupt_handle(&self) -> InterruptHandle
Signals the HotkeyManager to interrupt its event loop.
Examples found in repository?
examples/interrupt.rs (line 17)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
// Create the manager
let mut hkm = HotkeyManager::new();
// Register a system-wide hotkey with trigger key 'A' and modifier key 'CTRL'
hkm.register_hotkey(VKey::A, &[VKey::Control], || {
println!("Hotkey CTRL + A was pressed");
})
.unwrap();
// Get an interrupt handle that can be used to interrupt / stop the event loop from any thread
let handle = hkm.interrupt_handle();
// Create a second thread that will stop the event loop after 5 seconds
spawn(move || {
sleep(Duration::from_secs(5));
handle.interrupt();
});
// Run the event handler in a blocking loop. This will block until interrupted and execute the
// set callbacks when registered hotkeys are detected
hkm.event_loop();
println!("Event Loop interrupted");
}More examples
examples/app_command.rs (line 43)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() {
// The HotkeyManager is generic over the return type of the callback functions.
let mut hkm = HotkeyManager::new();
let modifiers = &[VKey::LWin, VKey::Shift];
// Register WIN + SHIFT + 1 for app command 1
hkm.register_hotkey(VKey::Vk1, modifiers, || {
println!("Pressed WIN + SHIFT + 1");
AppCommand::AppCommand1
})
.unwrap();
// Register WIN + SHIFT + 2 for app command 2
hkm.register_hotkey(VKey::Vk2, modifiers, || {
println!("Pressed WIN + SHIFT + 2");
AppCommand::AppCommand2
})
.unwrap();
// Register WIN + 3 for app command EXIT
hkm.register_hotkey(VKey::Vk3, modifiers, || {
println!("Pressed WIN + SHIFT + 3");
AppCommand::Exit
})
.unwrap();
// Register channel to receive events from the hkm event loop
let (tx, rx) = unbounded();
hkm.register_channel(tx);
// Run HotkeyManager in background thread
let handle = hkm.interrupt_handle();
thread::spawn(move || {
hkm.event_loop();
});
// App Logic
loop {
let command = rx.recv().unwrap();
match command {
AppCommand::AppCommand1 => {
println!("Do thing 1");
}
AppCommand::AppCommand2 => {
println!("Do thing 2");
}
AppCommand::Exit => {
println!("Exiting...");
handle.interrupt();
break;
}
}
}
}Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more