Python Forum
Please help me with keyboard clicker - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Please help me with keyboard clicker (/thread-26518.html)



Please help me with keyboard clicker - jurij7 - May-04-2020

Hello, my idea is that after i press "f" program start pressing arrows up, right, down and left in 0,2 sec, til I press "f" again.

My code is:

import time
import threading
from pynput.keyboard import Key, Controller
from pynput.keyboard import Listener, KeyCode
keyboard = Controller()

delay = 0.200
Key = [keyboard.press(Key.left),
    keyboard.release(Key.left)]
start_stop_key = KeyCode(char='f')
exit_key = KeyCode(char='e')

class ClickKB(threading.Thread):
    def __init__(self, delay, Key):
        super(ClickKB, self).__init__()
        self.delay = delay
        self.Key = Key
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    def run(self):
        while self.program_running:
            while self.running:
                #mouse.click(self.Key)
                time.sleep(self.delay)
            time.sleep(0.1)


keyboard = Controller()  # Create the controller
click_thread = ClickKB(delay, Key)
click_thread.start()


def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()


with Listener(on_press=on_press) as listener:
    listener.join()
But it is not working, can u please look on it and may be repair my code? Thank you very much.