dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.07k stars 1.16k forks source link

MouseGesture not getting called correctly when being added to InputGestureCollection #5141

Closed SetTrend closed 2 months ago

SetTrend commented 3 years ago

Problem description:

Having created a custom command like this:

public static RoutedUICommand ZoomIn { get; } = new RoutedUICommand("Zoom in", "ZoomIn", typeof(CustomCommands), new InputGestureCollection() { new KeyGesture(Key.OemPlus, ModifierKeys.Control), new MouseWheelGesture(WheelDirection.Down, ModifierKeys.Control) });

And having written a MouseWheelGesture like described here: https://stackoverflow.com/questions/2271342/mousebinding-the-mousewheel-to-zoom-in-wpf-and-mvvm#answer-7527482

The MouseWheelGesture.Matches() method is not getting called when the mouse wheel is rotated while the CTRL key is getting pressed.

Actual behavior:

The MouseWheelGesture.Matches() method is getting called for the CTRL key is being pressed, but not for the mouse wheel being rotated.

MouseGesture not triggered

Expected behavior:

The MouseWheelGesture.Matches() method should be getting called when the mouse wheel being rotated with a MouseWheelEventArgs event argument.


Laufzeitumgebung: OS Name: Windows OS Version: 10.0.19043 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.400\

Host (useful for support): Version: 5.0.9 Commit: 208e377a53

.NET SDKs installed: 3.1.412 [C:\Program Files\dotnet\sdk] 5.0.303 [C:\Program Files\dotnet\sdk] 5.0.400 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

* Windows version: (`winver`)

21H1 Build 19043.1165

singhashish-wpf commented 3 years ago

@SetTrend Please attach your sample app where you are able to reproduce the issue.

SetTrend commented 3 years ago

@singhashish-wpf:

Please find the MCVE here: SetTrend/mousegestureissue

I added steps to reproduce to the example repository's ReadMe.md file.

SetTrend commented 2 years ago

Did anyone have the time to investigate on this issue?

SetTrend commented 1 year ago

Did anyone have the time to investigate on this issue?

h3xds1nz commented 2 months ago

@SetTrend

1) You're calling base.Matches first, but your override initializes with MouseAction.None, on which the base (MouseGesture) call will always return false.

Therefore we have two solutions: 1) Initialize with MouseAction.WheelClick instead of MouseAction.None, then your code works out of the box. 2) Stop using the base check and check yourself for Modifiers flags (using Keyboard.Modifiers as source of truth, which is what MouseGesture does) + that the event is MouseWheelEventArgs.

SetTrend commented 2 months ago

Excellent answer! Thanks a lot for enlightening me!

h3xds1nz commented 2 months ago

Welcome, only took 3 years to figure this one out, right 😄