3

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.

Leandro
  • 33
  • Hi Leandro, did you notice the answer? – Jacob Vlijm Nov 20 '16 at 15:02
  • if the answer solved your problem, as you indicated, would you be so kind to accept the answer (tick the big "V" below the up/down arrows on the left). It is the appropriate way to indicate the answer worked for you. – Jacob Vlijm Nov 20 '16 at 16:54

1 Answers1

2

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.

Jacob Vlijm
  • 83,767
  • This worked perfectly! Thanks for the great response :) – Leandro Nov 20 '16 at 06:19
  • The only problem is that the input number changes. After I restarted it went from input20::capslock to input19::capslock. Is there some way to identify the file regardless of its name? – Leandro Nov 20 '16 at 06:31
  • Nevermind, I was able to pick the file based on its location in the directory using os.listdir(path). Thanks again! – Leandro Nov 20 '16 at 06:46