Handmade Hero»Forums»Code
127 posts
How to get the mouse wheel value?

I tried to use GET_WHEEL_DELTA_WPARAM like in the document, but it only returned 120 and -120. I also don't understand the difference between WM_MOUSEWHEEL and WM_MOUSEHWHEEL. How do you implement this in your code?

Mārtiņš Možeiko
2357 posts / 2 projects
How to get the mouse wheel value?

It is explained in the page you linked:

The high-order word indicates the distance the wheel is rotated, expressed in multiples or divisions of WHEEL_DELTA, which is 120. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user.

You simply divide by 120 to get how many scroll wheel rotations it moved up or down.

DWORD zDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;

That said, there is way to opt-in into higher precision rotations if you want. Not all mice support it, but when it supports then windows can give you more fine-grained rotation numbers. This usually is available on high-end gaming mouse where you can disable wheel notches where it rotates with fixed intervals. To do so, you need to adjust your exe manifest - see highResolutionScrollingAware and ultraHighResolutionScrollingAware elements in https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests. In such case you probably need to divide number in floats, to get partial scrolls:

float zDelta = (float)GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;

As for WM_MOUSEHWHEEL message - H there stands for "horizontal". Some mice have two wheels - your normal one for scrolling up&down (WM_MOUSEWHEEL message) and secondary for scrolling left&right (WM_MOUSEHWHEEL message). Here's example of such mouse:

Also laptop users with touchpad can "scroll" horizontally. When you move fingers up&down on touchpad that is vertical scroll with WM_MOUSEWHEEL message, but when you move fingers left&right on touchpad, that is horizontal scroll with WM_MOUSEHWHEEL message.

127 posts
How to get the mouse wheel value?
Replying to mmozeiko (#25652)

Thanks.