Search code examples
embedded-linuxcapslock

Set or read Caps Lock state in Linux without using X-Windows


We are running Yocto Linux for an embedded application. I'm looking for a way to programatically control the Caps Lock state either through bash prompt or code, either set the state or read the state so that the state is known, without using X-Windows. We do not have X-Windows libraries installed and we cannot add them at this time. All the solutions that we've found searching require an X-Windows library or use of an X tool, such as xmodmap. Any help would be greatly appreciated. This seems like such a simple thing to require bringing in X-Windows into our embedded device for. Isn't there a sysfs entry we can just read?

This question is similar to How can I turn on/off Caps Lock, Scroll Lock, Num Lock key programatically on Linux, but I'm looking for a solution without requiring X-Window Libraries or utilities.


Solution

  • A little bit of background. The caps lock state is not maintained by the keyboard itself, but by whichever piece of software is managing the keyboard. The keyboard simply sends the software a code every time the caps lock key is pressed, the software determines whether that is turning caps lock on or off, sets a flag accordingly, and sends a command back to the keyboard to turn the caps lock LED on or off.

    So in this case, if you're not using X, I assume the software managing the keyboard is the Linux kernel's console. This has a number of ioctls which can be used to manage the caps lock flag, and separately the LED state. See the man page console_ioctl(4) for more details.

    There is also a setleds(1) program (man page) which can be used to easily issue these ioctls. Its part of the kbd package of tools which appears to already have a Yocto recipe.