AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.33k stars 2.2k forks source link

Check for modifier keys in Click event #3318

Open mat1jaczyyy opened 4 years ago

mat1jaczyyy commented 4 years ago

Example use case: we want slightly different behavior when the user Ctrl+clicks a Button rather than just clicking it without holding a modifier key. Since Click event is raised when keyboard is used to trigger the Button as well, hacking around this with pointer and key events causes a lot of boilerplate code for little reason.

In WPF this is trivial to check for in any piece of code.

kekekeks commented 4 years ago

KeyModifiers should be passed with the event. There is no guarantee that some static property value has any correlation with particular event instance, so WPF way is a bad API design.

derekantrican commented 3 years ago

Is this resolved via https://github.com/AvaloniaUI/Avalonia/commit/f0e154ac90299e6f46ff280bb920353454a7f345?

I'm using MVVM architecture so I'm wondering if there is a way to check for a modifier key when executing the Command on a Button

maxkatz6 commented 3 years ago

@derekantrican I don't see if any properties were added to Click event EventArgs.

For MVVM you can use "Avalonia Behavior SDK" (example but UWP). You can also create custom behaviors.

But personally I don't like idea of "0 code in the code behind", because it isn't what MVVM is about. And I believe it is fine to use code behind for View-only logic (including UI-event handlers with its filtering).

derekantrican commented 3 years ago

Note that I was able to do this thanks to @wieslawsoltes 's sample code on https://github.com/wieslawsoltes/AvaloniaBehaviors/issues/73

bodong1987 commented 1 year ago

KeyModifiers should be passed with the event. There is no guarantee that some static property value has any correlation with particular event instance, so WPF way is a bad API design.

That's not a good api design, but it's very useful and easy to avoid some weird bugs. I have seen that in some cases, it is achieved by paying attention to the KeyDown and KeyUp events, but in many cases, if the control does not get the focus, they cannot get the message notification, so a series of problems will arise.