How to do things on a certain background window with python?

Question:

I am playing a game and in order to make things easier, I have a basic python script that repeatedly presses ‘z’.

import pydirectinput
import time

time.sleep(3)

while True:
  pydirectinput.press('z')

I believe that due to directx issues, only pydirecinput library works and other libraries can not imitate pressing ‘z’ on the game. So, I have to use pydirectinput.

Here is the problem: When I use the script, I can not change the window since the code repeatedly presses ‘z’.

How can I make the script that presses ‘z’ in the background even though the game is in background and open new windows?

Asked By: eva-2021

||

Answers:

The keyword here being "in the background". You need your loop in a thread or process (a thread may be enough).

See python threading. Also here

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
Answered By: Gulzar