我是新来的Python,我想我会做一个简单的自动刷新作为一个很酷的启动项目。Python 3 Autoclicker开/关热键
我希望用户能够指定点击间隔,然后用热键打开和关闭自动点击。
我知道Ctrl-C,你会看到在我当前的代码中,但我想让程序工作,以便热键不必在python窗口中激活。
import pyautogui, sys
print("Press Ctrl + C to quit.")
interval = float(input("Please give me an interval for between clicks in seconds: "))
try:
while True:
pyautogui.click()
except KeyboardInterrupt:
print("\n")
我是否需要制作一个tkinter消息框才能制作开关或者我可以使用热键?
感谢您的帮助。
更新
import multiprocessing
import time
import pyHook, pyautogui, pythoncom
import queue
click_interval = float(input("Please give an interval between clicks in seconds: "))
class AutoClicker(multiprocessing.Process):
def __init__(self, queue, interval):
multiprocessing.Process.__init__(self)
self.queue = queue
self.click_interval = click_interval
def run(self):
while True:
try:
task = self.queue.get(block=False)
if task == "Start":
print("Clicking...")
pyautogui.click(interval == click_interval)
except task == "Exit":
print("Exiting")
self.queue.task_done()
break
return
def OnKeyboardEvent(event):
key = event.Key
if key == "F3":
print("Starting auto clicker")
# Start consumers
queue.put("Start")
queue.join
elif key == "F4":
print("Stopping auto clicker")
# Add exit message to queue
queue.put("Exit")
# Wait for all of the tasks to finish
queue.join()
# return True to pass the event to other handlers
return True
if __name__ == '__main__':
# Establish communication queues
queue = multiprocessing.JoinableQueue()
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
AutoClicker.run(self)
我甚至不得不使用一个队列,或者我可以使用一个简单的while循环或if/else语句吗?如果按下F3,执行自动刷新,然后如果按下F4,则跳出循环。 –
我更新了我的答案,希望我解决了您的问题。如果您还有其他问题,请随时提问。我也敦促你自己尝试并实施while循环,并看到与之相关的困难。 –
我试过不同的东西,我也修改了你原来的代码,我相信会工作,但我只是不明白你的代码。我必须运行哪些函数才能使代码正常工作,以及所有“自我”调用和“init”都做了什么?我道歉,但它来自初学者。 –