Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if button was clicked or touched?

Is there a way to distinguish whether a button was clicked as in with a mouse or touched using a touchscreen in WPF?

like image 629
John Avatar asked Aug 09 '16 18:08

John


1 Answers

You can subscribe to PreviewMouseDown and PreviewTouchDown.

Page.xaml

<Button PreviewMouseDown="Button_PreviewMouseDown"
        PreviewTouchDown="Button_PreviewTouchDown" />

Page.xaml.cs

    private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Mouse was used.");
    }

    private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
    {
        MessageBox.Show("Touchscreen was used.");
    }

I don't believe you'll be able to access the eventargs of either in the actual click event.

If you need to perform work there as opposed to the preview events I would recommend setting an instance variable in the preview events so when you get to the click event you know where you came from.

like image 135
Derrick Moeller Avatar answered Sep 30 '22 14:09

Derrick Moeller