This does not work with Wayland; if you're using Ubuntu 17.10 and didn't change to using Xorg at login, this solution isn't for you.
You can use xdotool
for that. If the delay between the keystrokes should be consistent, it's as simple as that:
xdotool type --delay 100 something
This types something
with a delay of 100
milliseconds between each keystroke.
If the delay between the keystrokes should be random, let's say from 100 to 300 milliseconds, things get a bit more complicated:
$ text="some text"
for ((i=0;i<${#text};i++));
do
if [[ "${text:i:1}" == " " ]];
then
echo -n "key space";
else
echo -n "key ${text:i:1}";
fi;
[[ $i < $((${#text}-1)) ]] && echo -n " sleep 0.$(((RANDOM%3)+1)) ";
done | xdotool -
This for
loop goes through every single letter of the string saved in variable text
, printing either key <letter>
or key space
in the case of a space followed by sleep 0.
and a random number between 1 and 3 (xdotool
's sleep
interprets the number as seconds). The whole output of the loop is then piped to xdotool
, which prints the letters with the random delay in between. If you want to change the delay just change the (RANDOM%x)+y
part, y
being the lower and x-1+y
the upper limit – for 0.2 to 0.5 seconds it would be (RANDOM%4)+2
.
Note that this approach does not print the text, but rather type it exactly like the user would do, synthesizing single keypresses. In consequence the text gets typed into the currently focused window; if you change the focus part of the text will get typed in the newly focused window, which may or may not be what you want. In either case have a look at the other answers here, all of which are brilliant!