Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Distinguish between touch and mouse

Discussion in 'Input System' started by uwdlg, Feb 20, 2022.

  1. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    129
    Hi,

    for a WebGL project I want to display different control hints based on whether the user has a mouse and keyboard or a touchscreen device. Currently, I first check
    Touchscreen.current
    for null, and if so, set a (custom) enum to InputModality.Touchscreen, and afterwards do the same for
    Mouse.current
    . I always check both, because for laptops with touchscreens, I want mouse and keyboard controls to be the default (meaning the enum would first be set to Touchscreen but overwritten with MouseAndKeyboard).
    Here's the problem: on my old Samsung Galaxy Tab S3 (Android tablet) I use for testing, both Touchscreen.current and Mouse.current are not null, thus control hints for mouse and keyboard are shown.
    How should I distinguish between the two input modalities?
     
  2. AlexFCL

    AlexFCL

    Joined:
    Mar 27, 2013
    Posts:
    17
    Hi, I'm facing a similar situation for my WebGL project.

    In the editor, I check for Keyboard.current, Mouse.current, and Touchscreen.current; and also output their device ids. I get true for Keyboard and Mouse only which is correct. (keyboard id =1, mouse id =2)

    But once I built and run on my desktop browser, all test return true (including Touchscreen.current).
    When I ran the build on my mobile brower (iPhone 8 Chrome), I get the same results where all test return true (including Keyboard and Mouse).
    In both desktop browser webgl and mobile browser webgl, touchscreen id = 1, keyboard =2, mouse =3)

    It seems webgl aren't able to test these device correctly or should there be another way to detect touch vs mouse for webgl?

    @uwdlg were u able to find out how to do it?
     
    Last edited: Apr 29, 2022
  3. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    129
    Huh, that sounds even less reliable than what I get on my devices. I don't have a "good" solution, but this is what I settled on for the moment:
    Code (CSharp):
    1. public static InputModality DetectInputModality()
    2. {
    3.     if (Application.isMobilePlatform && Touchscreen.current != null) {
    4.         return InputModality.Touchscreen;
    5.     }
    6.  
    7.     if (Keyboard.current == null) {
    8.         return Touchscreen.current == null ? InputModality.MouseAndKeyboard : InputModality.Touchscreen;
    9.     }
    10.     return InputModality.MouseAndKeyboard;
    11. }
    For my devices at least, this works as I want it to. I think
    Application.isMobilePlatform
    is a more reliable way to check for a touchscreen. I don't know about special cases like touchscreen laptops or non-touchscreen smartphones (if that even still exists?) though.