Search Unity

Detect touchscreen

Discussion in 'Windows' started by stationx, Dec 22, 2013.

  1. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    So here is the situation:
    Surface tablets do support touch and keyboard / mouse.
    A laptop running windows 8 without touchscreen does not support touch, but keyboard and mouse.

    If you build a windows 8 game, it will be available on both surface and laptop.
    So, my question; how can one detcct if current device (laptop or surface) has got touchscreen?
    (I kn ow application.platform, but that doesn't covers something like 'metrodesktop' or 'metrosurface'.)
     
  2. Kenma

    Kenma

    Joined:
    Oct 23, 2013
    Posts:
    1
    I think you can implement both keyboard mouse input or touch screen accelerator input. Then no matter the current device got touch screen or not, the application can get correct input.

    Sample code:

    (1) For keyboard and accelerator:
    if (Input.GetKey(KeyCode.LeftArrow)) {
    //Do something while press left arrow
    }
    else if (Input.GetKey(KeyCode.RightArrow)) {
    //Do something while press right arrow
    }
    else {
    // Get the input of accelerator
    InputX = Input.acceleration.x;
    }
    }
    (2) For touch screen and mouse:
    Work in the same way with following events:

    public override void OnTouchBegan(UITouch touch) {
    ...
    }

    public override void OnTouchMoved(UITouch touch) {
    ...
    }

    public override void OnTouchEnded(UITouch touch) {
    ...
    }



    P.S. First time to reply and answer question, hope it can help you.
     
  3. mantler

    mantler

    Joined:
    Oct 30, 2013
    Posts:
    47
    Kenma is correct in that you don't want different code for tablets and laptops... you want to be able to support both touch and keyboard/mouse regardless of platform, and simply respond to the appropriate events. (Also, some laptops also support touch screens.)
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
  5. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
  6. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    Thnx for the replys!
    It is not really an issue of having code for both mouse and touch, but more that a desktop version should look more like a game that that can be played without touch. E.g. smaller buttons or disable some touch stuff at all. For instance, in my game, I have splitscreen modus, that only makes sense on a tablet and not on a desktop, so I want to disable that at all and give the gamer on desktop not that choice.
     
  7. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    heey, I got it working...this seems to be the solution...

    #if !UNITY_EDITOR UNITY_METRO

    Windows.Devices.Input.TouchCapabilities TouchCapabilities = new Windows.Devices.Input.TouchCapabilities();
    if(TouchCapabilities.TouchPresent != 0){
    numberOfTouches = TouchCapabilities.TouchPresent;
    isTouchy = true;
    }
    #endif
     
  8. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    There is now some 2x1 Intel Notebooks, where you can rotate the screen to change the Notebook to a Tablet.

    Is it possible in runtime to detect if It is running on "Notebook mode" or "Tablet Mode"? How?
     
  9. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
  10. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
  11. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I was not able to make it work! Any advice anybody?

    What I need more is to detect if the mouse available now or not.... When I change the Ultrabook to Tablet Mode, Unity hide the hardware mouse and lock the mouse position, but I'm using a custom mouse (a gameobject) and I was not able to detected this change.
     
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
  13. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    Hi Aurimas! Thanks for the answer!

    I tested it with TouchScreenKeyboard-isSupported.html but on my Ultrabook it always returns me true! I'm still not able to detect when the user is using it on Tablet or Notebook Mode.

    There is something else I can try?
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    There's also Input.mousePresent property. Maybe that works better?

    Unfortunately, it's kinda hard to define the boundaries of being a tablets or a laptop of such a device...
     
  15. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    I think I finally was able to solve it! :)

    Code (csharp):
    1.  
    2.     [DllImport("user32.dll")]
    3.     public static extern int GetSystemMetrics(SystemMetric smIndex);
    4.  
    5. public enum SystemMetric
    6. {
    7.     SM_CONVERTABLESLATEMODE = 0x2003,
    8.     SM_SYSTEMDOCKED = 0x2004,
    9. }
    10.  
    11.         if ((GetSystemMetrics(SystemMetric.SM_CONVERTABLESLATEMODE) == 0) && (TouchScreenKeyboard.isSupported))
    12.         {
    13.             GameCursor.gameObject.SetActive(false);
    14.             Debug.Log("slate/tablet mode");
    15.         }
    16.         else
    17.        {
    18.             GameCursor.gameObject.SetActive(true);
    19.             Debug.Log("docked/laptop mode");
    20.         }
    21.  
     
  16. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    Everything worked, but now I failed on Windows Store App Validation.

    GetSystemMetrics in user32.dll is not supported for this application type.
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
  18. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Hey @Wagenheimer, did you continue work on this? Is it still working, even if you can't publish on the Windows Store? I'm investigating the topic and would love a way to detect the convertible state.
     
  19. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    Hi @Martin Sharkbomb!

    I don't tested it yet, but I just found this :

    https://msdn.microsoft.com/en-us/magazine/mt590976.aspx

    Code (csharp):
    1.  
    2. public static bool IsWindows10UserInteractionModeTouch
    3. {
    4.   get
    5.   {
    6.   bool isInTouchMode = false;
    7. #if UNITY_WSA_10_0 && NETFX_CORE
    8.   UnityEngine.WSA.Application.InvokeOnUIThread(() =>
    9.   {
    10.   isInTouchMode =
    11.   UIViewSettings.GetForCurrentView().UserInteractionMode ==
    12.   UserInteractionMode.Touch;
    13.   }, true);
    14. #endif
    15.   return isInTouchMode;
    16.   }
    17. }
    18.  
    Please let me know if it worked for you!
     
  20. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Based on your code I built this, which seemed to work. I don't have a 2-in-1 device, but a friend of mine tested it and confirmed it. So that's good.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Runtime.InteropServices;
    3. public class SlateModeDetect : MonoBehaviour {
    4.     [DllImport("user32.dll")]
    5.     public static extern int GetSystemMetrics(SystemMetric smIndex);
    6.     public enum SystemMetric
    7.     {
    8.         SM_CONVERTABLESLATEMODE = 0x2003,
    9.         SM_SYSTEMDOCKED = 0x2004,
    10.     }
    11.     public enum ConvertibleMode { LaptopDockedMode, SlateTabletMode };
    12.     public ConvertibleMode currentMode;
    13.     void Update() {
    14.         if (GetSystemMetrics(SystemMetric.SM_CONVERTABLESLATEMODE) == 0 && TouchScreenKeyboard.isSupported) {
    15.             if (currentMode == ConvertibleMode.SlateTabletMode)
    16.                 return;
    17.          
    18.             currentMode = ConvertibleMode.SlateTabletMode;
    19.             Cursor.visible = false;
    20.             Cursor.lockState = CursorLockMode.Locked;
    21.             Debug.Log("slate/tablet mode");
    22.         }
    23.         else if (currentMode != ConvertibleMode.LaptopDockedMode) {
    24.             currentMode = ConvertibleMode.LaptopDockedMode;
    25.             Cursor.visible = true;
    26.             Cursor.lockState = CursorLockMode.None;
    27.             Debug.Log("docked/laptop mode");
    28.         }
    29.     }
    30. }
    Also great find on that link. Need to give that a try sometime.
     
    sevensails likes this.
  21. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    @Martin Sharkbomb I just tested your code on a real device and it works like a charm! Thanks for sharing it!
     
  22. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Always happy to share :D
     
  23. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    @Martin Sharkbomb
    Was you able to publish your APP to Windows Store?

    Using this code, the validation failed with the error :


    Supported APIs
    • Error Found: The supported APIs test detected the following errors:
      • API GetSystemMetrics in user32.dll is not supported for this application type. Assembly-CSharp.dll calls this API.
    • Impact if not fixed: Using an API that is not part of the Windows SDK for Windows Store apps violates the Windows Store certification requirements.
     
  24. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    GetSystemMetrics is not allowed to be used on Windows Store applications. User32.dll doesn't even exist outside of desktop: you'll crash (or get an exception) if you try to call it on a phone, hololens or xbox.
     
  25. sevensails

    sevensails

    Joined:
    Aug 22, 2013
    Posts:
    483
    @Tautvydas Zilys Is there an alternative to detect if the 2x1 is currently on Desktop or Tablet mode?
     
  26. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
  27. Moises_mlg

    Moises_mlg

    Joined:
    Jul 29, 2017
    Posts:
    2