2017-05-06 120 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”都做了什么?我道歉,但它来自初学者。 –