5
\$\begingroup\$

What do you think of my auto clicker script? Even though it's just for my own personal use, I'm thinking of using PyInstaller to turn it into an exe, so I know how to do that in the future.

import pyHook, threading
import tkinter as tk    
from pyautogui import click

t1token = 0
threadsRun = 1
ac = 0

def simplegui():

    def sunken():
        """keep startButton lowered and disabled.
            hook keyboard, start clicks function"""
        global threadsRun
        startButton.config(relief=tk.SUNKEN, state=tk.DISABLED, 
                           text="r=stop listening")
        hm.HookKeyboard()
        threadsRun = 1
        t1 = threading.Thread(target=clicks, args=(startButton,), 
                              name='clicks{}'.format(t1token))
        t1.start()

    def destroy():
        """exit program"""
        global threadsRun, ac
        ac = 0
        threadsRun = 0
        # Not sure if keyboard unhooks if press quit after press start
        root.destroy()

    startButton = tk.Button(root, text="Start", command=sunken, 
                            height=10, width=20)
    startButton.grid(row=1, column=0)

    quitButton = tk.Button(root, text="Quit", command=destroy, 
                           height=10, width=20)
    quitButton.grid(row=1, column=1)

def clicks(startButton):
    """right clicks the mouse at 1k clicks per minute
        also puts button back to normal state once done"""
    global t1token
    while threadsRun:
        while ac:
            click(clicks=17, interval=0.06, button='right')
    t1token += 1
    startButton.config(relief=tk.RAISED, state=tk.NORMAL, text="Start")

root = tk.Tk()

def OnKeyboardEvent(event):
    """A key was pressed"""
    global threadsRun, ac
    if event.Key == "R":
        ac = 0
        threadsRun = 0
        hm.UnhookKeyboard()
        return False
    if event.Key == "T":
        ac = not ac
        return False
    return True

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
simplegui()
root.mainloop()
\$\endgroup\$
2
  • \$\begingroup\$ Could you link to the modules please as I cant pip install them in command prompt \$\endgroup\$
    – Dan Howe
    May 6 '20 at 11:27
  • \$\begingroup\$ @DanHowe I'd try pip install pyhook3 pyautogui, as the pyhook package hasn't been updated since 2008. I can't vouch for any of these packages though. \$\endgroup\$ May 6 '20 at 16:38

Browse other questions tagged or ask your own question.