Python自动点击器

一、如何制作一个Python自动点击器?

当用户单击开始键时,代码将从键盘获取输入,并在用户单击退出键时终止自动点击器,自动点击器开始单击指针放置在屏幕上的任何位置。我们将在这里使用pynput模块。

二、什么是自动点击器?

自动点击器是一个脚本,您可以根据需要多次自动控制鼠标和键盘。它使用用户定义的键进行控制。它适用于 Windows、Mac 和 Linux 等各种平台。pywin32 模块中存在自动点击器。

用旧电脑搭建NAS在您的家庭中,通过将旧 PC 转变为NAS服务器,您可以轻松搭建个人云存储、智能家居中心和媒体流设备。这个经济实惠的选择不仅更加灵活,还能充分利用旧 PC 的性能,使其更为强大。选择适合您的NAS操作系统,如Windows、OpenMediaVault、UnRAID或TrueNAS,为您提供不同的功能和性能。通过添加适用于PC到NAS的硬件附件,如Intel X520-DA1网卡、PCIe转M.2适配卡和PCIe SATA控制器扩展卡,您可以进一步提升网络速度和存储效率。借助个人云软件如Nextcloud,智能家居中心软件如Home Assistant,以及流媒体软件如Plex,您的NAS不仅是一个存储解决方案,更是家庭网络的核心。搭建NAS,创造更便捷、智能、娱乐的家居生活!icon-default.png?t=N7T8https://fostmar.online/archives/454/三、制作:

在这个项目中,我们将使用跨平台模块pynput来同时控制鼠标和监视键盘,以创建简单的自动点击器。为了检查鼠标事件,我们将为此执行安装pynput 模块(用于控制鼠标),在 cmd 中安装模块:

pip install pynput 。

验证pynput模块是否已成功安装到您的工作环境中:在cmd或Python Shell系统上打开 IDLE 。执行命令:

import pynput

执行此命令后,输出应该给出零错误,这意味着模块已成功安装。

四、执行:

现在让我们继续使用 Python 构建自动点击器所需的代码。

下面是完整的程序:

# importing time and threading 
import time 
import threading 
from pynput.mouse import Button, Controller 

# pynput.keyboard is used to watch events of 
# keyboard for start and stop of auto-clicker 
from pynput.keyboard import Listener, KeyCode 


# four variables are created to 
# control the auto-clicker 
delay = 0.001
button = Button.right 
start_stop_key = KeyCode(char='a') 
stop_key = KeyCode(char='b') 

# threading.Thread is used 
# to control clicks 
class ClickMouse(threading.Thread): 
    
# delay and button is passed in class 
# to check execution of auto-clicker 
    def __init__(self, delay, button): 
        super(ClickMouse, self).__init__() 
        self.delay = delay 
        self.button = button 
        self.running = False
        self.program_running = True

    def start_clicking(self): 
        self.running = True

    def stop_clicking(self): 
        self.running = False

    def exit(self): 
        self.stop_clicking() 
        self.program_running = False

    # method to check and run loop until 
    # it is true another loop will check 
    # if it is set to true or not, 
    # for mouse click it set to button 
    # and delay. 
    def run(self): 
        while self.program_running: 
            while self.running: 
                mouse.click(self.button) 
                time.sleep(self.delay) 
            time.sleep(0.1) 


# instance of mouse controller is created 
mouse = Controller() 
click_thread = ClickMouse(delay, button) 
click_thread.start() 


# on_press method takes 
# key as argument 
def on_press(key): 
    
# start_stop_key will stop clicking 
# if running flag is set to true 
    if key == start_stop_key: 
        if click_thread.running: 
            click_thread.stop_clicking() 
        else: 
            click_thread.start_clicking() 
            
    # here exit method is called and when 
    # key is pressed it terminates auto clicker 
    elif key == stop_key: 
        click_thread.exit() 
        listener.stop() 


with Listener(on_press=on_press) as listener: 
    listener.join()

现在让我们执行我们编写的 python 程序,然后按开始 (a)和停止 (a)键以启动自动点击器。

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值