Search code examples
c#windowswinformsc#-4.0pinvoke

How to check programatically if keyboard is connected or not?


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.


Solution

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