WPF: How to detect if a mouse event was raised by a mouse, touch or stylus/pen input

If you've ever tried to detect if a given input was caused by mouse, touch or stylus/pen, you've probably noticed that it seems easy at first - WPF provides events for mouse, touch and stylus/pen input so just use those to detect what type of input was used right? Well unfortunately it's not as simple as that - see, for compatibility reasons, touches also raise stylus/pen events and mouse events in addition to touch events and stylus/pen input also raises mouse events in addition to stylus/pen events. This means that mouse events can be caused by mouse, touch or stylus/pen input and stylus events can be caused by stylus/pen input and touch input. Luckily, the arguments passed into mouse events can be used to detect if it was caused by mouse, touch or stylus/pen input, though, finding this information can be difficult if you don't know where to look.

How to detect if a mouse event was raised by mouse, touch or stylus/pen input

One of the arguments passed into mouse events is MouseEventArgs.StylusDevice. This contains information about the stylus that caused the event - but what if a mouse, not a stylus/pen caused the event? Well, in that case, StylusDevice will be null/Nothing. Because of this, you can detect if a mouse was used to trigger the event by detecting if StylusDevice is null/Nothing, like so (inside an event handler for a mouse event, such as PreviewMouseUp):

C#:

if (e.StylusDevice == null)

{

    MessageBox.Show("Mouse");

} else

{        

    MessageBox.Show("Touch or Stylus/Pen");           

}


VB:

If e.StylusDevice Is Nothing Then

    MessageBox.Show("Mouse")

Else

    MessageBox.Show("Touch or Stylus/Pen")

End If


So, if StylusDevice is not null or Nothing, that means that the event was triggered by either touch or stylus/pen input. Luckily, StylusDevice.TableDevice contains the information we need to distinguish between the two. The TabletDevice class contains information about the digitizer associated with the current StylusDevice. From here, we can check if TabletDevice.Type is Touch or Stylus to determine if touch input or stylus/pen input caused the event.

C#:

if (e.StylusDevice.TabletDevice.Type == TabletDeviceType.Touch)

{

    MessageBox.Show("Touch");

} else

{

    MessageBox.Show("Stylus/Pen");

}


VB:

If (e.StylusDevice.TabletDevice.Type == TabletDeviceType.Touch) Then 

    MessageBox.Show("Touch")

Else

    MessageBox.Show("Stylus/Pen")

End If



So, using the following code in a mouse event handler for an event such as PreviewMouseUp, we can determine if mouse, touch or stylus/pen input caused the event:

C#:

private void Window_PreviewMouseUp(object sender, MouseButtonEventArgs e)

{

    if (e.StylusDevice == null)

    {

        MessageBox.Show("Mouse");

    } else

    {

        if (e.StylusDevice.TabletDevice.Type = TabletDeviceType.Touch)

        {

            MessageBox.Show("Touch");

        } else

        {

            MessageBox.Show("Stylus/Pen");

        }

    }

}


VB:

Private Sub window_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles window.PreviewMouseUp

    If e.StylusDevice Is Nothing Then

        MessageBox.Show("Mouse")

    Else

        If (e.StylusDevice.TabletDevice.Type = TabletDeviceType.Touch) Then

            MessageBox.Show("Touch")

        Else

            MessageBox.Show("Stylus/Pen")

        End If

    End If

End Sub



Testing this on a device that supports mouse, touch and stylus/pen input, we can see that it successfully determines which type of device caused the event (specifically, this was tested with PreviewMouseUp (code above):




Tip: Set Switch.System.Windows.Input.Stylus.EnablePointerSupport to true in your app? We tested and this method usually still works with that feature on but could be a bit buggy at times as this feature can be buggy, for example, sometimes when using touch, it detected it as mouse. So if you have that feature enabled, we'd recommend making sure to thoroughly test to ensure that this method works correctly for you or turning off the feature. No issues were observed when this feature was disabled.

This snippet is available in Codly. Click the download link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store.

Did this guide help you? Do you have anything to add? Do you have any questions? Let us know in the comments! Also, if this guide did help you and you use an AdBlocker and you want to support us, consider disabling it for this site - if you do, it will be greatly appreciated!

Follow us to be notified when we release new guides:

Comments

Popular posts from this blog

How to show placeholder text in a WPF TextBox in C# and VB

How to enable dark title bar in WPF and Windows Forms/WinForms apps in C# and VB

How to use modern icons in XAML in WPF on Windows 10 and 11

How to change the colour of a WPF or Windows Forms/WinForms title bar in Windows 11 in C# and VB

Microsoft WebView2: How to check if the WebView2 runtime is installed in C# and VB