I’m using global hooks from user32.dll with dllimport in C#. Keyboard one works fine, but the mouse wheel events are a problem. This is my mouse event callback:
private IntPtr MouseInputCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0) return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
int eventType = wParam.ToInt32();
if (eventType == WM_MOUSEHWHEEL)
{
int wheelMovement = GetWheelDeltaWParam(eventType);
}
return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
}
Everything goes fine until I have to retrieve the WHEEL_DELTA value that shows which way and how much the wheel was scrolled. Since C# lacks the GET_WHEEL_DELTA_WPARAM macro, I’m using this code that should do the job:
private static int GetWheelDeltaWParam(int wparam) { return
(int)(wparam >> 16); }
But the output is always 0, which doesn’t make any sense.
EDIT – result:
MSLLHOOKSTRUCT mouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
int wheelMovement = GetWheelDeltaWParam(mouseData.mouseData);
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public Point pt;
public int mouseData;
public int flags;
public int time;
public long dwExtraInfo;
}
The problem is that
GET_WHEEL_DELTA_WPARAMis for extracting the mouse wheel delta from thewParamof aWindowProc, whereas what you have is aLowLevelMouseProccallback. In your case,the
wParamis simplyWM_MOUSEWHEEL; to get the wheel delta, you need to look inand within that struct,
you will find your value.
Please don’t ask me for the necessary C# P/Invoke details for working this struct as I would almost certainly get them wrong 🙂