Reputation: 361
I need to send keystrokes to an exe using python. I can run the exe using python from subprocess as
import subprocess
subprocess.Popen('myfile.exe',stdout=subprocess.PIPE)
but how can I keep the connection and keep sending keys. I don't want to read back from the exe just to send some keystrokes any suggestions
Upvotes: 0
Views: 347
Reputation:
Use stdin=subprocess.PIPE
and Popen.communicate()
:
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be data to be sent to the child process, or None, if no data should be sent to the child. If streams were opened in text mode, input must be a string. Otherwise, it must be bytes.
communicate() returns a tuple (stdout_data, stderr_data). The data will be strings if streams were opened in text mode; otherwise, bytes.
Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.
Upvotes: 1