1
  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
       <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
    
  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):

      https://stackoverflow.com/questions/15625367/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):

      https://python-keyboardleds.readthedocs.org/en/0.3.3/

    3. The site of the author of keyboardleds:

      http://jwilk.net/software/python-keyboardleds

  • why python...? if it is a linux system with the basic util-linux package installed, then you probably want setleds. – mikeserv Sep 26 '15 at 21:44
  • Please log in properly. If you're having problems with that, see here – terdon Sep 27 '15 at 15:05
0

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.

Randy

  • As the module names suggest, win32api and win32con are Windows exclusive and are not available on *NIX. I do think, however, the answer gives a good insight into how that is done on other operation systems. – undercat Mar 14 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.