Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET_X_LPARAM gives negative value

I have created a hook for mouse. I wanted to get mouse click coordinates, but the GET_X_LPARAM() gives me negative value (and always the same when clicking on different places). My problem becomes solved with GetCursorPos(), but I wonder why it is not working with GET_X_LPARAM/GET_Y_LPARAM. Here's the code:

LRESULT CALLBACK Recorder::mouseHook( int code, WPARAM wParam, LPARAM lParam ) {
 if( code < 0 )
     return CallNextHookEx( m_mouseHook, code, wParam, lParam );   

 switch( wParam ) {
    case WM_LBUTTONDOWN:{
        int _hereIsANegativeNumber = GET_X_LPARAM( lParam );
        break;}
 }

 return CallNextHookEx( 0, code, wParam, lParam );
}

This is how I set the hook:

m_mouseHook = SetWindowsHookEx( WH_MOUSE_LL, &mouseHook, GetModuleHandle( NULL ), 0 );
like image 781
tobi Avatar asked Dec 26 '22 18:12

tobi


2 Answers

In a WH_MOUSE_LL hook, lParam is not the mouse coordinates - instead it is a pointer to a MSLLHOOKSTRUCT.

So, to get coordinates:

POINT pt = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam)->pt;

See LowLevelMouseProc for more details.

like image 137
Jonathan Potter Avatar answered Jan 14 '23 05:01

Jonathan Potter


Because for the WH_MOUSE_LL in the LowLevelMouseProc procedure what you get as the LPARAM variable is a pointer to a MSLLHOOKSTRUCT structure and use the pt member of it for getting the mouse coordinates

like image 38
Ferenc Deak Avatar answered Jan 14 '23 05:01

Ferenc Deak