2
votes

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.

2
You might want to reconsider your design. If turning on the caps lock key doesn't let users type all-caps, then your app has big usability flaws.Anon.
I used to have an MSN messenger plugin years ago that flashed the caps lock / scroll lock / etc light when I received a new IM. I typed a few times by mistake in all caps, and the plugin was quickly uninstalled.alex
I can see it now...your program crashes unceremoniously and the caps lock LED gets inverted. Then a question everyone thinks is stupid is posted on SuperUser...Jason Punyon
A System.Windows.Forms.NotifyIcon would be easier to code. Just change the Icon property to point to different icons when your program is on or off.Chris R. Timmons
@cksubs: The problem isn't how frequently one uses the caps lock, it's how infrequently we learn something new about it. The way we know what the caps lock does is because out little brains figured it out a reeeeeeeeeeeeal long time ago and it's been baked in for 10, 20...50 years or so. Changing what it does would be the equivalent of coming out with an add on for your car that (as a feature) reversed the direction of the steering wheel...Jason Punyon

2 Answers

2
votes

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!)

2
votes

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);