How to check programatically if keyboard is connected or not?
Asked Answered
C

2

5

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.

Cochard answered 12/2, 2015 at 7:39 Comment(5)
@CallumLinington we have tried this but it shows list of keyboards registered with machine does not provide event if keyboard connected or not.ThanksCochard
These questions seem to be related #9931458 #11994180Holter
It may be related but I am using C#Cochard
I guess you finally will have to use P/Invoke.Holter
Partially off-topic but the default keyboard for surfaces don't connect via usbLoricate
I
7

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.

Imprison answered 12/2, 2015 at 8:16 Comment(4)
is there a way we can get event of keyboard connect/disconnect?Cochard
Did this really work? Afaik windows 8 tablets will always register as having 1 keyboard regardless of if they have a physical or not (the Surface 3 at least), or have i misunderstood something?Showiness
@Snellface, I've resolved the issue by checking for the USB value. Please see my answer.Placenta
will a bluetooth keyboard appear as a physical keyboard? Or the old bus connected keyboards? We really need a way to determine if there is a physical keyboard or just a virtual one.Subarctic
P
4

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
Placenta answered 11/6, 2018 at 19:42 Comment(1)
How does this work for laptops that have an embedded keyboard?Earreach

© 2022 - 2024 — McMap. All rights reserved.