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
impl HookBuilder
pub fn new() -> Self
sourcepub fn with_mouse(self) -> Self
pub fn with_mouse(self) -> Self
Instructs builder to spawn a new mouse hook in background thread on HookBuilder::build().
sourcepub fn with_keyboard(self) -> Self
pub fn with_keyboard(self) -> Self
Instructs builder to spawn a new keyboard hook in background thread on HookBuilder::build().