Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if a Windows device is touch-enabled

How do I detect if a device is touch-enabled in C# for a WinForms app (Not WPF).

  • I found information on GetSystemMetrics. But I can't find how to use this in C#.

  • I tried using System.Windows.Input.Tablet class. But it's not coming up in C#, even though I am using .NET Framework 4.5.

  • I tried using System.Windows.Devices. But it's not coming up in C#, even though I am using .NET Framework 4.5.

  • I have also checked Detect whether a Windows 8 Store App has a touch screen and How to detect a touch enabled device (Win 8, C#.NET), which would seem to make this question a duplicate. However, neither of these answers my question.

like image 220
RickInWestPalmBeach Avatar asked Jan 06 '15 17:01

RickInWestPalmBeach


2 Answers

GetSystemMetrics seems to be the right way to go. It should be accessed through P/Invoke like this:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);

public static bool IsTouchEnabled()
{
     const int MAXTOUCHES_INDEX = 95;
     int maxTouches = GetSystemMetrics(MAXTOUCHES_INDEX);

     return maxTouches > 0;
}
like image 172
sk_ Avatar answered Sep 28 '22 03:09

sk_


As taken from this answer

var hasTouch = Windows.Devices.Input
              .PointerDevice.GetPointerDevices()
              .Any(p => p.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch);
like image 42
Basic Avatar answered Sep 28 '22 01:09

Basic