I'm trying to create some scripts using xev and xdotool to allow me to record and re-play macros in linux. I've got the recording working with xev, and the replaying mostly works but appears to leave my X session in a weird state. I've got a macro file like this:

keydown e
keyup e
keydown c
keydown h
keyup c
keydown o
keyup h
keyup o
keydown space
keyup space
keydown apostrophe
keyup apostrophe
keydown h
keydown i
keyup h
keyup i
keydown apostrophe
keyup apostrophe

and I'm trying to use it with cat macro.txt | xargs xdotool. This types out my text in the terminal, but after it finishes, most of my keys don't work anymore; they either print out control characters, or they just don't do anything. I've had to restart my computer a couple times to get it re-set. Any idea how I can avoid this or what is going on? Or even what a good linux macro tool might be?

  • Does the sample you posted always cause that weird state, or only sometimes? Run xev from a terminal and, when the keys don't work, type one of the keys in the xev window and copy-paste the paragraph that begins with “KeyPress event”. Try pressing and releasing modifiers, in case the problem was a stuck modifier. – Gilles 'SO- stop being evil' Mar 13 '17 at 23:39
  • I don't really want to try it again since I'll probably have to restart my computer, but it does seem like a stuck modifier, since it behaved differently each time I did it. The first time it just printed out a control character no matter what key I pressed; the second time it didn't print anything. Why would modifiers get stuck? This smells like a race condition, which would be weird. – jstaab Mar 14 '17 at 15:42

I made my own custom macro using Linux Mint's Keyboard Shortcuts and adding a custom shortcut. I managed to get it to work by using --clearmodifiers, like so:

xdotool key --clearmodifiers Up

See if that helps.

Also, here's what the manual says on it:

CLEARMODIFIERS Any command taking the --clearmodifiers flag will attempt to clear any active input modifiers during the command and restore them afterwards.

   For example, if you were to run this command:
    xdotool key a

   The result would be 'a' or 'A' depending on whether or not you were holding the shift key on your keyboard. Often it is undesirable to have any modifiers active, so you can tell xdotool to clear any active modifiers.

   The order of operations if you hold shift while running 'xdotool key --clearmodifiers a' is this:

   1. Query for all active modifiers (finds shift, in this case)
   2. Try to clear shift by sending 'key up' for the shift key
   3. Runs normal 'xdotool key a'
   4. Restore shift key by sending 'key down' for shift

   The --clearmodifiers flag can currently clear of the following:

   ·   any key in your active keymap that has a modifier associated with it.  (See xmodmap(1)'s 'xmodmap -pm' output)

   ·   mouse buttons (1, 2, 3, 4, and 5)

   ·   caps lock

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.