How do I use the Python module "keyboardleds" to determine Caps Lock state?
1
vote
2
answers
3694
views
1. I want my system to ascertain the state of Caps Lock (is it on or off) by
inspecting the keyboard DIRECTLY. After much searching, I understand that this
can be done in Python by using the module "keyboardleds".
I'm not familiar with Python, **so my main request is, can you tell me how to
use the module keyboardleds in a Python script, so that it tells me what the
state of Caps Lock is?** The Python version on my system is 2.7.3rc2.
2. The information I've found about "keyboardleds", is too technical for me to
understand. Also an example I found of how keyboardleds is used, uses complex
python programming that I don't understand either. Also, the example I've looked
at uses keyboardleds for one of its **other** purposes, not for the purpose I want
(ie determining the state of Caps Lock).
I don't even know what the "import" commands mean at the beginning of the
example script, and whether I need all of them.
**I would be most grateful if you could tell me what python commands I need to
use, and what detailed contents I put in them.**
3. I just need the python script to give an exit status of zero (true) if Caps
Lock is off, and an exit status of 1 (false) if Caps Lock is on.
Say the python script is called "capslockstate". Then I'll use it in a bash script as follows:
if capslockstate
then
else
fi
4. SIGNIFICANT I BELIEVE:
Regarding accessing the keyboard, the info I've found refers to using either
/dev/console
, /dev/input/event*
, and /dev/input/by-path/*-event-kbd
. On my system, all 3 of these exist. In the latter 2 cases, I've got the following on my system:
/dev/input/event0 through to /dev/input/event9.
/dev/input/by-path/platform-i8042-serio-0-event-kbd
### ie only ^this^ one item exists
The "by-path" is the **actual** directory name.
I believe that one of the above 2 "/dev/input/..." entities is what I need to use (as opposed to /dev/console
), even though I don't understand the ins and outs of it.
5. Below are the websites where I found the information on keyboardleds:
1. The first site that put me onto using keyboardleds (with examples that are too complex for me):
Alternative approaches for accessing keyboard LEDs in Linux with Python
What particularly interested me on this site is the comment "the program [keyboardleds] accesses the keyboard DIRECTLY."
2. Official documentation I believe, on keyboardleds (too complex for me to understand):
python-keyboardleds 0.3.3 documentation
3. The site of the author of keyboardleds:
python-keyboardleds — Jakub Wilk's software
Asked by dave
(11 rep)
Sep 26, 2015, 09:12 PM
Last activity: Jun 11, 2025, 03:01 AM
Last activity: Jun 11, 2025, 03:01 AM