Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

capsnotify.sh

#!/bin/bash

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

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

then    
    notify-send "caps ON"

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

then    
    notify-send "caps OFF"    
fi

.xbindkeysrc

"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.

share|improve this question
add comment

2 Answers

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

xbindkeys

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.

share|improve this answer
add comment
up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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