Use IF in Pyautogui in Python (automation)

Asked

Viewed 20 times

-1

I am trying to make a piece of a code that it keeps clicking to the side automatically (with automation) and I want when I click certain key it stops working. I do not know what I put in "IF" to stop it from working, I do not know how I can say "if I click such thing stop working with the break". Please rent some help...

import pyautogui
import time
pyautogui.keyDown("alt")
pyautogui.press(["tab"])
pyautogui.keyUp("alt")
while True:
    pyautogui.press(["left"])
    if 
    break 

1 answer

0

Pyautogui only performs automation, it still cannot detect this type of event on the computer. What you can do is use, together with Pyautogui, the library pynput.

import pyautogui
from pynput import mouse

# Este é o callback para os eventos de click do mouse
def on_click(x, y, button, pressed):
    if button == mouse.Button.left and pressed:
        # Retornar False para a execução do listener de eventos
        print("Clicou!")
        return False

pyautogui.keyDown("alt")
pyautogui.press(["tab"])
pyautogui.keyUp("alt")

# Listener irá verificar quando o mouse clicará
with mouse.Listener(on_click=on_click) as listener:
    while True:

        # Assim que o mouse clicar, o listener irá encerrar e parar o loop
        if not listener.running:
            break

        pyautogui.press(["left"])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.