0

I have been learning python recently and decided to learn more about pyautogui, what you see down below is a macro of mine. My question is simple; is there a way to let this macro run in a specific window. For example: I want this macro to run in google chrome while I am in discord chatting with my friends (text channel so I'm not in the google chrome window). (Ignore my sloppy method of writing code)

import pyautogui
import random
import time
import mouse

#############################

tijd = 0
actief = 0
float (actief)

#############################

while not mouse.is_pressed('right'):
    time.sleep(0.01)
bank_x1, bank_y1 = pyautogui.position()

time.sleep (0.5)

while not mouse.is_pressed('right'):
    time.sleep(0.01)
bank_x2, bank_y2 = pyautogui.position()

print ("{} {} {} {}".format(bank_x1,bank_x2,bank_y1,bank_y2))

#############################

lijst = [[bank_x1,bank_x2,bank_y1,bank_y2,200,243],[1203,1236,721,749,23,49],[390,422,112,140,22,46]]
while not mouse.is_pressed('middle') or actief > tijd:
    for i in range(0, 4):
        x = random.randint(lijst[i][0], lijst[i][1])
        y = random.randint(lijst[i][2], lijst[i][3])
        pyautogui.moveTo(x, y)
        wacht = random.randint(lijst[i][4], lijst[i][5]) / 100
        time.sleep(wacht)
        str (actief_str)
        pyautogui.click()
    pyautogui.press('esc')
Joppe
  • 1
  • 3

1 Answers1

0

No. Not with pyautogui.

Pyautogui simulates actual keyboard and mouse input to the system, not to a specific window. Thus, it will always act exactly the same as if you were pressing the keys and clicking the mouse yourself. So, no. You cannot use it to send keystrokes to background applications. The keystrokes will always effect the window that is focused, just as they do when you physically input them with a keyboard or mouse.

This question shows how you could achieve it with winapi, but it is much more complicated and less user-friendly than pyautogui.

Filip Müller
  • 1,096
  • 5
  • 18