I'm trying to set the keyboard LEDs of a USB keyboard. I already tried / know the following:
To set PS/2 keyboard LEDs you can use the CreateFile("\\.\Keybd", GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); DeviceIoControl(hKeybd, IOCTL_KEYBOARD_SET_INDICATORS...
To set the LEDs of a truly HID compliant keyboard you can use DirectInput, SendDeviceData with the usage page 8.
You can use SendInput / keybd_event. But this will also changes the state. So when you change CAPS LOCK with this method while the user is typing, he/she will get upper and lower case letters :-)
When I investigated method 2 with two keyboards, the SendDeviceData function always returns E_NOTIMPL. It seems a lot of USB keyboard manufacturers rely on the build-in HID driver. In this case the HID driver reports that keyboard as a PC enhanced keyboard ans says it's an "emulation". But the Windows HID emulation doesn't support SendDeviceData.
So I suspect there must be another way to set the keyboa开发者_StackOverflow社区rds LEDs. Windows must be talking to the USB keyboard to set the LEDs :-)
How can I change the keyboard LEDs without changing the keyboard state?
Use the HID Descriptor Tool to view the HID Report Descriptors for each keyboard. The keyboard needs to implement usage page 8 ("LEDs"). Otherwise, control of those indicators may not be available through the USB interface or it may be done in a non-standard way.
Alternatively, you could try setting the special key states with keybd_event and maybe Windows will automagically toggle the LED for you.
精彩评论