Skip to content

Add MouseHorizontalWheel and PreviewMouseHorizontalWheel routed events - #11956

Open
Roshaless wants to merge 4 commits into
dotnet:mainfrom
Roshaless:feature/mouse-horizontal-wheel
Open

Roshaless wants to merge 4 commits into
dotnet:mainfrom
Roshaless:feature/mouse-horizontal-wheel

Conversation

@Roshaless

@Roshaless Roshaless commented Sep 21, 2026 •

Copy link
Copy Markdown

Fixes #3201

Main PR N/A

Description

WPF exposes PreviewMouseWheel and MouseWheel for vertical wheel
rotation, but has no equivalent routed events for horizontal wheel
rotation.

Windows sends WM_MOUSEHWHEEL to the active window when the horizontal
scroll wheel is tilted or rotated. HwndMouseInputProvider does not
handle 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:

  • HwndMouseInputProvider handles WM_MOUSEHWHEEL alongside
    WM_MOUSEWHEEL, negates the wheel delta to match the direction
    convention, and
    reports it as
    RawMouseActions.HorizontalWheelRotate
  • MouseDevice processes RawMouseActions.HorizontalWheelRotate and
    raises PreviewMouseHorizontalWheel / MouseHorizontalWheel
  • Mouse registers PreviewMouseHorizontalWheelEvent /
    MouseHorizontalWheelEvent and exposes the Add / Remove handler
    methods
  • UIElement, UIElement3D, and ContentElement expose the events
    and OnMouseHorizontalWheel / OnPreviewMouseHorizontalWheel
    virtual methods
  • ScrollViewer, TextBoxBase, and FlowDocumentScrollViewer scroll
    horizontally in response

See: Win32 WM_MOUSEHWHEEL

Customer 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 scroll
horizontally 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.cmd in the Debug
configuration and referenced them from a separate application (WPF
Gallery) to validate the feature against a real WPF app.

Verified that:

  • WM_MOUSEHWHEEL maps to RawMouseActions.HorizontalWheelRotate
  • PreviewMouseHorizontalWheel and MouseHorizontalWheel fire in
    tunnel / bubble order
  • ScrollViewer, TextBoxBase, and FlowDocumentScrollViewer scroll
    horizontally on the new event
  • The API contract in ref/PresentationCore.cs matches the runtime
    implementation

Risk

Low.

The change adds new events and a new message handler. Existing event
routing, vertical wheel handling, and the RawMouseActions enumeration
semantics are not modified.

Microsoft Reviewers: Open in CodeFlow

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
@Roshaless
Roshaless requested a review from a team September 21, 2026 03:50
@Roshaless
Roshaless requested a review from a team as a code owner September 21, 2026 03:50
@Roshaless

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@dotnet-policy-service dotnet-policy-service Bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Sep 21, 2026
@h3xds1nz

h3xds1nz commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

This is currently not actionable as it requires an API Proposal and must go through the API Review process.

@miloush

miloush commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

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?

@Roshaless

Roshaless commented Sep 21, 2026 •

Copy link
Copy Markdown
Author

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 OnMouseHorizontalWheel implementation is copied from OnMouseWheel, which produces the wrong direction for horizontal input:

  • Scrolling down reports a negative delta and maps to ScrollInfo.MouseWheelDown() — correct.
  • Scrolling right reports a positive delta but maps to ScrollInfo.MouseWheelLeft() — inverted.
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 (wheel = -wheel) so the horizontal delta follows the same convention as the vertical wheel, or leave the raw delta alone and flip the direction inside OnMouseHorizontalWheel.

// 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"
@miloush

miloush commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

What does UWP/WinUI do?

@Roshaless

Copy link
Copy Markdown
Author

What does UWP/WinUI do?

The raw horizontal wheel delta is reported as-is, without negation.
This means the handler needs to flip the direction itself.

// Change
    if (e.Delta < 0) { ScrollInfo.MouseWheelRight(); }
    else { ScrollInfo.MouseWheelLeft(); }
// to
    if (e.Delta < 0) { ScrollInfo.MouseWheelLeft(); }
    else { ScrollInfo.MouseWheelRight(); }

@h3xds1nz

Copy link
Copy Markdown
Member

Imho we shouldn't attempt to "fix" things inside HwndMouseInputProvider.

@miloush

miloush commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like debug code, please remove it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it kind of is there for the vertical one too

@Roshaless Roshaless Sep 22, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actions = msg == WindowMessage.WM_MOUSEWHEEL ? RawMouseActions.VerticalWheelRotate : RawMouseActions.HorizontalWheelRotate;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actions = msg == WindowMessage.WM_MOUSEWHEEL ? RawMouseActions.VerticalWheelRotate : RawMouseActions.HorizontalWheelRotate;

Too long for one line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API Change Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Official mouse events for WM_MOUSEHWHEEL horizontal wheel scrolling are not defined yet

4 participants