title: Discovering if a keyboard is attached to a Surface Pro device
body: <p>I have a UWP app running on a surface pro device and I am trying to tell if a keyboard is attached to the device (this is the surface pro keyboard so attaches to the bottom, not usb) so that my application can go down different code paths.</p>
  
  <p><strong>Here is the list of things I have tried and their results:</strong></p>
  
  <p><strong>1.</strong>
  <a href="https://stackoverflow.com/questions/20124702/how-to-detect-if-the-surface-keyboard-is-attached">How to detect if the surface keyboard is attached?</a></p>
  
  <pre><code>KeyboardCapabilities keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities();
       return keyboardCapabilities.KeyboardPresent != 0 ? true : false;
  </code></pre>
  
  <p>But this always returns true on a surface pro device as specified here: <a href="https://stackoverflow.com/questions/12841218/windows-8-winrt-keyboardcapabilities-keyboardpresent-is-always-true/14050531">Windows 8 WinRT KeyboardCapabilities.KeyboardPresent is always true</a></p>
  
  <p><strong>2.</strong> <a href="https://stackoverflow.com/questions/20124702/how-to-detect-if-the-surface-keyboard-is-attached">How to detect if the surface keyboard is attached?</a></p>
  
  <p>Converted the Network watcher to c#</p>
  
  <pre><code>    public bool KeyboardAttached { get; set; }
  
      private void SetupKeyboardWatcher()
      {
          var watcher = Windows.Devices.Enumeration.DeviceInformation.CreateWatcher();
          watcher.Added += Watcher_Added;
          watcher.Updated += Watcher_Updated;
          watcher.Removed += Watcher_Removed;
          watcher.Start();
      }
  
  private void Watcher_Updated(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformationUpdate args)
  {
      if (args.Id.IndexOf("{884b96c3-56ef-11d1-bc8c-00a0c91405dd}") != -1)
      {
          if (args.Properties.ContainsKey("System.Devices.InterfaceEnabled"))
          {
              // keyboard is connected 
              KeyboardAttached = true;
          }
          else
          {
              // keyboard disconnected
              KeyboardAttached = false;
          }
      }
  }
  
  private void Watcher_Added(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformation args)
  {
      if ((args.Id.IndexOf("{884b96c3-56ef-11d1-bc8c-00a0c91405dd}") != -1) &amp;&amp; (args.Id.IndexOf("MSHW0007") == -1))
      {
          if (args.Properties.ContainsKey("System.Devices.InterfaceEnabled"))
          {
              // keyboard is connected 
              KeyboardAttached = true;
          }
      }
  }
  
      private void Watcher_Removed(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformationUpdate args)
      {
          if (args.Id.IndexOf("{884b96c3-56ef-11d1-bc8c-00a0c91405dd}") != -1)
          {
              if (args.Properties.ContainsKey("System.Devices.InterfaceEnabled"))
              {
                  // keyboard is connected 
                  KeyboardAttached = true;
              }
              else
              {
                  // keyboard disconnected
                  KeyboardAttached = false;
              }
          }
      }
  </code></pre>
  
  <p>This returns <code>keyboardAttached</code> true when the onscreen keyboard shows up</p>
  
  <p><strong>3.</strong> <a href="https://stackoverflow.com/questions/20124702/how-to-detect-if-the-surface-keyboard-is-attached">How to detect if the surface keyboard is attached?</a></p>
  
  <pre><code>bool bIsDesktop = false;
  
  var uiMode = UIViewSettings.GetForCurrentView().UserInteractionMode;
  if (uiMode == Windows.UI.ViewManagement.UserInteractionMode.Mouse)          // Typical of Desktop
        bIsDesktop = true;
  </code></pre>
  
  <p>always returns true</p>
  
  <p>Outside of my app the Windows OS acts differently depending on whether the keyboard is attached or not (it pops up a on screen keyboard) so there must be a way of doing it.</p>
  
  <p>I'm not sure whether this link contains any information of relevance <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/keyboard-and-mouse-class-drivers" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/keyboard-and-mouse-class-drivers</a></p>
  
  <p>Is there a way of telling if a keyboard is attached to a surface pro device?</p>