pythoncapslock

Python - How to get current keylock status?


I'm attempting to write a simple programme that displays the current status of the different keylocks, but I'm unable to find a solution as to how to get the current status of them in Python. Thank you.


Solution

  • └──> xset q | grep LED
      auto repeat:  off    key click percent:  0    LED mask:  00000000
    └──> xset q | grep LED
      auto repeat:  off    key click percent:  0    LED mask:  00000001
    

    When the caps lock is on, the LED mask should be 1 and if the LED mask is off, it should be 0.

    Additionally since you mentioned that you wanted to use python, you could get the value in the following way

    >>> import commands
    >>> # Caps Lock is off.
    >>> commands.getoutput('xset q | grep LED')[65]
    '0'
    >>> # Setting Caps Lock on now.
    >>> commands.getoutput('xset q | grep LED')[65]
    '1'
    

    python 3 version:

    import subprocess
    if subprocess.check_output('xset q | grep LED', shell=True)[65] == 50 :
        capslock = False
    if subprocess.check_output('xset q | grep LED', shell=True)[65] == 51 :
        capslock = True
    print( "capslock ON is : ", capslock )