Ubuntu – No caps lock indicator light

16.04indicatorkeyboardkeyboard-layout

Ever since I first dual-booted Ubuntu 16.04 on my Surface Pro 3, my caps lock indicator LED on my keyboard hasn't worked. I decided to try and do something about it recently. When I type the command

echo 1 | sudo tee /sys/class/leds/input45\:\:capslock/brightness

The light comes on, so it is at least accessible. I'm not sure what to do to make the light go on or off depending on the state of caps lock though. Any help would be greatly appreciated.

Best Answer

  • Apparently, somehow the led is not set automatically. The background- patch below will take care of that:

    #!/usr/bin/env python3
    import subprocess
    import time
    
    led = "/sys/class/leds/input45::capslock/brightness"
    
    while True:
        time.sleep(1)
        ledstate = open(led).read().strip() == "1"
        capstate = "Caps Lock:   on" in \
                subprocess.check_output(["xset", "-q"]).decode("utf-8")
        if ledstate != capstate:
            newled = "0" if capstate == False else "1"
            open(led, "wt").write(newled)
    

    How to use:

    • Copy the script into an empty file, save it as fix_led (no extension) in /usr/local/bin and make it executable.
    • Since you need to edit the file /sys/class/leds/input45::capslock/brightness you need to add the script to the sudoers file, as explained e.g. here.
    • Test- run the script by running

      sudo /usr/local/bin/fix_led
      

      in a terminal, test your Caps Lock key.

    • Now add the script to your startup applications: Dash > Startup Applications > Add. Add the command:

      /bin/bash -c "sleep 10 && sudo /usr/local/bin/fix_led"
      

    That's it. On next restart (log in), it should work.

    Notes

    • Of course, the patch should work in all situations where the led is not functioning. the exact location of the file capslock/brightness may vary however. Set, if necessary, the location in the line:

      led = "/sys/class/leds/input45::capslock/brightness"
      

      in the head of the script (don't escape the : in python). I tested the script by making it set the led the wrong way :) (led off when Caps Lock was on, on when it was off).

    • The extra load of the script is none.

    Explanation

    Information on the current Caps Lock state can be fetched by the command:

    xset -q
    

    Once per second, the script checks if Caps Lock: on is in the output. The script also checks if the current state matches the led state (either 1 or 0), as read from the capslock/brightness file.

    If these two do not match, the script sets the led state according to the real Caps Lock state.