Basil Gass
Basil Gass

Reputation: 352

Execute external application and send some key events to it

I wasn't able to find a solution for Python.

I am abelt o launch the application (using subprocess.Popen or subprocess.call), but I can't find a way to do the other part:

I want to send a serie of keys (kind of macro) to the application I just opened. Like:

Tab Tab Enter Tab Tab Delete ...

Is there a way to do this that is Mac and PC compatible ? Or, in case not, only PC ?

Thanks for your help, Basil

PS. I know there are some application to automate some keys event, but I want to make my own.

Upvotes: 3

Views: 2938

Answers (3)

Basil Gass
Basil Gass

Reputation: 352

Thanks for answering. I tried using the subprocess.Popen(), but it seems that it doesn't work. Sending the '\t' string does not work... It simply does nothing... Notice that the application is not python based (it's an installation application - basically, they are auto-extracting zip files (.exe) and I have hundreds of them...)

I'll try the other idea with some windows modules... but I really would prefer using something Mac and PC compatible...

Upvotes: 0

Mark
Mark

Reputation: 108567

Under windows you could use the venerable SendKeys to do this. There's a few implementations floating around. One, using the win32 extentions or two, there's even a couple ready-to-use modules available

Upvotes: 2

Rakis
Rakis

Reputation: 7874

Run the subprocess.Popen() command with the argument stdin=subprocess.PIPE then use the Popen object's stdin file to write data to the process's standard input stream. For some of the commands you mentioned, standard string escapes are available (like '\t' for TAB). However, if you need a more comprehensive keyset, you'll need to break out an old ASCII table and piece together the strings via the chr() function.

p = subprocess.Popen(['bash',], stdin=subprocess.PIPE)
p.stdin.write('echo "Hello\t\t\tWorld"\n')

Upvotes: 1

Related Questions