Struct willhook::hook::Hook

source ·
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 Windows

Implementations§

source§

impl Hook

source

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,
        }
    }
}

Trait Implementations§

source§

impl Drop for Hook

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Hook

§

impl Send for Hook

§

impl Sync for Hook

§

impl Unpin for Hook

§

impl UnwindSafe for Hook

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.