簡體   English   中英

打開外部程序時如何自動“按鍵”?

[英]How to 'press key' automatically when an external program is open?

我有一個使用 Python 啟動外部程序的腳本。 當外部程序打開時,我試圖按不同的鍵來捕獲統計信息。

我試圖做這樣的事情:

  with open(listFix[e] + "otherStats.txt" ,"w") as secondOutput :
        command2 = "{0} --screen 0 --window 0 0 960 540 s s s S {1}".format(screenCommand,listFix[e])
        processText = subprocess.Popen(command2,stdout=secondOutput)
        time.sleep(4)
        processText.kill()

但這不起作用。 當我手動執行此操作時,預期的 output 將寫入控制台。

( i want the sss S key to be pressed )

我該如何解決這個問題?

您可以為此使用 function Popen.comunicate 它允許您發送stdin值並從進程中獲取輸入的stdoutstderr ,這可能是這里的最佳實踐,文檔還告訴您如何使用它並且不使用stdin.write

Use communicate() rather than.stdin.write, .stdout.read or.stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

你可以在這里閱讀

您在 cmd 行中傳遞的值是程序的參數,而不是輸入。 這意味着您的 porgram 只是嘗試立即使用那些 arguments 運行命令

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM