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.

2intor
  • 1,044
  • 1
  • 8
  • 19
  • @CallumLinington we have tried this but it shows list of keyboards registered with machine does not provide event if keyboard connected or not.Thanks – 2intor Feb 12 '15 at 07:42
  • These questions seem to be related http://stackoverflow.com/questions/9930958/win32-determining-when-keyboard-is-connected-disconnected http://stackoverflow.com/questions/11993680/detect-keyboard-presence-in-windows-8-desktop-program – Codor Feb 12 '15 at 07:42
  • It may be related but I am using C# – 2intor Feb 12 '15 at 07:43
  • 2
    I guess you finally will have to use P/Invoke. – Codor Feb 12 '15 at 07:44
  • Partially off-topic but [the default keyboard](http://www.microsoft.com/surface/en-us/support/hardware-and-drivers/touch-cover-the-touch-keyboard) for surfaces don't connect via usb – Sayse Feb 12 '15 at 08:11

2 Answers2

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.

callmebob
  • 6,128
  • 5
  • 29
  • 46
  • is there a way we can get event of keyboard connect/disconnect? – 2intor Feb 12 '15 at 08:42
  • 1
    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? – Snellface Nov 12 '15 at 15:30
  • @Snellface, I've resolved the issue by checking for the USB value. Please see my answer. – user8128167 Jun 11 '18 at 19:40
  • 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. – Garr Godfrey Nov 01 '21 at 20:57
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
user8128167
  • 6,929
  • 6
  • 66
  • 79