Search code examples
c#wpfsilverlightc#-4.0touch

How to identify the input pointer whether it is mouse or finger on TouchScreen in C# WPF?


In my application, i would like to process both mouse and Touch events for smoothness. But here i allowed my code such as mouse event should work only when input pointer is mouse and touch events will work only when input pointer is finger on touch screen(Like Outlook 2013 on touch devices) . How to achieve this? or If any built-in events available to identify the input pointer means please tell me.

Please provide your suggestion to me.


Solution

  • WPF offers different types of input events (e.g.: MouseLeftButtonDown, StylusDown, TouchDown). All these events have associated event arguments that let you access the input device that caused the interaction (e.g: MouseEventArgs.Device, etc.).

    Update:

    The Mouse events fire for an actual computer mouse and input by stylus alike. If you need to identify touch input you could utilize the Preview variant of the Stylus related events (e.g. PreviewStylusButtonDown). Then in the correspondig event handler you could set a global variable (of type bool) indicating that a touch input is being processed. Accessing this variable in the mouse event handler let's you invoke code depending on what the input type is. Finally don't forget to 'unset' the Boolean when the interaction has finished, when using this approach.

    Alternatively you can also just put code directly in the handler for StylusButtonDown, in which case you need to be aware that the mouse event is still being fired.