use std::{
sync::{Arc, Mutex},
thread::spawn,
};
use windows_hotkeys::{
keys::{ModKey, VKey},
threadsafe::HotkeyManager,
HotkeyManagerImpl,
};
fn main() {
let mut hkm = HotkeyManager::new();
println!("Created HKM1 on thread {:?}", std::thread::current().id());
let mut hkm2 = HotkeyManager::new();
println!("Created HKM2 on thread {:?}", std::thread::current().id());
let hkm2_interrupt = hkm2.interrupt_handle();
hkm2.register(VKey::C, &[ModKey::Alt], move || {
println!("Hotkey ALT + C was pressed");
})
.unwrap();
println!(
"Registered Keys for HKM2 on thread {:?}",
std::thread::current().id()
);
let hkm2 = Arc::new(Mutex::new(hkm2));
hkm.register(VKey::A, &[ModKey::Alt], move || {
println!("Hotkey ALT + A was pressed");
let hkm2 = hkm2.clone();
spawn(move || {
if let Ok(hkm2) = hkm2.try_lock() {
println!(
"Start listening for hotkeys with HKM2 on thread {:?}",
std::thread::current().id()
);
hkm2.event_loop();
println!("HotkeyManager2 ended");
} else {
println!("HotkeyManager2 is already active");
}
});
})
.unwrap();
println!(
"Registered Keys for HKM1 on thread {:?}",
std::thread::current().id()
);
hkm.register(VKey::B, &[ModKey::Alt], move || {
println!("Hotkey ALT + B was pressed");
hkm2_interrupt.interrupt();
})
.unwrap();
println!(
"Registered Keys for HKM1 on thread {:?}",
std::thread::current().id()
);
spawn(move || {
println!(
"Started listening for hotkeys with HKM1 on thread {:?}",
std::thread::current().id()
);
hkm.event_loop();
})
.join()
.unwrap();
}