Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to LEDs of a USB keyboard under Windows

I'm trying to set the keyboard LEDs of a USB keyboard. I already tried / know the following:

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

  2. To set the LEDs of a truly HID compliant keyboard you can use DirectInput, SendDeviceData with the usage page 8.

  3. 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 keyboards 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?

like image 527
martin Avatar asked Nov 05 '22 00:11

martin


1 Answers

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.

like image 98
Judge Maygarden Avatar answered Nov 09 '22 09:11

Judge Maygarden