7

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

2 Answers2

15

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;
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
sk_
  • 2,105
  • 17
  • 31
1

As taken from this answer

var hasTouch = Windows.Devices.Input
              .PointerDevice.GetPointerDevices()
              .Any(p => p.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch);
Community
  • 1
  • 1
Basic
  • 26,321
  • 24
  • 115
  • 201
  • As taken from my question....Windows.Devices is not coming up in c#/.Net 4.5 in VS2012Ultimate – RickInWestPalmBeach Jan 06 '15 at 17:34
  • Your question said "System.Windows.Input.Tablet". I'm not using that namespace... If this isn't working, can you clarify what you mean by "not coming up" ? No intellisense? Doesn't compile? What's the compiler error? – Basic Jan 06 '15 at 17:35
  • I tried System.Windows.Input.Tablet AND System.Windows.Devices. Neither of these are being allowed. No Intellisense...not compiling...not showing up in Object browser...Underlined in red in VS – RickInWestPalmBeach Jan 06 '15 at 17:37
  • 1
    I'm not trying to be argumentative but "not being allowed" is not a technical description of the problem. Are you getting compiler errors? If so, what are they? Have you added a reference to the `System.Windows.Input` namespace? (Right-click project, Add Reference) – Basic Jan 06 '15 at 17:38
  • "System.Windows.Input" is not showing up as an available namespace. Only "System.Windows.Input.Manipulations" – RickInWestPalmBeach Jan 06 '15 at 17:40
  • 2
    I think (although I'm not 100% positive) that `Windows.Devices.Input` is marshalling `WinRT` and I don't think it's available for `Win32` apps. – Jcl Jan 06 '15 at 17:40
  • 6
    In fact, if you check http://msdn.microsoft.com/en-us/library/windows.devices.input.pointerdevice.aspx, on "Requirements" it clearly says `Windows Store Apps Only`. This answer is clearly wrong (the linked answer is for `metro apps`) – Jcl Jan 06 '15 at 17:41
  • FYI if possible, make sure you reference the assembly....not all assemblies are referenced OOB but do check to see if it is supported on the platform in question – Ahmed ilyas Jan 06 '15 at 17:41
  • 1
    Ahmed: When I browse for the assembly, not finding System.Windows.Input or System.Windows.Devices. – RickInWestPalmBeach Jan 06 '15 at 17:44
  • JCL: Then, in other words, Microsoft, in their infinite wisdom, has not created a way to detect a touch-enabled device in Win32/Winforms applications? – RickInWestPalmBeach Jan 06 '15 at 17:45
  • @RickInWestPalmBeach notice the namespace doesn't necessarily match the assembly name where it's defined (i.e., the namespace could be `System.Windows.Input` but it could be defined in `MyInputBindings.dll`). – Jcl Jan 06 '15 at 17:45
  • @RickInWestPalmBeach I absolutely didn't say that: I don't know the answer to the question (or else I'd be answering it)... it's more likely possible. What I said is that the way this answer (the one we are commenting) proposes is wrong, since it's for Windows Store apps, and not for WinForms apps like you are requesting. – Jcl Jan 06 '15 at 17:47
  • Related: I love @HighCore's comment on [this](http://stackoverflow.com/q/25394291/2589202) question – crthompson Jan 06 '15 at 17:47
  • I just found this in the MSDN: System.Windows.Input provides types to support the Windows Presentation Foundation (WPF) input system. This includes device abstraction classes for mouse, keyboard, and stylus devices, a common input manager class, support for commanding and custom commands, and various utility classes. [Here](http://msdn.microsoft.com/en-us/library/gg145013%28v=vs.110%29.aspx) – RickInWestPalmBeach Jan 06 '15 at 17:48
  • 1
    If you want to use `System.Windows.Input`, you need to reference `PresentationCore.dll` on your project (that's the WPF main library). I would advise against this if you are doing WinForms, since there are many class names that collide and namespacing can be confusing... there's nothing preventing you to do it, but my recommendation is that you don't mix the two (at least, not in the same project) – Jcl Jan 06 '15 at 17:51
  • 1
    You can use this, which uses the Win32 api: http://stackoverflow.com/questions/5957751/is-there-a-way-to-programmatically-tell-if-a-system-is-touch-enabled – Jcl Jan 06 '15 at 17:52
  • JCL: Already tried this...Could not find how to use GetSystemMetrics. – RickInWestPalmBeach Jan 06 '15 at 17:54
  • 2
    @RickInWestPalmBeach when in doubt with using any Win32 function in .NET, I recommend http://pinvoke.net . In this case, check: http://www.pinvoke.net/default.aspx/user32.getsystemmetrics – Jcl Jan 06 '15 at 17:55
  • @JCL: I got it to work with GetSystemMetrics from pinvoke! Thanks! – RickInWestPalmBeach Jan 06 '15 at 18:18