0

I am calling a Windows console application via python script. If the Windows console application exits with errors then the application prints "Press any key to continue" in the console. Now I would like to pass a random key press.

I have used keyboard.press_and_release(), keyboard.write(), pyautogui.press() to send the random keypress. But these functions are sending the keys only after the complete python scripts ends, not after "Press any key to continue". If I press any key then the script ends and then a random key gets pressed with these functions. I want to avoid me pressing the key.

How can I do it?

SS Hegde
  • 729
  • 2
  • 14
  • 33
  • How are you interacting with the console app? – alonkh2 Sep 23 '21 at 12:49
  • @alonkh2 No interaction as such. A plain python script calls another application via SubProcess.call() command. The keypress should happen only if another application prints "Press any key to continue". – SS Hegde Sep 23 '21 at 12:52
  • Can you share the part of the script that calls the app? – Nima Sep 23 '21 at 12:52
  • @Nima: ```subprocess.call(application)``` and in next line ```keyboard.press_and_release('a')``` – SS Hegde Sep 23 '21 at 12:54
  • Have you tried something like `process.command("c")`? – alonkh2 Sep 23 '21 at 12:54
  • @SSHegde what you did would never work, since `subprocess.call()` "freezes" the code until the process is over, thus the `keyboard.press_and_release('a')` line would not run in time. – alonkh2 Sep 23 '21 at 12:57
  • Tried now. ```process is not defined.``` may I know the module in which it is? Googling gave me only ```subprocess``` releated stuff. – SS Hegde Sep 23 '21 at 12:58
  • process is not a module - it's a variable. You'd do something like this - `process = subprocess.open(application)`, then the line I suggested – alonkh2 Sep 23 '21 at 12:59
  • 1
    https://stackoverflow.com/questions/1124884/interact-with-a-windows-console-application-via-python this is what I was referring to – alonkh2 Sep 23 '21 at 13:02
  • It is not enough. Please share the code snippet. – Nima Sep 23 '21 at 13:03
  • @alonkh2 : I tried the way you said by ```process.command()```. But when you assign a value from ```subprocess.open(application)``` it returns either 0 or 1. can't use it like ```process.command()``` – SS Hegde Sep 23 '21 at 13:05

1 Answers1

1

alonkh2's hint on the question worked with lots of modification.

retval = subprocess.Popen( runCommand, stdin=subprocess.PIPE, shell=True)
retval.communicate(input="{}\n".format("a").encode("utf-8"))
SS Hegde
  • 729
  • 2
  • 14
  • 33