4
MSG msg;

LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
  if (nCode >= 0) {
    if (wParam == WM_MOUSEWHEEL) cout << GET_WHEEL_DELTA_WPARAM(wParam) << endl; //prints 0
  }
  return CallNextHookEx(0, nCode, wParam, lParam);
}

bool get_state(){
    if(GetMessage(&msg,GetActiveWindow(), 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

int main()
{
    HHOOK mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
    while(true){
        get_state();
    }
    UnhookWindowsHookEx(mousehook);
    return 0;
}

I found a mouse hook in this thread, which works, but when I attempt to check the state of the mouse wheel (whether it is moving forwards or backwards), the function to get the movement always returns 0.

Am I calling the GET_WHEEL_DELTA_WPARAM(wParam) wrong? Or is the way I am using the hook not compatible with the GET_WHEEL_DELTA_WPARAM(wParam) function?

Another thread I found has code to get the mouseData (which may allow me to get the movement value I'm looking for) from a struct named MOUSEHOOKSTRUCTEX, but when I try to compile, I get the error "'MOUSEHOOKSTRUCTEX' was not declared in this scope".

Note that this code is stripped down to isolate the issue, so please forgive odd pieces such as the while loop running unhindered.

The fix is to get the movement using:

MSLLHOOKSTRUCT *pMhs = (MSLLHOOKSTRUCT *)lParam;
short zDelta            = HIWORD(pMhs->mouseData);

In the MouseHookProc function.

Answer given by user chris

Community
  • 1
  • 1
asqapro
  • 175
  • 3
  • 15
  • 1
    Read the documentation. The mouse wheel data is stored in `lParam`'s `mouseData` member. It also says `lParam` is a pointer to a `MSLLHOOKSTRUCT` object, not `MOUSEHOOKSTRUCTEX` (which is for `WH_MOUSE`). – chris Oct 19 '13 at 03:59
  • Thank you so much man, the MSLLHOOKSTRUCT was the thing it needed. I'll post your response as an edit in my initial question. – asqapro Oct 19 '13 at 04:15

0 Answers0