Simulating keystrokes from clipboard

From Jeremy Bryan Smith
Jump to navigation Jump to search

If you're like me and refuse to manually type anything, you might appreciate this trick. If you ever have to interface with a program or system that doesn't support pasting text from the clipboard but only supports typing keys (such as a virtual machine console, VNC, or KVM software): Notes:

  • This is for creating a SH script and is for the X window system (Linux, BSD, Unix, etc). Not Windows. Don't use Windows.
  • Requires the xclip program. To install (on apt-based Linux distros in this example):
    sudo apt-get install xclip
  • Requires the xdotool program. To install (on apt-based Linux distros in this example):
    sudo apt-get install xdotool
#!/bin/sh

# Get the contents of the X clipboard with:
TEXT="$(xclip -o -selection clipboard)"

# Sleep 5 seconds before sending keys. During this period, give the application's window focus so that it will receive the keystrokes
echo Sleeping 5 seconds before sending keys...
sleep 5

# Send the keystrokes
xdotool type --delay 100  "$TEXT"


If the keys are typed too fast for the program to catch up (maybe you're typing into a serial program running at a low baud rate), you can increase the delay value from 100 to 500 (milliseconds) with --delay 500.
I set up a global hotkey with my window manager to run the above script.