I made a simple bash script to display a notification whenever my capslock key is pressed. It works fine when I call it like bash capsnotify.sh.

The problem now is when I bind my above script to capslock key using xbindkeys tool it doesn't work as required. It shows a notification caps ON when my caps is on but it doesn't show caps OFF notification when my caps is off instead it again shows the caps ON notification.



value=$(xset -q | awk '/Caps/ {print $4}')

if [ "$value" == "on" ]

    notify-send "caps ON"

elif [ "$value" == "off" ]

    notify-send "caps OFF"    


"bash /home/ranveer/capsnotify.sh"
  m:0x2 + c:66

So, the problem is after binding my caps lock key on both events(on/off) it shows caps ON notification.


Probably the easiest thing to do would be to specify to xbindkeys that your script should be launched with gnome-terminal (or equivalent), for example, and minimise the size of the terminal. You could replace your entry in ~/.xbindkeysrc with:

"gnome-terminal --geometry=1x1+1141+27 --hide-menubar -e /home/ranveer/capsnotify.sh"
 m:0x2 + c:66

We gained the geometry readout of the terminal by placing it at the top right hand corner of the screen and then running xwininfo and clicking on the window. By specifying such a small terminal (1x1) and its top right hand position (1141+27) and hiding the menubar, we can ensure that a thumbnail sized terminal actually opens momentarily underneath the caps lock notify messages at the top right of the screen. It should be possible to specify the geometry like this with other terminals such as well.

After any changes to ~/.xbindkeysrc, you must enter

killall xbindkeys

and then


for the new changes to be read.

In the screenshot below, I pressed caps-lock twice very quickly so that the first notify-send message had not yet disappeared:

enter image description here

There may be other ways of solving the problem, but this is a quick reliable one that does not entail any rewriting of your script or installing other programs.


What worked for me is adding sleep before the call to my script in xbindkeys. So, now my .xbindkeysrc looks like

"sleep 0.1 && bash /home/ranveer/capsnotify.sh"

I believe it works by adding sleep because the call to xset to query the state of CapsLock happens before the Xserver has toggled the state which might be due to my window manager which grabs the key event and doesn't process it super-quickly and thus X toggles a bit later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.