Sending keystrokes to Windows exe programs

Chris Angelico rosuav at
Sat Apr 2 21:52:39 CEST 2011

On Sun, Apr 3, 2011 at 2:48 AM, Alex van der Spek <zdoor at> wrote:
> I can start a windows program on Vista with:
>>>> import subprocess
>>>> dva=subprocess.Popen(DVAname,stdin=subprocess.PIPE)
> Unfortunately sending keystrokes with communicate() does not appear to work:
>>>> dva.communicate('F2')
> this does not produce any result but it does make IDLE become really idle.

I've just looked over the Python subprocess module. Is your subprocess
(named by the variable DVAname) one which takes key names on STDIN and
emits the appropriate keys?

dva.communicate('F2') will send the two-character string "F2" to the
STDIN of the process, and then wait for process termination. That's
why IDLE stops dead. If you want to send it a string and then keep
running, I think you want to use the stdin attribute:

Chris Angelico

More information about the Python-list mailing list