как прервать цикл while в pyautogui, нажав на определенную клавишу клавиатуры - PullRequest
1 голос
/ 04 мая 2019

Я хочу сломать свой код, щелкая конкретную клавишу, потому что это невозможно сделать с помощью мыши (мышь используется программой).

import pyautogui
import time
from mss import mss

start_x = 610
start_y = 600

cords_x = [0, 140, 280, 420]

bbox = (start_x, start_y, start_x + 500, start_y + 1)


def start():
    with mss() as sct:
        while True:
            img = sct.grab(bbox)
            for cord in cords_x:
                if img.pixel(cord, 0)[0] < 80:
                    pyautogui.click(start_x + cord, start_y)



time.sleep(5)
start()

1 Ответ

0 голосов
/ 04 мая 2019

вам просто нужно скачать модуль клавиатуры и импортировать его вот так

import pyautogui
import time
from mss import mss
import keyboard

def start():
    with mss() as sct:
        while True:
            img = sct.grab(bbox)
            for cord in cords_x:
                if img.pixel(cord, 0)[0] < 80:
                    pyautogui.click(start_x + cord, start_y)

            try:
                if keyboard.is_pressed('q'): # it will stop working by clicking q you can change to to any key
                    break
                else:
                    pass
            finally:
                pass


time.sleep(5)
start()
...