Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a button click event was triggered by keyboard or mouse in WPF?

Tags:

button

events

wpf

Is there a simple way to tell what triggered Click event of a Button apart from setting multiple flags in Mouse/Key Up/Down event handlers? I'm currently only interested in distinguishing mouse from everything else, but it would be nice to handle Stylus and other input types if possible. Do I have to create my own button control to achieve this?

Edit: To clarify why I care: in this particular case I'm trying to implement "next" and "previous" buttons for a sort of picture viewer. Pictures in question may be of different size and buttons' positions will change (so they are always centered below picture). It's quite annoying to follow such buttons with mouse if you need to scroll through several pictures, so I want to keep mouse position constant relative to clicked button, but only if it was clicked by mouse, not keyboard.

Edit2: It does not matter whether the buttons are on top or down at the bottom, since the center can change anyway. "Picture viewer" here is just an abstraction and in this particular case it's important for me that top left corner of the picture retains it's position, but it's out of the scope of the question to go in details. Scaling the picture is not so trivial in this sort of application as well, so I do want to know the answer to the question I asked not going into UI implementation discussion.

like image 518
Stanislav Kniazev Avatar asked Mar 02 '23 03:03

Stanislav Kniazev


1 Answers

if (InputManager.Current.MostRecentInputDevice is KeyboardDevice)
like image 102
Nir Avatar answered Mar 04 '23 11:03

Nir