locked
How to determine if mouse or touch is being used?

    Question

  • In a puzzle game I am developing, the user manipulates puzzle pieces (UserControls) with touch or mouse and this works fine.

    But using the mouse is not intuitive or efficient for a mouse user so I'd like to detect if the user is using a mouse and show additional buttons that replicate the functionality of manipulating the puzzle pieces directly.

    The ManipulationCompleted event has PointerDeviceType in its event args which tells you if the manipulation was performed by touch, mouse, or pen.

    But I need something similar before the user does any manipulation.

    The PointerMoved event args don't offer the same PointerDeviceType and that event gets raised on touches as well as mouse move so that won't work.

    I know about Windows.Devices.Input.TouchCapabilities() but that doesn't help either because if the user is running the app on a non-touch external monitor attached to a tablet or touch laptop, TouchPresent is still == 1.

    Ideally there would be a MouseMoved event that was only raised on mouse movement. If I detected this event on my app's main page, I would show the extra ui buttons.

    Thoughts?

    Wednesday, April 30, 2014 5:21 AM

Answers

  • There is no way to tell what input the user will use before the user does so.

    You can get the pointer type from the PointerEventArgs.CurrentPoint.PointerDevice.PointerDeviceType in a PointerMoved message.

    Wednesday, April 30, 2014 12:25 PM
    Owner
  • UIElement.PointerPressed gets a PointerRoutedEventArgs object as a parameter.  You can determine the type of input like this:

    bool isDragTouchDriven = e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch;


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, April 30, 2014 12:34 PM
    Moderator

All replies

  • There is no way to tell what input the user will use before the user does so.

    You can get the pointer type from the PointerEventArgs.CurrentPoint.PointerDevice.PointerDeviceType in a PointerMoved message.

    Wednesday, April 30, 2014 12:25 PM
    Owner
  • UIElement.PointerPressed gets a PointerRoutedEventArgs object as a parameter.  You can determine the type of input like this:

    bool isDragTouchDriven = e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch;


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, April 30, 2014 12:34 PM
    Moderator
  • Thanks to Rob and Matt! I overlooked the Pointer.PointerDeviceType available in the PointerMoved event handler. That does exactly what I want. Thanks!
    Wednesday, April 30, 2014 1:39 PM
  • Glad to help! One more thing: for a global state like it sounds like you want you'll probably be better off using the CoreWindow.Pointer* events rather than the Xaml Pointer* events. The Xaml events will be better if you're targeting a specific control.

    You might want to take a look at the ScrollViewer styles and templates for an example of setting up TouchIndicator and MouseIndicator visual states to trigger based on the last Pointer* events raised.

    --Rob

    Wednesday, April 30, 2014 1:57 PM
    Owner