7

Is it possible to bind a (global) key press to some command and still not disrupt the key press from completing? What I mean is, if I try the bindkey solution posted elsewhere here:

# In file: ~/.xbindkeysrc
# Bind key 'q' to running 'some_command'
"some_command"
  q

then the key press 'q' never completes as it otherwise would do: i.e., never prints the character 'q' on the terminal, for example.

Using xdotool to send a 'q' key press like this:

# In file: ~/.xbindkeysrc
# Bind key 'q' to running 'some_command'
"some_command && xdotool key q"
  q

results in loop since the 'q' key press executed by xdotool will execute another 'some_command' via the binding.

To be a little clearer, I want the key press 'q' to execute as it normally does and in addition execute some external command. The solution above replaces the 'q' key press event with executing some external command. The problem is that if that external command also simulates a 'q' key press, then the binding re-launches the external command and I get stuck in an infinite loop.

9
  • possible duplicate of Associating Multiple Commands with one key in .xbindkeys May 9, 2015 at 16:08
  • 3
    @Serg: I think that Q is not really the same as mine because, well, I know how to run two commands on the same line! Also, I think the solution will not involve running two or more commands after the key has been bound because the loop condition will remain. What I think I need is a completely different solution. I think.
    – Zorawar
    May 9, 2015 at 16:14
  • So, what exactly you're trying to achieve ? some_command launches some applications and you're trying to make it start with q pressed ? May 9, 2015 at 16:18
  • @Serg: yup, but I want the 'q' press to also continue. As it is, some_command replaces the q press. I want the q press to be executed as normal in addition to some_command also being executed. Maybe this wasn't clear in the Q...
    – Zorawar
    May 9, 2015 at 16:21
  • Ok, so what I did is I bound "gnome-terminal && xdotool key q" to q and now I have infinitely spawning gnome-terminal. Is that what you're experiencing ? May 9, 2015 at 16:37

1 Answer 1

7

OK, so I will post a solution that I've found, but maybe someone else has a better one. Following an answer presented here, I can see all the keys pressed by running

xinput test <keyboad_id>

in a terminal. It's then just a simple case of piping the output of that command into a program that watches for strings like "key release 24" (the output when 'q' is released on my keyboard) and which will then do whatever you like when it does match this string.

For example, we can catch a press of the 'q' key and make a sound like this:

xinput test <keyboard_id> | while read in ; do
  [[ $in = "key press   24" ]] && aplay /usr/share/sounds/purple/alert.wav
done

We can then, obviously, watch for other inputs as well and run something else if desired.

2
  • This is I guess the best solution unless you want to write your own. May 14, 2015 at 14:21
  • @don_crissti: whoa, nice link! Thanks! I might just do that in fact. It looks like a quick way to learn something interesting!
    – Zorawar
    May 15, 2015 at 3:23

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.