14

I'm using the following code to detect if a user is in tablet mode or not. I'm on a Surface Pro and when I decouple the keyboard and make the PC into a tablet, IsTabletMode returns true (which it should.) When I use the "Tablet Mode" button without decoupling the screen, IsTabletMode always returns false. Has anyone experienced this and How can I resolve it?

/*
 * Credit to Cheese Lover
 * Retrieved From: http://stackoverflow.com/questions/31153664/how-can-i-detect-when-window-10-enters-tablet-mode-in-a-windows-forms-applicatio
 */
public static class TabletPCSupport
{
   private static readonly int SM_CONVERTIBLESLATEMODE = 0x2003;
   private static readonly int SM_TABLETPC = 0x56;

   private Boolean isTabletPC = false;

   public Boolean SupportsTabletMode { get { return isTabletPC; }}

   public Boolean IsTabletMode 
   {
       get
       {
           return QueryTabletMode();
       }
   }

   static TabletPCSupport ()
   {
        isTabletPC = (GetSystemMetrics(SM_TABLETPC) != 0);
   }

   [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "GetSystemMetrics")]
   private static extern int GetSystemMetrics (int nIndex);

   private static Boolean QueryTabletMode ()
   {
       int state = GetSystemMetrics(SM_CONVERTIBLESLATEMODE);
       return (state == 0) && isTabletPC;
   }
}
T.S.
  • 14,772
  • 10
  • 47
  • 66
S. Walker
  • 2,005
  • 6
  • 25
  • Does the PC detect the keyboard while in tablet mode and the keyboard is attached? (I can't check this.) If so, you should be able to add logic based on the presence of a keyboard. – Ben J Mar 30 '17 at 15:14
  • The PC does detect the keyboard when in tablet mode. I am not looking to detect if a keyboard exists or not, I'm looking to match the theme of my application with the windows "tablet" theme. You know bigger buttons, etc. The existence of a keyboard has no bearing on this. – S. Walker Mar 30 '17 at 17:21
  • Did you try the "solution" posted in the question of the answer you've linked? – Brunner May 22 '17 at 14:20
  • Possible duplicate of [How can I detect when Window 10 enters tablet mode in a Windows Forms application?](https://stackoverflow.com/questions/31153664/how-can-i-detect-when-window-10-enters-tablet-mode-in-a-windows-forms-applicatio) – Knowledge Cube Jun 06 '17 at 19:48

1 Answers1

2

Edit 2: The SM_TABLETPC is only supported by Windows XP Tablet PC Edition and Windows Vista. There doesn't seem to be any reference to Windows 10 here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms700675(v=vs.85).aspx

You can use this: GetSystemMetrics(SM_CONVERTIBLESLATEMODE). A “0” returned means it is in tablet mode. A “1” returned means it is in non-tablet mode. https://software.intel.com/en-us/articles/how-to-write-a-2-in-1-aware-application

Can you replace the QueryTabletMode method with this:

   private static Boolean QueryTabletMode ()
   {
       int state = GetSystemMetrics(SM_CONVERTIBLESLATEMODE);
       return (state == 0);
   }

Edit: You might need to check this periodically as there's no event to see if the PC's tablet mode was turned on

SP1062
  • 31
  • 4
  • @SP1026, thank you for your response. Unfortunatly, we scrapped this feature. However, we never did solve the issue here. The problem is that Windows 10 allows users to switch to tablet mode using the actions pane at the bottom right of the screen. This means that as long as the PC supports touches, the user may switch to tablet mode at any time, even if they have a keyboard attached. I was looking for a way to detect that the PC had switched to Tablet mode, regardless of its ability to support touches, has a keyboard attached, or any other variation. – S. Walker Jan 18 '18 at 13:14
  • The code in my question does successfully test if the user has a Tablet PC, but fails to check if Windows 10 itself is in tablet mode. When I decouple my keyboard, my PC ask me if I want to switch to Tablet Mode, I always click no. This is where the code in my question fails to meet my needs. – S. Walker Jan 18 '18 at 13:17
  • Edited the answer, let me know what the GetSystemMetrics(SM_CONVERTIBLESLATEMODE) returns when in tablet mode and when not in tablet mode. – SP1062 Jan 18 '18 at 14:55