2017-05-06 122 views
2

我是新來的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) 

回答

1

首先,如果你想監控的Python窗口外全局輸入您需要pyHook或類似的東西。它將允許您監視鍵盤事件。我選擇了F3F4按鍵,它們用於啓動和停止自動刷新。

要完成你所問的問題,我所知道的最好方法是創建一個進程,它將執行點擊操作,並通過使用隊列與它進行通信。當按下F4鍵時,它會將一個"Exit"字符串添加到隊列中。自動刷新會識別這個,然後返回。

在按下F4鍵之前,隊列將保持爲空,並且queue.Empty異常將持續發生。這將執行一次單擊鼠標。

import multiprocessing 
import time 
import pyHook, pyautogui, pythoncom 
import queue 

class AutoClicker(multiprocessing.Process): 
    def __init__(self, queue, interval): 
     multiprocessing.Process.__init__(self) 
     self.queue = queue 
     self.click_interval = interval 

    def run(self): 
     while True: 
      try: 
       task = self.queue.get(block=False) 
       if task == "Exit": 
        print("Exiting") 
        self.queue.task_done() 
        break 

      except queue.Empty: 
       time.sleep(self.click_interval) 
       print("Clicking...") 
       pyautogui.click() 
     return 

def OnKeyboardEvent(event): 

    key = event.Key 

    if key == "F3": 
     print("Starting auto clicker") 
     # Start consumers 
     clicker = AutoClicker(queue, 0.1) 
     clicker.start() 
    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() 

請記住,這個實現遠非完美,但希望它有助於作爲一個起點。

爲什麼我不能使用簡單的while循環或if/else語句?

while循環被阻塞,這意味着當循環運行時,它會阻止所有其他代碼執行。

所以一旦點擊者循環開始,它將無限期地停留在那個循環中。當發生這種情況時,您無法檢查F4鍵按壓,因爲循環會阻止任何其他代碼執行(即檢查按鍵的代碼)。由於我們希望自動點擊器點擊和按鍵檢查同時發生,因此它們需要分開處理(因此它們不會彼此阻擋)。

檢查按鍵的主要過程需要能夠以某種方式與auto-clicker進程進行通信。所以當按下F4鍵時,我們希望點擊過程退出。使用隊列是溝通的一種方式(還有其他方法)。 auto-clicker可以連續檢查while循環中的隊列。當我們想要停止點擊時,我們可以在主進程的隊列中添加一個"Exit"字符串。下一次clicker進程讀取隊列時,它會看到並中斷。

+0

我甚至不得不使用一個隊列,或者我可以使用一個簡單的while循環或if/else語句嗎?如果按下F3,執行自動刷新,然後如果按下F4,則跳出循環。 –

+0

我更新了我的答案,希望我解決了您的問題。如果您還有其他問題,請隨時提問。我也敦促你自己嘗試並實施while循環,並看到與之相關的困難。 –

+0

我試過不同的東西,我也修改了你原來的代碼,我相信會工作,但我只是不明白你的代碼。我必須運行哪些函數才能使代碼正常工作,以及所有「自我」調用和「init」都做了什麼?我道歉,但它來自初學者。 –