Struct willhook::hook::HookBuilder

source ·
pub struct HookBuilder { /* private fields */ }
Expand description

The only way to build a hook is to use HookBuilder. It is possible to choose what types of hooks are active. Currently only “mouse” and “keyboard” hooks are supported (due to Windows API restrictions).

Build hook for both mouse and keyboard:

use willhook::hook::HookBuilder;
fn main() {
    let hook = HookBuilder::new()
                .with_mouse()
                .with_keyboard()
                .build();
    assert!(hook.is_some());
}

Limitations

At least one hook type has to be specified, otherwise build will fail:

let bad_hook = HookBuilder::new().build();
assert!(bad_hook.is_none());

There can be only one hook at the moment, even if we try to create different type:

let hook = HookBuilder::new()
            .with_mouse()
            .build();
 
assert!(hook.is_some());
// Building second hook while the first one is still in scope will fail.
// Even if that second hook is keyboard hook:
let another_hook = HookBuilder::new().with_keyboard().build();
assert!(another_hook.is_none());

Only after the old hook is dropped, the new one can be created:

let hook = HookBuilder::new()
            .with_mouse()
            .build();
 
assert!(hook.is_some());
// It could go out of scope as well, but let's drop it explicitly:
drop(hook);
// Since there is no "active" hook at the moment, now we can create another:
let another_hook = HookBuilder::new().with_keyboard().build();
assert!(another_hook.is_some());

Implementations§

source§

impl HookBuilder

source

pub fn new() -> Self

source

pub fn with_mouse(self) -> Self

Instructs builder to spawn a new mouse hook in background thread on HookBuilder::build().

source

pub fn with_keyboard(self) -> Self

Instructs builder to spawn a new keyboard hook in background thread on HookBuilder::build().

source

pub fn build(self) -> Option<Hook>

Builds the requested hooks and returns common handle for them. If any hooks are active, then the build fails.

Auto Trait Implementations§

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.