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)键以启动自动点击器。

相关推荐

  1. Python创建可网页

    2024-01-07 18:00:02       8 阅读
  2. Ubuntu鼠标自动脚本-工具xdotool简单使用

    2024-01-07 18:00:02       15 阅读
  3. vue设置自身以外其他区域关闭列表

    2024-01-07 18:00:02       5 阅读
  4. python实现模拟用户行为测试

    2024-01-07 18:00:02       36 阅读
  5. 实用Python定时Chrome网页按钮

    2024-01-07 18:00:02       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 18:00:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 18:00:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 18:00:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 18:00:02       18 阅读

热门阅读

  1. 【MySQL】关于日期转换的方法

    2024-01-07 18:00:02       41 阅读
  2. Github Copilot 快速入门

    2024-01-07 18:00:02       38 阅读
  3. Vue中的组件通信方式及应用场景

    2024-01-07 18:00:02       42 阅读
  4. Linux iptables地址转换

    2024-01-07 18:00:02       30 阅读
  5. 代码随想录算法训练营

    2024-01-07 18:00:02       36 阅读
  6. leetcode08-棒球比赛

    2024-01-07 18:00:02       40 阅读