How to pause a while loop until a certain key is pressed?

333 Views Asked by At

I want the clicking to pause when you press xbutton1 and it to continue when I press xbutton2.

Here's my code:

import win32api
import win32con
import pyautogui

click = True


def clicking():
    get = win32api.GetKeyState(win32con.VK_LBUTTON)
    if get < 1:
        pyautogui.click()


def stop():
    if win32api.GetKeyState(win32con.VK_XBUTTON1):
        click = False


def continue_program():
    if win32api.GetKeyState(win32con.VK_XBUTTON2):
        click = True


while True:
    if click == True:
        clicking()

    else:
        stop()
1

There are 1 best solutions below

4
On
import win32api
import win32con
import pyautogui

click = True


def clicking():
    get = win32api.GetKeyState(win32con.VK_LBUTTON)
    if get < 1:
        pyautogui.click()


def check():
    if win32api.GetKeyState(win32con.VK_XBUTTON1):
        click = False
    if win32api.GetKeyState(win32con.VK_XBUTTON2):
        click = True
    # Actually, here still has some contitions to check.
    return click


while True:
    if not check():
        continue
    # The things you want the loop to do. 
    clicking()