我是新來的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」都做了什麼?我道歉,但它來自初學者。 –