Way to turn on keyboard's caps-lock light with

2019-07-21 15:23发布

问题:

I'm writing a program that uses caps-lock as a toggle switch. It would be nice to set the LED of the key to show that my program is on or off, like the capslock key does naturally.

I know that I could just SendInput('Capslock'); or whatever to actually turn caps-lock on and off. But my app is a typing program, and I don't want to have to deal with translating the all-caps keys that turning it on would give me into their lower/upper cases. I might go that route eventually, but not for this version.

I would however be interested in just turning on the LED light WITHOUT actually turning on caps-lock. Is there any way to do that?

Thank you.

回答1:

You can find code for changing indicators on the keyboard from C# at Faking num lock, caps lock and scroll lock leds



回答2:

I'm pretty sure you can't toggle the LED without toggling the actual Caps lock, unless you were writing a keyboard driver. (I'm not recommending that!)



回答3:

There are plugin for Miranda IM named "Keyboard Notify Ext." which contains in its source code C implementation of controlling leds. See file keyboard.c in source. Probably you can port it to C#.

Here are most interesting highlights from source code:

mir_snprintf(aux1, sizeof(aux1), "Kbd%d", i);
mir_snprintf(aux2, sizeof(aux2), "\\Device\\KeyboardClass%d", i);
DefineDosDevice(DDD_RAW_TARGET_PATH, aux1, aux2);

mir_snprintf(aux1, sizeof(aux1), "\\\\.\\Kbd%d", i);
hKbdDev[i] = CreateFile(aux1, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

...

result |= DeviceIoControl(hKbdDev[i], IOCTL_KEYBOARD_SET_INDICATORS, &InputBuffer, DataLength, NULL, 0, &ReturnedLength, NULL);