2

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
menteith
  • 271

2 Answers2

1

Workaround

Tried it on Ubuntu 19.10

contents of myscript.sh file.

#!/bin/bash

notify-send "Script Ran Successfully"

contents of .xbindkeysrc file.

"/bin/bash $HOME/myscript.sh"
XF86Calculator

Once you logged in,

run xbindkeys --poll-rc. the key XF86Calculator should run your script.

Reference for --poll-rc option https://wiki.archlinux.org/index.php/Xbindkeys and see the comment below the answer in this link https://askubuntu.com/a/1126410/739431

enter image description here

PRATAP
  • 22,460
1

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

Raffa
  • 32,237
  • Thanks for this. Unfortunately, it didn't do the trick. You are very welcome to come up with another solution. Btw, Please see my updated question. – menteith Nov 07 '19 at 18:36
  • I forgot to mention: I tried you solution but with no luck. – menteith Nov 07 '19 at 19:27
  • I've updated my question. – menteith Nov 07 '19 at 20:33
  • F3 works fine (with my C program). BTW, when xev is running, it doesn't show output (print keycode) when I press XF86Calculator key. – menteith Nov 07 '19 at 21:15
  • I don't have FF installed but I tried google-chrome-stable (if needed I can try with FF, thought). Calculator is launched instead of Chrome (I tried google-chrome-stable as well as full path to the executable). – menteith Nov 07 '19 at 21:38
  • I would rather say thank you for your patience. I had (and still have) disabled hot key that launches calculator. – menteith Nov 07 '19 at 21:47
  • Thank you very much for your patience and help. Much appreciated! – menteith Nov 07 '19 at 22:01
  • Upvoted as it almost does the trick - it works but after n presses (10, 20, cannot tell) it stops and runs calculator. – menteith Nov 12 '19 at 21:45