Microsoft-ui-xaml: Cannot discover whether a Keyboard is attached to a Surface device in a UWP app

Created on 28 Oct 2019  路  4Comments  路  Source: microsoft/microsoft-ui-xaml

As advised from this issue:
https://github.com/MicrosoftDocs/windows-uwp/issues/1977

I am now posting this here for a response:

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

  1. 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

  1. 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://docs.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?

Original SO link: https://stackoverflow.com/questions/57612875/discovering-if-a-keyboard-is-attached-to-a-surface-pro-device

question

All 4 comments

@jkennedy24 Our platform tries to encourage you to think not just about one device since your for WinUI could run on desktop, xbox, surface, hololens, etc.

I think a more "universal" way to ask your question might be: "If I were to put focus in a TextBox right now, would the software keyboard show up"?

If you had an answer to that question, would it help your scenario?

@jevansaks My issue is - when I focus a textbox the keyboard pops up and covers the textbox I am typing into.

my solution is:
if the keyboard is not attached (i.e. the popup keyboard is going to display) then show a popup page with a textbox in it to allow the user to type into.

problem
You state:

Our platform tries to encourage you to think not just about one device since your for WinUI could run on desktop, xbox, surface, hololens, etc.

I understand the point you make, but I know my app will only ever run on a desktop/surface. Its only been designed that way. For example why would Netflix ever design an application to run on both Desktop and hololens, the UI's and UX would be so different it would be crazy to think that the same app could run on both without platform specific tweaks.

Unfortunately, Microsoft aren't giving the developers the tools to deliver great apps on UWP and issues highlighted such as this one are exactly why the UX of UWP apps are flagging behind its competitors (apple and google)

if the keyboard is not attached (i.e. the popup keyboard is going to display) then show a popup page with a textbox in it to allow the user to type into.

Thanks for clarifying! This should automatically work in XAML apps. There is a feature where TextBox with focus should scroll into view and even if you don't declare a ScrollViewer there is a magic RootScrollViewer that appears at the root of the window to scroll the whole window contents if it has to.

So just to check, which OS version are you running on?

And are you able to share a repro app of the problem?

Is there a fix in place or a fix in flight? I'm still encountering this issue.

Was this page helpful?
0 / 5 - 0 ratings