Ubuntu – Xbindkeys launches correctly application for the first time only

bashcommand linegnomescriptsxbindkeys

When I put the following line in ~/.xbindkeysrc GNOME correctly launches application vlc.

"vlc"
   XF86Calculator

The problem is that I'd like to run a shell script. When I put the following in ~/.xbindkeysrc GNOME doesn't launch my script but calculator.

"/home/menteith/bin/audio.sh"
   XF86Calculator

Why is that? The script is executable and works as expected when is run from command line.

UPDATE
Thanks to @PRATAP I know know that when I use a compiled program in ~/.xbindkeysrc (i.e. not a bash script) and press XF86Calculator button for the first time it launches my program, but when I press XF86Calculator button for the second time I launches calculator instead.

UPDATE 2

Thanks to , @Raffa I tried yet another solution. I wrote a simple C program:

#include <stdio.h>
#include <stdlib.h>

int main() {
  puts("Starting now:");
  system("killall -s1 xbindkeys; /home/menteith/bin/audio");
  return 0;
}

It kills xbindkeys and then runs wrapper, which in turn run the script. However, it still fails to work. After second time and next times calculator is being launched.

UPDATE 3
My script switched between audio from monitor (HDMI) and audio from PC.

#!/usr/bin/env bash

output=$(pactl list cards |grep 'Active Profile:' | cut -d ':' -f 3)

if [ $output == "analog-stereo" ]; then
    out="hdmi-stereo"
else
    out="analog-stereo"
fi

echo test
# Set the choosen card profile as sink
pactl set-card-profile "alsa_card.pci-0000_00_1f.3" "output:${out}"


# Set the default sink to the new one
pacmd set-default-sink "alsa_card.pci-0000_00_1f.3.${out}" &> /dev/null

# Redirect the existing inputs to the new sink
for i in $(pacmd list-sink-inputs | grep index | awk '{print $2}'); do
    pacmd move-sink-input "$i" "alsa_card.pci-0000_00_1f.3.${out}" &> /dev/null
done

Best Answer

Step # One:

Firstly: I would suggest modifying the ~/.xbindkeysrc file by clearing the file first then please copy and paste the following code into the file replacing Key_Name with your actual key name so it will look like this:

"sh /home/menteith/bin/audio.sh && killall -s1 xbindkeys"
Key_Name

Secondly: restart xbindkeys by running the following command in the terminal:

killall -s1 xbindkeys

Finally: try the configured key and see if it works as expected.


Step # Two:

Please inspect system settings under Keyboard Shortcuts like in the image below:

enter image description here

If there is a hot key configured for Launch calculator here, then it should be disabled. Otherwise, it might interfere with xbindkeys and capture the hot-key press preventing your script from running and running the calculator instead.


Step # Three:

Another place to look into is the /usr/share/X11/xkb/symbols/pc file.

Firstly: open the file in the gedit editor by running the following command in the terminal:

sudo nano /usr/share/X11/xkb/symbols/pc

Secondly: look for a line that contains XF86Calculator in it. It will look something like this:

key  <Key_Name> {   [XF86Calculator]    };

Comment out this line by adding a // before it like this:

//key  <Key_Name> { [XF86Calculator]    };

Take note of the Key_Name in <Key_Name> and save and close the file.

Thirdly: edit your ~/.xbindkeysrc file replacing XF86Calculator with the noted Key_Name in step Secondly above. It should look like this:

"sh /home/menteith/bin/audio.sh && killall -s1 xbindkeys"
Key_Name

Save and close the file.

Fourthly: Clear the xkb settings cache by running the following command in the terminal:

sudo rm -rf /var/lib/xkb/*

Fifthly: reboot your system to activate your new xkb configuration or reload the new xkb configuration by setting an xkb map layout using the following command in the terminal:

setxkbmap -layout us

Finally: start xbindkeys by running the following command in the terminal:

xbindkeys -f ~/.xbindkeysrc

Then try your Hot-Key. If nothing happens, restart xbindkeys by running the following command in the terminal:

killall -s1 xbindkeys

Now, try your Hot-Key again and see if it works as expected.

Best Of Luck

Related Question