How to get simulate Keyboard typing using python
Seemingly type out text with an arbitrary speed
Since you want it to seemingly be typed out, you probably would like the characters to appear not too fast.
You can use the code below to open a text file and seemingly "type" it anywhere.
#!/usr/bin/env python3
import subprocess
import time
import sys
# open the textfile
text = open(sys.argv[1]).read().strip()
for ch in text:
# type out the text
subprocess.call(["xdotool", "type", ch])
# increase or decrease the time below to type slower or faster
time.sleep(0.1)
How to use
You'll need xdotool to be installed
sudo apt-get install xdotool
Copy te script into an empty file, save it as
type_out.py
Run it with the text file as an argument:
python3 /path/to/type_out.py <textfile>
In case you'd like it to type into anything, you might want to add a small break before it to run, to be able to prevent it from starting to type while you're not ready yet...
That's it!
Related videos on Youtube
YouBob Brutegeek
Updated on September 18, 2022Comments
-
YouBob Brutegeek over 1 year
The idea is simple. Let say I need to get a pre-written sum of text, from a text file, to display on screen using the keyboard to type it out. This is different than just displaying text form regular output, it needs to come form the keyboard like if someone was typing it.
Any ideas would be very handy to me.
-
Jacob Vlijm about 8 yearstype into what? what is the context?
-
YouBob Brutegeek about 8 yearsLike a game chat.. Take the contents from a text files and send it over to game console. but it would just key storks
-
Jacob Vlijm about 8 yearsPosted my answer, please let me know.
-
YouBob Brutegeek about 8 yearsThanks!! This looks like it should work!! Thank you for your answer!
-
Jacob Vlijm about 8 yearsYou're welcome! Always happy if it works :)
-
-
Sergiy Kolodyazhnyy about 8 yearsSince you ,not tou :)
-
Jacob Vlijm about 8 years@Serg haha, Since you, not tou :) ?
-
Sergiy Kolodyazhnyy about 8 yearsSpelling mistake in your answer , first line
-
Jacob Vlijm about 8 years@Serg ah, typing typos are my strongest quality :)
-
Sergiy Kolodyazhnyy about 8 yearsHehe, i see. I'm sure there is a script for generating typos somewhere too XD. Anyway, good answer
-
kasperd about 8 yearsWhy not drop the loop and instead call
xdotool
with argument--delay 100
? -
kasperd about 8 years@JacobVlijm It works as advertised on my Ubuntu 14.04.4 LTS install. What is the exact command you were trying?
-
Jacob Vlijm about 8 years@kasperd I kind of have an idea how to type an xdotool command :) The issue is that when the value gets too high (typing too slow) it starts repeating the same character. An issue that does not occur when using the loop.
-
kasperd about 8 years@JacobVlijm I see. That sounds like a bug. I hadn't tried values larger than 1000, and 1000 worked just fine for me. If I increase it to 10000 I do see a problem.
-
Jacob Vlijm about 8 years@kasperd However, I must admit I didn't think of it. Would you mind if I edited it in with a side note?
-
kasperd about 8 years@JacobVlijm Go ahead and edit as you see fit.
-
kasperd about 8 years@JacobVlijm Btw. Too low values of delay can cause keystrokes to be reordered. That is not a bug in
xdotool
though. When the system is highly loaded that can even happen to real keystrokes. I blamecompiz
. -
Jacob Vlijm about 8 years@kasperd I think I will keep the delay handling inside python, thanks though!