How to get simulate Keyboard typing using python

6,585

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

  1. You'll need xdotool to be installed

    sudo apt-get install xdotool
    
  2. Copy te script into an empty file, save it as type_out.py

  3. 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!

Share:
6,585

Related videos on Youtube

YouBob Brutegeek
Author by

YouBob Brutegeek

Updated on September 18, 2022

Comments

  • YouBob Brutegeek
    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
      Jacob Vlijm about 8 years
      type into what? what is the context?
    • YouBob Brutegeek
      YouBob Brutegeek about 8 years
      Like 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
      Jacob Vlijm about 8 years
      Posted my answer, please let me know.
    • YouBob Brutegeek
      YouBob Brutegeek about 8 years
      Thanks!! This looks like it should work!! Thank you for your answer!
    • Jacob Vlijm
      Jacob Vlijm about 8 years
      You're welcome! Always happy if it works :)
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 8 years
    Since you ,not tou :)
  • Jacob Vlijm
    Jacob Vlijm about 8 years
    @Serg haha, Since you, not tou :) ?
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 8 years
    Spelling mistake in your answer , first line
  • Jacob Vlijm
    Jacob Vlijm about 8 years
    @Serg ah, typing typos are my strongest quality :)
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 8 years
    Hehe, i see. I'm sure there is a script for generating typos somewhere too XD. Anyway, good answer
  • kasperd
    kasperd about 8 years
    Why not drop the loop and instead call xdotool with argument --delay 100?
  • kasperd
    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
    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
    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
    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
    kasperd about 8 years
    @JacobVlijm Go ahead and edit as you see fit.
  • kasperd
    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 blame compiz.
  • Jacob Vlijm
    Jacob Vlijm about 8 years
    @kasperd I think I will keep the delay handling inside python, thanks though!