1

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.

Here is the list of things I have tried and their results:

1. How to detect if the surface keyboard is attached?

KeyboardCapabilities keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities();
     return keyboardCapabilities.KeyboardPresent != 0 ? true : false;

But this always returns true on a surface pro device as specified here: Windows 8 WinRT KeyboardCapabilities.KeyboardPresent is always true

2. How to detect if the surface keyboard is attached?

Converted the Network watcher to c#

    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) && (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;
            }
        }
    }

This returns keyboardAttached true when the onscreen keyboard shows up

3. How to detect if the surface keyboard is attached?

bool bIsDesktop = false;

var uiMode = UIViewSettings.GetForCurrentView().UserInteractionMode;
if (uiMode == Windows.UI.ViewManagement.UserInteractionMode.Mouse)          // Typical of Desktop
      bIsDesktop = true;

always returns true

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.

I'm not sure whether this link contains any information of relevance https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/keyboard-and-mouse-class-drivers

Is there a way of telling if a keyboard is attached to a surface pro device?

SomeRandomDev
  • 129
  • 11
JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • Sorry for the late reply, we are researching this question and will give you an answer when we get the result. – Richard Zhang Aug 29 '19 at 10:03
  • @RichardZhang-MSFT Thanks for the reply. I am 90% sure it's not possible to discover if you have a keyboard attached a surface pro. Which is unfortunate as it means I can't provide a good UX for my UWP app. I'd be very interrested to know if you manage to find a solution – JKennedy Aug 29 '19 at 11:50
  • Thanks for reporting this issue, the result is always returning true when we try to test the code in my surface with latest Windows 10 SDK installed to check whether the keyboard is attached or not. I am not sure if it is by design, could you please report this issue in Feedback Hub in the Windows 10, the engineering team can get your trace and help you to troubleshoot this issue, besides you can also get the latest issue update from the Feedback hub. It will be better if you can share the Feedback link in here, then we can help you to monitor this issue as well. Thanks. – Amy Peng - MSFT Aug 30 '19 at 06:56
  • Thanks Amy, reported this in the feedback hub here: https://aka.ms/AA5yp4l – JKennedy Aug 30 '19 at 08:02
  • @AmyPeng-MSFT I haven't had any response through the feedback portal. have you managed to make any progress with this issue? – JKennedy Sep 23 '19 at 10:38

0 Answers0