From 0468d32bc1e2b3f1519d9f145d12fb434c5852dc Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 16 Dec 2020 19:05:33 +0900 Subject: [PATCH] debugging --- src/lib.rs | 5 +++-- src/windows/keyboard.rs | 10 +++++++--- src/windows/mouse.rs | 12 +++++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1039577..7e1e1fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,12 +20,13 @@ pub enum EventType { MouseMoveTo { x: f64, y: f64 }, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct Event { pub time: SystemTime, pub event_type: EventType, } -pub type HookCallback = fn(event: Event); +// pub type HookCallback = fn(event: Event); +pub type HookCallback = for<'a> fn(&'a Event); pub struct Robot; diff --git a/src/windows/keyboard.rs b/src/windows/keyboard.rs index c8c287a..24a297c 100644 --- a/src/windows/keyboard.rs +++ b/src/windows/keyboard.rs @@ -262,7 +262,9 @@ fn default_callback(event: Event) { } static KEYBOARD_HHOOK: Lazy> = Lazy::new(AtomicPtr::default); -static mut HOOK_CALLBACK: HookCallback = default_callback; +static mut HOOK_CALLBACK: HookCallback = |event| { + default_callback(*event); +}; impl Robot { pub unsafe fn key_listen(callback: HookCallback) -> Result<(), windows::HookError> { @@ -287,7 +289,9 @@ impl Robot { } } - HOOK_CALLBACK = default_callback; + HOOK_CALLBACK = |event| { + default_callback(*event); + }; Ok(()) } @@ -375,7 +379,7 @@ impl Robot { event_type, time: SystemTime::now(), }; - HOOK_CALLBACK(event); + HOOK_CALLBACK(&event); } } CallNextHookEx(null_mut(), code, w_param, l_param) diff --git a/src/windows/mouse.rs b/src/windows/mouse.rs index 9b635a5..8f4cd40 100644 --- a/src/windows/mouse.rs +++ b/src/windows/mouse.rs @@ -24,7 +24,9 @@ fn default_callback(event: Event) { } static MOUSE_HHOOK: Lazy> = Lazy::new(AtomicPtr::default); -static mut HOOK_CALLBACK: HookCallback = default_callback; +static mut HOOK_CALLBACK: HookCallback = |event| { + default_callback(*event); +}; impl Robot { pub unsafe fn mouse_listen(callback: HookCallback) -> Result<(), windows::HookError> { @@ -49,7 +51,9 @@ impl Robot { } } - HOOK_CALLBACK = default_callback; + HOOK_CALLBACK = |event| { + default_callback(*event); + }; Ok(()) } @@ -219,15 +223,13 @@ impl Robot { _ => None, }; - println!("mouse {:?}", opt); - if let Some(event_type) = opt { let event = Event { event_type, time: SystemTime::now(), }; - HOOK_CALLBACK(event); + HOOK_CALLBACK(&event); } } CallNextHookEx(null_mut(), code, w_param, l_param)