Sep-24-2020, 05:01 AM
I got this to work, but when I press s to stop the script, it will keep running the marketloop() function till end before stopping. My question is that how do I make the function stop instantly too? I tried adding break in stop buffing function but returned an expected 'break' outside loop error.
import pyautogui as pg import time import keyboard import random import win32api, win32con import threading from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode start_stop_key = KeyCode(char='s') exit_key = KeyCode(char='q') def marketloop(): #this has code that clicks through lots of things class BuffItems(threading.Thread): def __init__(self): super(BuffItems, self).__init__() self.running = False self.program_running = True def start_buffing(self): self.running = True def stop_buffing(self): self.running = False def exit(self): self.stop_buffing() self.program_running = False def run(self): while self.program_running: while self.running: marketloop() buff_thread = BuffItems() buff_thread.start() def on_press(key): if key == start_stop_key: if buff_thread.running: buff_thread.stop_buffing() else: buff_thread.start_buffing() elif key == exit_key: buff_thread.exit() listener.stop() with Listener(on_press=on_press) as listener: listener.join()