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?