pub struct Hook {}Expand description
Handle to a low-level Windows hook for keyboard and/or mouse events, regardless of application focus. For more details see the HookBuilder. When the handle goes out of scope, then the low-level hook is removed.
Example
{
// create low-level hook and return the handle
let hook = HookBuilder::new().with_mouse().build().unwrap();
}
// hook handle goes out of scope,
// underlying low-level hook(s) are unhooked from WindowsImplementations§
source§impl Hook
impl Hook
sourcepub fn try_recv(&self) -> Result<InputEvent, TryRecvError>
pub fn try_recv(&self) -> Result<InputEvent, TryRecvError>
Tries to receive an event from the low-level hook(s) running in the background thread(s). If there are no events at the moment, will return Err(std::sync::mpsc::Empty):
// create low-level hook and store handle in `hook`
let hook = HookBuilder::new().with_mouse().build().unwrap();
// This example definitely can't receive any user input, so the try_recv will fail:
assert!(hook.try_recv().is_err());
assert_eq!(hook.try_recv().err(), Some(TryRecvError::Empty));Hook::try_recv() should be treated as a foundation for more complex processing. For example if one would be intereted in only unique key presses with timestamps (regardless of how long the key press lasts):
use std::sync::mpsc::channel;
use std::time::Instant;
let (event_sender, _event_receiver) = channel();
while let Ok(event) = hook.try_recv() {
// Process only "press ups" to find unique key presses,
// because if a user holds a key, then Windows can emit multiple "key down" events
if let InputEvent::Keyboard(event) = event {
match event.pressed {
KeyPress::Up(is_system) => { event_sender.send( (event, Instant::now() )); },
_ => continue,
}
}
}