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.
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.
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 <perform an action based on the fact that Caps Lock is off> else <perform an action based on the fact that Caps Lock is on> fi
SIGNIFICANT I BELIEVE:
Regarding accessing the keyboard, the info I've found refers to using either
/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.
Below are the websites where I found the information on keyboardleds:
The first site that put me onto using keyboardleds (with examples that are too complex for me):
What particularly interested me on this site is the comment "the program [keyboardleds] accesses the keyboard DIRECTLY."
Official documentation I believe, on keyboardleds (too complex for me to understand):
The site of the author of keyboardleds:
I have been having good luck using:
from win32con import VK_CAPITAL if GetKeyState(VK_CAPITAL) == 1: print ("CAPS Lock is on.") elif GetKeyState(VK_CAPITAL) == 0: print ("CAPS Lock is off.")
This is on a windows computer with Python3, so not 100% sure if it works on linux also, but worth noting as an option for those needing to do this on windows, this works for sure in that case - I've seen it myself.