How to use Xdotool to Automate Input Events


Article Outline
  1. 1. xdotool
    1. 1.1. Install xdotool [2]
  2. 2. xdotool installation and usage [3]
    1. 2.1. Keystrokes
    2. 2.2. Mouse Input

xdotool

xdotool is used to simulate (generate) X11 keyboard/mouse input events.[1]

Install xdotool [2]

  • sudo apt-get install xdotool

xdotool installation and usage [3]

It’s good to understand its basic usage. This tool may come in handy in a variety of situations.

Keystrokes

  • Basic usage:
    • xdotool key x
      • Replace x with any character
      • simulates a simultaneous key ‘press’ and ‘release’ with no delay between events.
  • To initiate ‘keydown’ and ‘keyup’ events separately:
    • xdotool keydown x
    • xdotool keyup x
  • Simulate Keystroke with Modifier Keys (e.g. <CTRL+S>, <SHIFT+1>)
    • xdotool key ctrl+s
  • Simulate keys with “–repeat” and “–delay” (in milliseconds)
    • xdotool key --repeat 5 --delay 20 x
      • Avoid setting high delay values. There may be issues with repetition when the delay is set to be more than 500 ms. Workaround: Use “for” and “while” loop statements to overcome this limitation.
        • The following command will input the “x” key three times with a delay of 2 seconds in between each keystroke.
        • for i in {1..3}; do xdotool key x; sleep 2; done
        • The following command uses a while loop to repeatedly simulate keypresses of the ‘x’ key until the loop is manually interrupted by pressing the <CTRL+C> key:
        • while true; do xdotool key x; sleep 2; done
  • Simulate a Key Sequence (multiple keys one after another)
    • xdotool key a b c

Mouse Input

  • Mouse Click Key:
    • 1 – Left click
    • 2 – Middle click
    • 3 – Right click
    • 4 – Scroll wheel up
    • 5 – Scroll wheel down
  • Simulate Mouse Clicks:
    • xdotool click 3
      • Simulates a right click at the current location of the pointer.
  • Mouse Clicks using coordinates:
    • xdotool mousemove 100 100 click 3
      • Use the desired “X” and “Y” coordinates calculated from the top left corner of the screen.
  • Keystrokes can be combined with mouse clicks.
  • Get Active Window and Minimize
    • xdotool getactivewindow windowminimize
  • To see the manual:
  • man xdotool

To find the correct name for keys use the following debug tool:

  • xev

Xev prints the contents of X events, it creates a window and then asks the X server to send it events whenever anything happens to the window (such as it being moved, resized, typed in, clicked in, etc.). You can also attach it to an existing window. It is useful for seeing what causes events to occur and to display the information that they contain; it is essentially a debugging and development tool.[4]


  1. Debian Package Tracker - xdotool ↩︎

  2. ubuntu manuals - xdotool ↩︎

  3. How to Use Xdotool to Stimulate Mouse Clicks and Keystrokes in Linux ↩︎

  4. Debian Manpages - xev ↩︎