fast right click loop python

Solutions on MaxInterview for fast right click loop python by the best coders in the world

showing results for - "fast right click loop python"
Juan Diego
14 Apr 2019
1import time
2import threading
3from pynput.mouse import Button, Controller
4from pynput.keyboard import Listener, KeyCode
5
6
7delay = 0.001
8button = Button.left
9start_stop_key = KeyCode(char='s')
10exit_key = KeyCode(char='e')
11
12
13class ClickMouse(threading.Thread):
14    def __init__(self, delay, button):
15        super(ClickMouse, self).__init__()
16        self.delay = delay
17        self.button = button
18        self.running = False
19        self.program_running = True
20
21    def start_clicking(self):
22        self.running = True
23
24    def stop_clicking(self):
25        self.running = False
26
27    def exit(self):
28        self.stop_clicking()
29        self.program_running = False
30
31    def run(self):
32        while self.program_running:
33            while self.running:
34                mouse.click(self.button)
35                time.sleep(self.delay)
36            time.sleep(0.1)
37
38
39mouse = Controller()
40click_thread = ClickMouse(delay, button)
41click_thread.start()
42
43
44def on_press(key):
45    if key == start_stop_key:
46        if click_thread.running:
47            click_thread.stop_clicking()
48        else:
49            click_thread.start_clicking()
50    elif key == exit_key:
51        click_thread.exit()
52        listener.stop()
53
54
55with Listener(on_press=on_press) as listener:
56    listener.join()
57