怎样实现鼠标自动操作脚本

wen 实用脚本 21

本文目录导读:

怎样实现鼠标自动操作脚本

  1. Python + PyAutoGUI(推荐)
  2. AutoHotkey(Windows)
  3. Selenium(网页自动化)
  4. 按键精灵(简单易用)
  5. 安全最佳实践
  6. 使用建议

我来介绍几种实现鼠标自动操作脚本的方法,从简单到复杂:

Python + PyAutoGUI(推荐)

安装

pip install pyautogui pillow

基本示例

import pyautogui
import time
# 获取屏幕尺寸
screen_width, screen_height = pyautogui.size()
print(f"屏幕尺寸: {screen_width}x{screen_height}")
# 模拟鼠标移动
pyautogui.moveTo(500, 500, duration=1)  # 移动到(500,500)
# 模拟点击
pyautogui.click(500, 500)  # 点击
pyautogui.doubleClick(500, 500)  # 双击
pyautogui.rightClick(500, 500)  # 右键
# 拖拽操作
pyautogui.drag(100, 0, duration=0.5)  # 向右拖拽100像素
# 识别图片并点击
try:
    button_pos = pyautogui.locateOnScreen('button.png')
    if button_pos:
        pyautogui.click(button_pos)
except:
    print("未找到目标图片")

高级脚本示例

import pyautogui
import time
import random
def human_like_move(x, y):
    """模拟人类移动轨迹"""
    # 添加随机延迟
    time.sleep(random.uniform(0.1, 0.3))
    # 曲线移动
    pyautogui.moveTo(x, y, 
                    duration=random.uniform(0.2, 0.5),
                    tween=pyautogui.easeOutQuad)
def auto_click_workflow():
    """自动点击工作流"""
    # 安全机制:防止失控
    pyautogui.FAILSAFE = True
    for i in range(5):
        # 检查是否按下了紧急停止键
        if pyautogui.onScreen(0, 0):
            # 模拟用户操作
            x = random.randint(200, 800)
            y = random.randint(200, 600)
            human_like_move(x, y)
            pyautogui.click()
            time.sleep(random.uniform(1, 3))

AutoHotkey(Windows)

基本语法

; 定义热键
F1::Click  ; 按F1点击
F2::MouseMove, 500, 500  ; 移动鼠标到指定位置
F3::Send, Hello World  ; 发送文本
; 循环点击
^j::  ; Ctrl+J
loop 10
{
    Click
    Sleep, 1000
}
return
; 图像识别并点击
^g::
CoordMode, Pixel, Screen
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, target.png
if (ErrorLevel = 0)
{
    Click, %FoundX%, %FoundY%
}
return

实用脚本

#Persistent
SetTimer, AutoClick, 5000  ; 每5秒执行一次
AutoClick:
    MouseGetPos, x, y
    Click, %x%, %y%
    ToolTip, 已点击 (%x%, %y%)
    SetTimer, RemoveToolTip, 2000
return
RemoveToolTip:
    ToolTip
return
F10::Pause  ; F10暂停
F11::ExitApp  ; F11退出

Selenium(网页自动化)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
# 初始化浏览器
driver = webdriver.Chrome()
driver.get("https://example.com")
# 等待页面加载
time.sleep(2)
# 模拟鼠标操作
element = driver.find_element(By.ID, "button-id")
action = ActionChains(driver)
# 移动到元素并点击
action.move_to_element(element).click().perform()
# 拖拽操作
source = driver.find_element(By.ID, "draggable")
target = driver.find_element(By.ID, "droppable")
action.drag_and_drop(source, target).perform()
# 右键点击
action.context_click(element).perform()
# 模拟复杂操作
action.click_and_hold(element).move_by_offset(100, 0).release().perform()
driver.quit()

按键精灵(简单易用)

// 基础脚本
Delay 1000
MoveTo 500, 500
LeftClick 1
Delay 500
// 循环操作
For 10
    MoveTo 500, 500
    LeftClick 1
    Delay 2000
EndFor
// 查找图像
FindPic 0,0,1920,1080,"目标.png",0.9,intX,intY
If intX > 0 And intY > 0
    MoveTo intX, intY
    LeftClick 1
EndIf

安全最佳实践

import pyautogui
import threading
class MouseBot:
    def __init__(self):
        self.running = False
        self.emergency_stop = False
    def start(self):
        """启动自动化"""
        self.running = True
        # 启动紧急停止监听
        threading.Thread(target=self._emergency_listener, daemon=True).start()
    def _emergency_listener(self):
        """监听紧急停止键"""
        import keyboard
        keyboard.add_hotkey('ctrl+shift+q', self.stop)
        keyboard.wait('ctrl+shift+q')
    def stop(self):
        """停止所有操作"""
        self.running = False
        self.emergency_stop = True
        print("紧急停止!")
    def human_click(self, x, y):
        """安全的点击操作"""
        if not self.running:
            return
        # 安全检查
        if not pyautogui.onScreen(x, y):
            print("坐标超出屏幕范围")
            return
        # 模拟人类操作
        pyautogui.moveTo(x, y, duration=0.2)
        time.sleep(0.1)
        pyautogui.click()

使用建议

  1. 开始前准备

    • 设置安全机制(紧急停止键)
    • 测试环境先运行
    • 备份重要数据
  2. 调试技巧

    # 显示鼠标位置
    pyautogui.displayMousePosition()
    # 截图调试
    pyautogui.screenshot('debug.png', region=(0, 0, 500, 500))
  3. 避免检测

    • 添加随机延迟
    • 模拟人类移动轨迹
    • 避免固定模式

根据你的具体需求(游戏辅助、办公自动化、网页操作等),选择合适的工具和方法。

抱歉,评论功能暂时关闭!