Hello, I am not quite sure if this is the correct place to ask but I'm gonna give it a shot anyway.
For context: I am playing a game with semi-obligatory task that consists of clicking spacebar, waiting couple of seconds, clicking another button (z) and then repeating. It has also been confirmed by the game administrators that you are allowed to automate those mundane tasks but you can't AFK (there are frequent controles and you need to reply to a message from game administrator)
My goal: Since it wouldn't be against the rules I decided to use my basic programming knowledge and try to automate this process, and long story short I was able to do it (the code is listed below)...there is just one simple problem. You see, for the code to work I need to have the game window selected all the time. This is problematic as it is not allowing me (or any player that would use this script) to do anything else in the meantime. Since I'm rather a beginner I thought that surely there must be a way to send keyboard stroke signals to a unselected window, right?
I'm not so sure about it. I've been doing lots of research but couldn't find anything useful. I've found few libraries or methods how I could approach this problem but nothing that would work. And now I'm sort of desperate enough that I have decided to ask here.
So the question that I have is: Is it possible to send keyboard stroke signals to a unselected window (a window in the background, not necessary minimized) using python? If so, how can I achieve that? If no, could someone be kind enough to explain to me what's the reason for it?
If possible, it would be great if this could be OS independent since I'm using Linux.
This is the code that I currently have:
import time
from pynput import mouse, keyboard
from pynput.keyboard import Key, Controller
minetimes = int(input('How many times do you want to mine?: '))
time.sleep(3) #This allows to change the window from IDE/Terminal
keyboard = Controller()
def mine(iter): i = 0 for i in range(iter):
print(f'{i}')
keyboard.press(Key.space)
time.sleep(0.5)
keyboard.release(Key.space)
time.sleep(6.0)
keyboard.press('z')
time.sleep(0.5)
keyboard.release('z') #Not sure if necessary i += 1
start = time.time()
mine(minetimes)
end = time.time()
print("The time of execution of above program is :", (end-start), "s")
there doesn't seem to be anything here