How to create a shortcut that executes an xdotool command to simulate a key press?

The issue

Your shortcut is performed at the same time as your simulated key press

If you run a command, simulating a key press, called with ... another key press, you will actually simulate a third key press, which is the combination of both :)

To solve

You need to prevent your shortcut to run at the same time as the simulated key press, in other words, add a delay:

/bin/bash -c "sleep 1 && xdotool key XF86AudioPlay"

Add this command to a shortcut, and it should work.

Note

Of course you can play a bit with the value in sleep 1, to set it to the best value that feels comfortable, depending on how "sticky" the combination is pressed. sleep 0.5 might very well do, and make the shortcut act more promptly.


You can also use the command directly in the shortcut definition, so you do not need to create a standalone script for it.

Instead of using the "sleep 1" workaround you can use the --clearmodifiers flag, i.e. your custom shortcut would look like this:

Name: test
Command: xdotool key --clearmodifiers XF86AudioPlay

Shortcut: Ctrl+Alt+R

For me this works when mapping the shortcuts to Super+F9.


An alternative (for snappier performance) to using sleep 1 && xdotool ... is to use xdotool to release your shortcut keys. A bit lengthier, but you can also use

/bin/bash -c "xdotool keyup R Alt_L Ctrl_L && xdotool key XF86AudioPlay"