-1

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?

Gulzar
  • 23,452
  • 27
  • 113
  • 201
eva-2021
  • 25
  • 4

1 Answers1

0

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
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • This works well however how can I make the script work on the certain background window? When I open chrome for example, the script writes 'z' to chrome. We should make it such that sends its output only to certain window, let say 'Game' window. – eva-2021 Feb 20 '23 at 20:29
  • @eva-2021 Try [selenium](https://selenium-python.readthedocs.io/). Your question is too broad to be answered more than that, as it requires research. – Gulzar Feb 21 '23 at 10:37
  • Thank you for your intuition. As far as I see selenium works with web drivers. I would be really pleased if you could lead me to the right way in selenium. I can do the rest – eva-2021 Feb 21 '23 at 17:18
  • @eva-2021 I have no experience with Selenium. You should try to get what you need done with it, and if you don't succeed, ask again, a specific question. Someone will likely answer – Gulzar Feb 22 '23 at 11:55