How to Use Caps Lock LED as HDD LED Indicator in Linux

gentookeyboardlinux

I have my Caps Lock key remapped to Esc, so now I have a LED on my keyboard that is never on. I'd like to use it for something useful, like HDD or LAN activity.

Especially that I'm using a computer with a custom box & heatsinks (built into my drawer so I don't have to listen to the HDD and it doesn't take up much space), and the LEDs are not visible otherwise.

If there's a library for controlling the keyboard LEDs, I could do some coding myself, if there isn't a solution for this already.

I'm on Gentoo ~amd64.

EDIT: Ok, forget about the HDD LED. It was just an example.

I want the Caps Lock LED to light up when it's my birthday. Or when I have email. Or when I say "Caps Lock LED, please light up!".

I absolutely do not want to use an extra wire. In fact, it would be nice if this would work on wireless keyboards as well.

Best Answer

Well to change the led indicator on VT console you can use setleds. So if you're in a VT you can just type

setleds

and you'll get your current led status. If you don't want to enable numlock, just light it up you can type:

setleds -L +num 

It comes trickier in X and this is the "simplest" way to try it. Please note that X is usually ran as root so you'll either have to check permissions on X-windows tty or run it with root privileges. Usually X is tty7. /dev/console should work being the system console and by that all VTs should be affected.

sudo su -c 'setleds -L +num < /dev/tty7'

I think this will also work:

sudo su -c 'setleds -L +num < /dev/console'

here's list of light options

 [{+|-}num] [{+|-}caps] [{+|-}scroll]

If you don't have setleds in you system, my guess is that you can get it from this emerge package sys-apps/kbd.

If you are more of person who likes to code stuff here's a link to en example code to change leds in X. I did not test this, but just by looking the code looked ok.

And here's a shell script to do what you originally wanted. To have caps or other leds as HDD indicators.

#!/bin/bash

# Check interval seconds
CHECKINTERVAL=0.1

# console
CONSOLE=/dev/console

#indicator to use [caps, num, scroll]
INDICATOR=caps

getVmstat() {
  cat /proc/vmstat|egrep "pgpgin|pgpgout"  
}
#turn led on
function led_on()
{
    setleds -L +${INDICATOR} < ${CONSOLE}
}
#turn led off
function led_off()
{
    setleds -L -${INDICATOR} < ${CONSOLE}
}
# initialise variables
NEW=$(getVmstat)
OLD=$(getVmstat)
## 
while [ 1 ] ; do
  sleep $CHECKINTERVAL # slowdown a bit
  # get status 
  NEW=$(getVmstat)
  #compare state
  if [ "$NEW" = "$OLD" ]; then  
    led_off ## no change, led off
  else
    led_on  ## change, led on
  fi
  OLD=$NEW  
done
Related Question