Repository navigation
Conversation
dotnet#3201) WPF exposes vertical wheel events (PreviewMouseWheel / MouseWheel) but has no equivalent for horizontal wheel rotation. Modern trackpads and tilt wheels emit WM_MOUSEHWHEEL, which WPF currently drops. This change adds the missing routed events and wires them through the input pipeline and the scrollable controls. - `HwndMouseInputProvider.cs`: handle WM_MOUSEHWHEEL alongside WM_MOUSEWHEEL, negate the wheel delta to match the direction convention, and report it as RawMouseActions.HorizontalWheelRotate - `MouseDevice.cs`: process RawMouseActions.HorizontalWheelRotate in PreNotifyInput / PostProcessInput and raise PreviewMouseHorizontalWheel / MouseHorizontalWheel - `Mouse.cs`: register PreviewMouseHorizontalWheelEvent / MouseHorizontalWheelEvent and add the corresponding Add/Remove handler methods - `Elements.xml`: add Mouse.PreviewMouseHorizontalWheel and Mouse.MouseHorizontalWheel to the mouse wheel event group, regenerating UIElement, UIElement3D, and ContentElement - `ScrollViewer.cs`, `TextBoxBase.cs`, `FlowDocumentScrollViewer.cs`: scroll horizontally in response to MouseHorizontalWheel - `ref/PresentationCore.cs`: update the API contract
|
@dotnet-policy-service agree |
|
This is currently not actionable as it requires an API Proposal and must go through the API Review process. |
|
The PR is straightforward and looks reasonable, the directions are slightly confusing though. It inverts the delta value and then scrolls right when it is negative? Why is that? Can anyone check other other frameworks which way the values go? |
The raw horizontal wheel delta does not follow the same direction convention as the vertical wheel delta: scrolling down reports a negative value, while scrolling right reports a positive value. The current
Reference:
// OnMouseWheel (Vertical)
if (ScrollInfo != null)
{
if (e.Delta < 0) { ScrollInfo.MouseWheelDown(); }
else { ScrollInfo.MouseWheelUp(); }
}
// OnMouseHorizontalWheel (Following OnMouseWheel's pattern)
if (ScrollInfo != null)
{
if (e.Delta < 0) { ScrollInfo.MouseWheelRight(); }
else { ScrollInfo.MouseWheelLeft(); }
}The direction can be fixed in one of two places: negate the delta in // HwndMouseInputProvider
if (msg == WindowMessage.WM_MOUSEHWHEEL)
wheel = -wheel;
// Or
// OnMouseHorizontalWheel (Adjusted):
if (ScrollInfo != null)
{
if (e.Delta < 0) { ScrollInfo.MouseWheelLeft(); }
else { ScrollInfo.MouseWheelRight(); }
} |
- Add a comment in HwndMouseInputProvider explaining why the horizontal wheel delta is negated (WM_MOUSEHWHEEL reports a positive delta for a right tilt, opposite to WM_MOUSEWHEEL) - Fix the inline comments in TextBoxBase.OnMouseHorizontalWheel: the conditions describe horizontal scrolling, not vertical, so "scrolling up/down" should be "scrolling left/right"
|
What does UWP/WinUI do? |
The raw horizontal wheel delta is reported as-is, without negation. // Change
if (e.Delta < 0) { ScrollInfo.MouseWheelRight(); }
else { ScrollInfo.MouseWheelLeft(); }
// to
if (e.Delta < 0) { ScrollInfo.MouseWheelLeft(); }
else { ScrollInfo.MouseWheelRight(); } |
|
Imho we shouldn't attempt to "fix" things inside |
|
Agreed, we should report the values we get, the control decides how to interpret it. Also negative delta meaning wheel left sounds natural to me. |
HwndMouseInputProvider now reports the raw horizontal wheel delta without negating it. Each handler decides the direction from the raw delta: positive scrolls right, negative scrolls left. - `HwndMouseInputProvider`: drop the `wheel = -wheel` negation - `ScrollViewer`, `TextBoxBase`, `FlowDocumentScrollViewer`: update `OnMouseHorizontalWheel` to match the raw delta semantics
| // Mouse horizontal wheel rotate events are never considered redundant. | ||
| if ((rawMouseInputReport.Actions & RawMouseActions.HorizontalWheelRotate) == RawMouseActions.HorizontalWheelRotate) | ||
| { | ||
| // Console.WriteLine("RawMouseActions.HorizontalWheelRotate"); |
There was a problem hiding this comment.
This looks like debug code, please remove it.
There was a problem hiding this comment.
Well it kind of is there for the vertical one too
There was a problem hiding this comment.
This looks like debug code, please remove it.
It's copied from the VerticalWheelRotate block above, kept for consistency.
If we remove it, should the one above go too?
// Mouse wheel rotate events are never considered redundant.
if ((rawMouseInputReport.Actions & RawMouseActions.VerticalWheelRotate) == RawMouseActions.VerticalWheelRotate)
{
- // Console.WriteLine("RawMouseActions.VerticalWheelRotate");
actions |= RawMouseActions.VerticalWheelRotate;
// Tell the InputManager that the MostRecentDevice is us.
_inputManager.MostRecentInputDevice = this;
}
// Mouse horizontal wheel rotate events are never considered redundant.
if ((rawMouseInputReport.Actions & RawMouseActions.HorizontalWheelRotate) == RawMouseActions.HorizontalWheelRotate)
{
- // Console.WriteLine("RawMouseActions.HorizontalWheelRotate");
actions |= RawMouseActions.HorizontalWheelRotate;
// Tell the InputManager that the MostRecentDevice is us.
_inputManager.MostRecentInputDevice = this;
}| { | ||
| actions = RawMouseActions.HorizontalWheelRotate; | ||
| } | ||
|
|
There was a problem hiding this comment.
actions = msg == WindowMessage.WM_MOUSEWHEEL ? RawMouseActions.VerticalWheelRotate : RawMouseActions.HorizontalWheelRotate;
There was a problem hiding this comment.
actions = msg == WindowMessage.WM_MOUSEWHEEL ? RawMouseActions.VerticalWheelRotate : RawMouseActions.HorizontalWheelRotate;
Too long for one line
Fixes #3201
Main PR N/A
Description
WPF exposes
PreviewMouseWheelandMouseWheelfor vertical wheelrotation, but has no equivalent routed events for horizontal wheel
rotation.
Windows sends
WM_MOUSEHWHEELto the active window when the horizontalscroll wheel is tilted or rotated.
HwndMouseInputProviderdoes nothandle this message, so horizontal scroll input never reaches the UI
through WPF's routed event system.
This change adds the missing routed events and wires them through the
input pipeline and the scrollable controls.
The new events follow the same pattern as the existing vertical wheel
events:
HwndMouseInputProviderhandlesWM_MOUSEHWHEELalongsideWM_MOUSEWHEEL,negates the wheel delta to match the directionreports it asconvention, and
RawMouseActions.HorizontalWheelRotateMouseDeviceprocessesRawMouseActions.HorizontalWheelRotateandraises
PreviewMouseHorizontalWheel/MouseHorizontalWheelMouseregistersPreviewMouseHorizontalWheelEvent/MouseHorizontalWheelEventand exposes theAdd/Removehandlermethods
UIElement,UIElement3D, andContentElementexpose the eventsand
OnMouseHorizontalWheel/OnPreviewMouseHorizontalWheelvirtual methods
ScrollViewer,TextBoxBase, andFlowDocumentScrollViewerscrollhorizontally in response
See: Win32
WM_MOUSEHWHEELCustomer Impact
Applications that want to support horizontal scrolling from a touchpad
or a tilt mouse wheel currently cannot receive the input through WPF.
With this change, horizontal wheel input flows through the standard
routed event system, so existing
ScrollViewer-based controls scrollhorizontally out of the box.
Regression
No. This is a new feature. The existing vertical wheel events and all
other input paths are unchanged.
Testing
Built the WPF runtime packages with
build.cmdin the Debugconfiguration and referenced them from a separate application (WPF
Gallery) to validate the feature against a real WPF app.
Verified that:
WM_MOUSEHWHEELmaps toRawMouseActions.HorizontalWheelRotatePreviewMouseHorizontalWheelandMouseHorizontalWheelfire intunnel / bubble order
ScrollViewer,TextBoxBase, andFlowDocumentScrollViewerscrollhorizontally on the new event
ref/PresentationCore.csmatches the runtimeimplementation
Risk
Low.
The change adds new events and a new message handler. Existing event
routing, vertical wheel handling, and the
RawMouseActionsenumerationsemantics are not modified.
Microsoft Reviewers: Open in CodeFlow