@@ -11,7 +11,7 @@ use winapi::shared::ntdef::LONG;
1111use winapi:: shared:: windef:: HHOOK ;
1212use winapi:: um:: errhandlingapi:: GetLastError ;
1313use winapi:: um:: winuser:: {
14- SetWindowsHookExA , KBDLLHOOKSTRUCT , MSLLHOOKSTRUCT , WHEEL_DELTA , WH_KEYBOARD_LL , WH_MOUSE_LL ,
14+ KBDLLHOOKSTRUCT , MSLLHOOKSTRUCT , SetWindowsHookExA , WH_KEYBOARD_LL , WH_MOUSE_LL , WHEEL_DELTA ,
1515 WM_KEYDOWN , WM_KEYUP , WM_LBUTTONDOWN , WM_LBUTTONUP , WM_MBUTTONDOWN , WM_MBUTTONUP ,
1616 WM_MOUSEHWHEEL , WM_MOUSEMOVE , WM_MOUSEWHEEL , WM_RBUTTONDOWN , WM_RBUTTONUP , WM_SYSKEYDOWN ,
1717 WM_SYSKEYUP , WM_XBUTTONDOWN , WM_XBUTTONUP ,
@@ -24,104 +24,120 @@ lazy_static! {
2424 pub ( crate ) static ref KEYBOARD : Mutex <Keyboard > = Mutex :: new( Keyboard :: new( ) . unwrap( ) ) ;
2525}
2626
27- pub unsafe fn get_code ( lpdata : LPARAM ) -> DWORD { unsafe {
28- let kb = * ( lpdata as * const KBDLLHOOKSTRUCT ) ;
29- kb. vkCode
30- } }
31- pub unsafe fn get_scan_code ( lpdata : LPARAM ) -> DWORD { unsafe {
32- let kb = * ( lpdata as * const KBDLLHOOKSTRUCT ) ;
33- kb. scanCode
34- } }
35- pub unsafe fn get_point ( lpdata : LPARAM ) -> ( LONG , LONG ) { unsafe {
36- let mouse = * ( lpdata as * const MSLLHOOKSTRUCT ) ;
37- ( mouse. pt . x , mouse. pt . y )
38- } }
27+ pub unsafe fn get_code ( lpdata : LPARAM ) -> DWORD {
28+ unsafe {
29+ let kb = * ( lpdata as * const KBDLLHOOKSTRUCT ) ;
30+ kb. vkCode
31+ }
32+ }
33+ pub unsafe fn get_scan_code ( lpdata : LPARAM ) -> DWORD {
34+ unsafe {
35+ let kb = * ( lpdata as * const KBDLLHOOKSTRUCT ) ;
36+ kb. scanCode
37+ }
38+ }
39+ pub unsafe fn get_point ( lpdata : LPARAM ) -> ( LONG , LONG ) {
40+ unsafe {
41+ let mouse = * ( lpdata as * const MSLLHOOKSTRUCT ) ;
42+ ( mouse. pt . x , mouse. pt . y )
43+ }
44+ }
3945// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)
4046/// confusingly, this function returns a WORD (unsigned), but may be
4147/// interpreted as either signed or unsigned depending on context
42- pub unsafe fn get_delta ( lpdata : LPARAM ) -> WORD { unsafe {
43- let mouse = * ( lpdata as * const MSLLHOOKSTRUCT ) ;
44- HIWORD ( mouse. mouseData )
45- } }
46- pub unsafe fn get_button_code ( lpdata : LPARAM ) -> WORD { unsafe {
47- let mouse = * ( lpdata as * const MSLLHOOKSTRUCT ) ;
48- HIWORD ( mouse. mouseData )
49- } }
48+ pub unsafe fn get_delta ( lpdata : LPARAM ) -> WORD {
49+ unsafe {
50+ let mouse = * ( lpdata as * const MSLLHOOKSTRUCT ) ;
51+ HIWORD ( mouse. mouseData )
52+ }
53+ }
54+ pub unsafe fn get_button_code ( lpdata : LPARAM ) -> WORD {
55+ unsafe {
56+ let mouse = * ( lpdata as * const MSLLHOOKSTRUCT ) ;
57+ HIWORD ( mouse. mouseData )
58+ }
59+ }
5060
51- pub unsafe fn convert ( param : WPARAM , lpdata : LPARAM ) -> Option < EventType > { unsafe {
52- match param. try_into ( ) {
53- Ok ( WM_KEYDOWN ) | Ok ( WM_SYSKEYDOWN ) => {
54- let code = get_code ( lpdata) ;
55- let key = key_from_code ( code as u16 ) ;
56- Some ( EventType :: KeyPress ( key) )
57- }
58- Ok ( WM_KEYUP ) | Ok ( WM_SYSKEYUP ) => {
59- let code = get_code ( lpdata) ;
60- let key = key_from_code ( code as u16 ) ;
61- Some ( EventType :: KeyRelease ( key) )
61+ pub unsafe fn convert ( param : WPARAM , lpdata : LPARAM ) -> Option < EventType > {
62+ unsafe {
63+ match param. try_into ( ) {
64+ Ok ( WM_KEYDOWN ) | Ok ( WM_SYSKEYDOWN ) => {
65+ let code = get_code ( lpdata) ;
66+ let key = key_from_code ( code as u16 ) ;
67+ Some ( EventType :: KeyPress ( key) )
68+ }
69+ Ok ( WM_KEYUP ) | Ok ( WM_SYSKEYUP ) => {
70+ let code = get_code ( lpdata) ;
71+ let key = key_from_code ( code as u16 ) ;
72+ Some ( EventType :: KeyRelease ( key) )
73+ }
74+ Ok ( WM_LBUTTONDOWN ) => Some ( EventType :: ButtonPress ( Button :: Left ) ) ,
75+ Ok ( WM_LBUTTONUP ) => Some ( EventType :: ButtonRelease ( Button :: Left ) ) ,
76+ Ok ( WM_MBUTTONDOWN ) => Some ( EventType :: ButtonPress ( Button :: Middle ) ) ,
77+ Ok ( WM_MBUTTONUP ) => Some ( EventType :: ButtonRelease ( Button :: Middle ) ) ,
78+ Ok ( WM_RBUTTONDOWN ) => Some ( EventType :: ButtonPress ( Button :: Right ) ) ,
79+ Ok ( WM_RBUTTONUP ) => Some ( EventType :: ButtonRelease ( Button :: Right ) ) ,
80+ Ok ( WM_XBUTTONDOWN ) => {
81+ let code = get_button_code ( lpdata) as u8 ;
82+ Some ( EventType :: ButtonPress ( Button :: Unknown ( code) ) )
83+ }
84+ Ok ( WM_XBUTTONUP ) => {
85+ let code = get_button_code ( lpdata) as u8 ;
86+ Some ( EventType :: ButtonRelease ( Button :: Unknown ( code) ) )
87+ }
88+ Ok ( WM_MOUSEMOVE ) => {
89+ let ( x, y) = get_point ( lpdata) ;
90+ Some ( EventType :: MouseMove {
91+ x : x as f64 ,
92+ y : y as f64 ,
93+ } )
94+ }
95+ Ok ( WM_MOUSEWHEEL ) => {
96+ let delta = get_delta ( lpdata) as c_short ;
97+ Some ( EventType :: Wheel {
98+ delta_x : 0 ,
99+ delta_y : ( delta / WHEEL_DELTA ) as i64 ,
100+ } )
101+ }
102+ Ok ( WM_MOUSEHWHEEL ) => {
103+ let delta = get_delta ( lpdata) as c_short ;
104+ Some ( EventType :: Wheel {
105+ delta_x : ( delta / WHEEL_DELTA ) as i64 ,
106+ delta_y : 0 ,
107+ } )
108+ }
109+ _ => None ,
62110 }
63- Ok ( WM_LBUTTONDOWN ) => Some ( EventType :: ButtonPress ( Button :: Left ) ) ,
64- Ok ( WM_LBUTTONUP ) => Some ( EventType :: ButtonRelease ( Button :: Left ) ) ,
65- Ok ( WM_MBUTTONDOWN ) => Some ( EventType :: ButtonPress ( Button :: Middle ) ) ,
66- Ok ( WM_MBUTTONUP ) => Some ( EventType :: ButtonRelease ( Button :: Middle ) ) ,
67- Ok ( WM_RBUTTONDOWN ) => Some ( EventType :: ButtonPress ( Button :: Right ) ) ,
68- Ok ( WM_RBUTTONUP ) => Some ( EventType :: ButtonRelease ( Button :: Right ) ) ,
69- Ok ( WM_XBUTTONDOWN ) => {
70- let code = get_button_code ( lpdata) as u8 ;
71- Some ( EventType :: ButtonPress ( Button :: Unknown ( code) ) )
72- }
73- Ok ( WM_XBUTTONUP ) => {
74- let code = get_button_code ( lpdata) as u8 ;
75- Some ( EventType :: ButtonRelease ( Button :: Unknown ( code) ) )
76- }
77- Ok ( WM_MOUSEMOVE ) => {
78- let ( x, y) = get_point ( lpdata) ;
79- Some ( EventType :: MouseMove {
80- x : x as f64 ,
81- y : y as f64 ,
82- } )
83- }
84- Ok ( WM_MOUSEWHEEL ) => {
85- let delta = get_delta ( lpdata) as c_short ;
86- Some ( EventType :: Wheel {
87- delta_x : 0 ,
88- delta_y : ( delta / WHEEL_DELTA ) as i64 ,
89- } )
90- }
91- Ok ( WM_MOUSEHWHEEL ) => {
92- let delta = get_delta ( lpdata) as c_short ;
93- Some ( EventType :: Wheel {
94- delta_x : ( delta / WHEEL_DELTA ) as i64 ,
95- delta_y : 0 ,
96- } )
97- }
98- _ => None ,
99111 }
100- } }
112+ }
101113
102114type RawCallback = unsafe extern "system" fn ( code : c_int , param : WPARAM , lpdata : LPARAM ) -> LRESULT ;
103115pub enum HookError {
104116 Mouse ( DWORD ) ,
105117 Key ( DWORD ) ,
106118}
107119
108- pub unsafe fn set_key_hook ( callback : RawCallback ) -> Result < ( ) , HookError > { unsafe {
109- let hook = SetWindowsHookExA ( WH_KEYBOARD_LL , Some ( callback) , null_mut ( ) , 0 ) ;
120+ pub unsafe fn set_key_hook ( callback : RawCallback ) -> Result < ( ) , HookError > {
121+ unsafe {
122+ let hook = SetWindowsHookExA ( WH_KEYBOARD_LL , Some ( callback) , null_mut ( ) , 0 ) ;
110123
111- if hook. is_null ( ) {
112- let error = GetLastError ( ) ;
113- return Err ( HookError :: Key ( error) ) ;
124+ if hook. is_null ( ) {
125+ let error = GetLastError ( ) ;
126+ return Err ( HookError :: Key ( error) ) ;
127+ }
128+ HOOK = hook;
129+ Ok ( ( ) )
114130 }
115- HOOK = hook;
116- Ok ( ( ) )
117- } }
131+ }
118132
119- pub unsafe fn set_mouse_hook ( callback : RawCallback ) -> Result < ( ) , HookError > { unsafe {
120- let hook = SetWindowsHookExA ( WH_MOUSE_LL , Some ( callback) , null_mut ( ) , 0 ) ;
121- if hook. is_null ( ) {
122- let error = GetLastError ( ) ;
123- return Err ( HookError :: Mouse ( error) ) ;
133+ pub unsafe fn set_mouse_hook ( callback : RawCallback ) -> Result < ( ) , HookError > {
134+ unsafe {
135+ let hook = SetWindowsHookExA ( WH_MOUSE_LL , Some ( callback) , null_mut ( ) , 0 ) ;
136+ if hook. is_null ( ) {
137+ let error = GetLastError ( ) ;
138+ return Err ( HookError :: Mouse ( error) ) ;
139+ }
140+ HOOK = hook;
141+ Ok ( ( ) )
124142 }
125- HOOK = hook;
126- Ok ( ( ) )
127- } }
143+ }
0 commit comments