简体   繁体   中英

Get HID Keyboard Device Using Raw Input from C# in Windows 8

I read article Using Raw Input from C# to handle multiple keyboards and download the source . It works when I run it in Windows 7, but failed when I run it in Windows 8.

I've got "Object reference not set to an instance of an object." error on

string deviceClass = (string)OurKey.GetValue("Class");
if (deviceClass.ToUpper().Equals("KEYBOARD"))

I placed breakpoint at string deviceClass and see the value is null. If you look at the downloaded source the value is obtained from the registry. Is there any difference between Windows 7 and Windows 8 to obtain this value? How do I solve this?

That code expects a Class value to be present in the appropriate HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\HID\\ key, on my Windows 8 machine none of the entries have this key so you will get the behaviour you describe.

That is a poor way to get the class even if the key did exist; instead p/invoke GetRawInputDeviceInfo() for the device class.

This is due to the Class REG_SZ value no longer used in Windows 8 and higher.

You can use the Service value instead but make sure to search for the appropriate data kbdhid

string deviceClass = (string)OurKey.GetValue("Service");
if (deviceClass.Equals("kbdhid"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM