Caps Lock notification in Xubuntu


If your keyboard lacks visual lock indicators, especially for Caps Lock function, you can use a small hack to have a notification whenever the Caps Lock key is pressed. Here are the basics:

One thing to consider is having a delay before the state is being queried and only after that the notification should be displayed. First you need to have the script created. I've saved mine in my personal home directory but you can make it available for other users by placing it in a common area. They say it's a good practice to put this kind of scripts in the /opt directory so if you want to do it, you can just

sudo mkdir -p /opt/scripts
Create the script using vi:
sudo vi /opt/scripts/capslock.sh
What the script will be doing is to use xset, grep and awk to get the Caps Lock state into a shell variable. Copy and paste the following into vi:
#!/bin/bash
sleep 0.1
output=$(xset q | grep "Caps Lock:" | awk '{if ($4 == "on") {print "ON"} else {print "OFF"}}')
notify-send -t 5000 -i dialog-information "Caps Lock" "$output"
The 0.1 seconds delay is there to give time for the Caps Lock to transition its state before reading it, otherwise it will display bogus status. Save the file then quit vi.
Make the script executable:
sudo chmod ugo+x /opt/scripts/capslock.sh
Now for the graphical part.
Now you should have an indication of the Caps Lock status lasting 5 seconds. You can alter the time duration by changing the value after -t in the script.

ctrl-alt-delete.ro


The republished blog