Ubuntu – xdotool’s type command eating characters

command linekeyboardshortcut-keysxdotoolxmonad

I'm trying to get xmonad to type a common sequence of characters whenever I hit a simple key combo. One instance of this problem is simply typing my email address.

This is what the configuration file for xmonad looks like for me:

import XMonad
import XMonad.Config.Gnome (gnomeConfig)
import XMonad.Util.CustomKeys (customKeys)
import XMonad.Util.EZConfig

main = xmonad $ gnomeConfig {
  keys = customKeys delKeys insKeys,
  logHook = return ()
  }
  where
    -- Remap mod-p to dmenu and mod-[sd] to swapping xinerama screens.
    delKeys :: XConfig l -> [(KeyMask, KeySym)]
    delKeys XConfig { modMask = modMask } =
      [ (modMask, xK_p), (modMask, xK_w), (modMask, xK_e) ]

    insertEmailAddress :: KeyMask -> [((KeyMask, KeySym), X())]
    insertEmailAddress modMask = [ ((noModMask, xK_Menu), spawn "xdotool type thisismyemail@example.com") ]

    insKeys :: XConfig l -> [((KeyMask, KeySym), X())]
    insKeys XConfig { modMask = modMask } = insertEmailAddress modMask

However, when I hit my menu key (it's in the bottom right of my keyboard) I get only the last half or so. Like: yemail@example.com or isismyemail@example.com. It's totally unpredictable where the printing will start.

I have found a workaround by changing the xdotool command to xdotool sleep 0.1 type thisismyemail@example.com. This seems to work without fail but I have no idea why. Any thoughts?

using xmonad 0.11 & xdotool version 3.20140217.1

Best Answer

The --sync option was exactly what I was looking for!

However, the --sync option only exists on a particular set of get or search commands.

The solution turned out to be: xdotool getwindowfocus windowfocus --sync type thisismyemail@example.com

What's happening here is that we first get the window that is focused, via getwindowfocus (getactivewindow is an alternative if the other doesn't work).

All we're doing differently is getting the active window and setting it to the same thing. The key difference is the presence of the --sync option which forces xdotool to wait until the previous command has been applied.

So we guarantee that we're focused to a particular window and will be able to type commands.

Related Question