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.

Zorawar
  • 845
  • 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 09 '15 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 ? – Sergiy Kolodyazhnyy May 09 '15 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 09 '15 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 ? – Sergiy Kolodyazhnyy May 09 '15 at 16:37
  • @Serg: Yup, that's exactly the problem. When you press 'q' a terminal (or whatever) will open and xdotool simulates another 'q' press. But then that 'q' press gets bound to opening a terminal and xdotool simulates another 'q' press... – Zorawar May 09 '15 at 17:12
  • I don't see any workaround. Even with xdotool type'q' " . You've basically got infinite loop, where you press and press and press q over and over again, launching that some_command – Sergiy Kolodyazhnyy May 09 '15 at 17:39
  • Change the shortcut perhaps to something else, like Ctrl+q – Sergiy Kolodyazhnyy May 09 '15 at 17:42
  • @Serg: Yeah, I'm using exactly a work-around like that for now, but I'd like not to. Surely it must be possible. (I'm essentially asking how a keylogger works.) I'm guessing what I want is not to bind the key to some command but instead monitor it via another running program. – Zorawar May 09 '15 at 18:05

1 Answers1

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.

Zorawar
  • 845