I am developing an application with C# winforms.
Our application is going to be installed on win8 surface(touch screen device).
We want to check if a keyboard is connected via USB then our app will not show soft keypad otherwise it will show.
Many methods are avaliable to check for WinRT but none for winforms C#.
Please let me know if my question is not clear.
Thanks in advance.
I just wrote this and tested on W8:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select Name from Win32_Keyboard");
foreach(ManagementObject keyboard in searcher.Get())
{
if (!keyboard.GetPropertyValue("Name").Equals(""))
{
Console.WriteLine("KB Name: {0}", keyboard.GetPropertyValue("Name"));
}
}
I also connected a second keyboard and can see it detected. When I unplug one I get one entry, when unplug both I get nothing.
I also found some examples here: Example 1 and here Example 2
Hope this helps.
To determine if it is connected via USB, check for that string:
private readonly string USB = "USB";
private bool GetKeyboardPresent()
{
bool keyboardPresent = false;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_Keyboard");
foreach (ManagementObject keyboard in searcher.Get())
{
foreach (PropertyData prop in keyboard.Properties)
{
if (Convert.ToString(prop.Value).Contains(USB))
{
keyboardPresent = true;
break;
}
}
}
return keyboardPresent;
}
Or you could instead potentially use this Powershell command:
PS C:\Users\myUserID> Get-WmiObject Win32_Keyboard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With