如何用python在某个后台窗口做事?

问题描述 投票:0回答:1

我正在玩游戏,为了让事情变得更简单,我有一个基本的 python 脚本,可以反复按“z”。

import pydirectinput
import time

time.sleep(3)

while True:
  pydirectinput.press('z')

我相信由于directx 问题,只有pydirecinput 库可以工作,其他库不能模仿在游戏中按下'z'。所以,我必须使用 pydirectinput。

这里是问题所在:当我使用脚本时,我无法更改窗口,因为代码重复按'z'。

即使游戏在后台并打开新窗口,我如何制作在后台按“z”的脚本?

python multithreading selenium-webdriver directx
1个回答
0
投票

这里的关键词是“在后台”。您需要在线程或进程中进行循环(线程可能就足够了)。

参见python线程还有这里

def background_function(arg):
    while True:
        pydirectinput.press('z')


if __name__ == "__main__":
    thread = Thread(target = background_function, args = (10, ), daemon=True)
    thread.start()
    thread.join()
    print("thread finished...exiting")  # won't be reached
© www.soinside.com 2019 - 2024. All rights reserved.