Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
type into what? what is the context? – Jacob Vlijm Feb 27 at 6:13
    
Like a game chat.. Take the contents from a text files and send it over to game console. but it would just key storks – YouBob Brutegeek Feb 27 at 6:17
    
Posted my answer, please let me know. – Jacob Vlijm Feb 27 at 6:51
    
Thanks!! This looks like it should work!! Thank you for your answer! – YouBob Brutegeek Feb 27 at 14:38
    
You're welcome! Always happy if it works :) – Jacob Vlijm Feb 27 at 15:59
up vote 1 down vote accepted

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|improve this answer
    
Since you ,not tou :) – Serg Feb 27 at 6:39
    
@Serg haha, Since you, not tou :) ? – Jacob Vlijm Feb 27 at 6:45
1  
Spelling mistake in your answer , first line – Serg Feb 27 at 6:48
1  
Hehe, i see. I'm sure there is a script for generating typos somewhere too XD. Anyway, good answer – Serg Feb 27 at 6:51
1  
@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. – kasperd Feb 27 at 9:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.