桌面控制技能
**OpenClaw 最先进的桌面自动化技能。**提供像素级精准的鼠标控制、闪电般快速的键盘输入、屏幕截图、窗口管理和剪贴板操作。
功能
鼠标控制
-
绝对定位- 移动到精确坐标 -
相对移动- 从当前位置移动 -
流畅的移动——自然、类人的鼠标路径 -
点击类型- 左键、右键、中键、双击、三击 -
拖放- 从 A 点拖动到 B 点 -
滚动- 垂直和水平滚动 -
位置追踪- 获取当前鼠标坐标
键盘控制
-
文本输入- 快速、准确的文本输入 -
热键- 执行键盘快捷键(Ctrl+C、Win+R 等) -
特殊键- Enter、Tab、Esc、方向键、F 功能键 -
组合键- 多键组合 -
按住并松开- 手动按键状态控制 -
打字速度- 可配置每分钟字数 (从瞬间到接近人类速度)
屏幕操作
-
屏幕截图- 截取整个屏幕或特定区域 -
图像识别- 查找屏幕上的元素(通过 OpenCV) -
颜色检测- 获取坐标处的像素颜色 -
多显示器- 支持多个显示器
窗口管理
-
窗口列表- 获取所有打开的窗口 -
激活窗口- 将窗口置于前方 -
窗口信息- 获取位置、大小、标题 -
最小化/最大化- 控制窗口状态
安全功能
-
安全机制- 将鼠标移至角落即可中止 -
暂停控制- 紧急停止机制 -
审批模式- 操作需要确认 -
边界检查- 防止超出屏幕范围的操作 -
日志记录- 跟踪所有自动化操作
快速入门
安装
首先,安装所需的依赖项:
pip install pyautogui pillow opencv-python pygetwindow
基本用法
from skills.desktop_control import DesktopController
# Initialize controller
dc = DesktopController(failsafe=True)
# Mouse operations
dc.move_mouse(500, 300) # Move to coordinates
dc.click() # Left click at current position
dc.click(100, 200, button="right") # Right click at position
# Keyboard operations
dc.type_text("Hello from OpenClaw!")
dc.hotkey("ctrl", "c") # Copy
dc.press("enter")
# Screen operations
screenshot = dc.screenshot()
position = dc.get_mouse_position()
完整 API 参考
鼠标功能
move_mouse(x, y, duration=0, smooth=True)
将鼠标移动到屏幕绝对坐标。
参数:
-
x(整数):X 坐标(从左侧算起的像素) -
y(整数):Y 坐标(从顶部算起的像素) -
duration(浮点数):移动时间,单位为秒(0 = 瞬间,0.5 = 平滑) -
smooth(布尔值):使用贝塞尔曲线实现自然运动
例子:
# Instant movement
dc.move_mouse(1000, 500)
# Smooth 1-second movement
dc.move_mouse(1000, 500, duration=1.0)
move_relative(x_offset, y_offset, duration=0)
移动鼠标相对于当前位置。
参数:
-
x_offset(整数):水平移动的像素数(正数 = 向右) -
y_offset(整数):垂直方向移动的像素数(正数 = 向下) -
duration(浮点数):移动时间(秒)
例子:
# Move 100px right, 50px down
dc.move_relative(100, 50, duration=0.3)
click(x=None, y=None, button='left', clicks=1, interval=0.1)
执行鼠标点击操作。
参数:
-
x, y(整数,可选):点击坐标(无 = 当前位置) -
button(字符串):‘左’、‘右’、‘中间’ -
clicks(整数):点击次数(1 = 单次点击,2 = 双击) -
interval(浮点数):多次点击之间的延迟
例子:
# Simple left click
dc.click()
# Double-click at specific position
dc.click(500, 300, clicks=2)
# Right-click
dc.click(button='right')
drag(start_x, start_y, end_x, end_y, duration=0.5, button='left')
拖放操作。
参数:
-
start_x, start_y(整数):起始坐标 -
end_x, end_y(整数):结束坐标 -
duration(浮点数):拖拽持续时间 -
button(str): 要使用的鼠标按钮
例子:
# Drag file from desktop to folder
dc.drag(100, 100, 500, 500, duration=1.0)
scroll(clicks, direction='vertical', x=None, y=None)
滚动鼠标滚轮。
参数:
-
clicks(整数):滚动量(正数 = 向上/向左,负数 = 向下/向右) -
direction(字符串):‘垂直’ 或 ‘水平’ -
x, y(整数,可选):滚动位置
例子:
# Scroll down 5 clicks
dc.scroll(-5)
# Scroll up 10 clicks
dc.scroll(10)
# Horizontal scroll
dc.scroll(5, direction='horizontal')
get_mouse_position()
获取鼠标当前坐标。
返回值: (x, y)元组
例子:
x, y = dc.get_mouse_position()
print(f"Mouse is at: {x}, {y}")
键盘功能
type_text(text, interval=0, wpm=None)
以可配置的速度输入文本。
参数:
-
text(str): 要输入的文本 -
interval(浮点数):按键之间的延迟(0 = 瞬间) -
wpm(整数,可选):每分钟字数(覆盖间隔)
例子:
# Instant typing
dc.type_text("Hello World")
# Human-like typing at 60 WPM
dc.type_text("Hello World", wpm=60)
# Slow typing with 0.1s between keys
dc.type_text("Hello World", interval=0.1)
press(key, presses=1, interval=0.1)
按下并松开按键。
参数:
-
key(字符串):键名(参见“键名”部分) -
presses(整数):按压次数 -
interval(浮点数):按键之间的延迟
例子:
# Press Enter
dc.press('enter')
# Press Space 3 times
dc.press('space', presses=3)
# Press Down arrow
dc.press('down')
hotkey(*keys, interval=0.05)
执行键盘快捷键。
参数:
-
*keys(str):要一起按下的键 -
interval(浮点数):按键之间的延迟
例子:
# Copy (Ctrl+C)
dc.hotkey('ctrl', 'c')
# Paste (Ctrl+V)
dc.hotkey('ctrl', 'v')
# Open Run dialog (Win+R)
dc.hotkey('win', 'r')
# Save (Ctrl+S)
dc.hotkey('ctrl', 's')
# Select All (Ctrl+A)
dc.hotkey('ctrl', 'a')
key_down(key)/key_up(key)
手动控制按键状态。
例子:
# Hold Shift
dc.key_down('shift')
dc.type_text("hello") # Types "HELLO"
dc.key_up('shift')
# Hold Ctrl and click (for multi-select)
dc.key_down('ctrl')
dc.click(100, 100)
dc.click(200, 100)
dc.key_up('ctrl')
屏幕功能
screenshot(region=None, filename=None)
截取屏幕或区域。
参数:
-
region(元组,可选):(左,上,宽,高)用于局部捕获 -
filename(字符串,可选):保存图像的路径
返回值: PIL 图像对象
例子:
# Full screen
img = dc.screenshot()
# Save to file
dc.screenshot(filename="screenshot.png")
# Capture specific region
img = dc.screenshot(region=(100, 100, 500, 300))
get_pixel_color(x, y)
获取坐标处像素的颜色。
返回值: RGB元组(r, g, b)
例子:
r, g, b = dc.get_pixel_color(500, 300)
print(f"Color at (500, 300): RGB({r}, {g}, {b})")
find_on_screen(image_path, confidence=0.8)
在屏幕上查找图像(需要 OpenCV)。
参数:
-
image_path(str): 模板图像的路径 -
confidence(浮点数):匹配阈值(0-1)
返回值: (x, y, width, height)或无
例子:
# Find button on screen
location = dc.find_on_screen("button.png")
if location:
x, y, w, h = location
# Click center of found image
dc.click(x + w//2, y + h//2)
get_screen_size()
获取屏幕分辨率。
返回值: (width, height)元组
例子:
width, height = dc.get_screen_size()
print(f"Screen: {width}x{height}")
窗口函数
get_all_windows()
列出所有打开的窗户。
**返回值:**窗口标题列表
例子:
windows = dc.get_all_windows()
for title in windows:
print(f"Window: {title}")
activate_window(title_substring)
按标题将窗口置于最前。
参数:
title_substring(str): 要匹配的窗口标题的一部分
例子:
# Activate Chrome
dc.activate_window("Chrome")
# Activate VS Code
dc.activate_window("Visual Studio Code")
get_active_window()
获取当前聚焦窗口。
**返回值:**窗口标题(字符串)
例子:
active = dc.get_active_window()
print(f"Active window: {active}")
剪贴板功能
copy_to_clipboard(text)
将文本复制到剪贴板。
例子:
dc.copy_to_clipboard("Hello from OpenClaw!")
get_from_clipboard()
从剪贴板获取文本。
**返回值:**字符串
例子:
text = dc.get_from_clipboard()
print(f"Clipboard: {text}")
关键名称参考
字母键
'a'通过'z'
数字键
'0'通过'9'
功能键
'f1'通过'f24'
特殊按键
-
'enter'/'return' -
'esc'/'escape' -
'space'/'spacebar' -
'tab' -
'backspace' -
'delete'/'del' -
'insert' -
'home' -
'end' -
'pageup'/'pgup' -
'pagedown'/'pgdn'
方向键
'up'//'down''left''right'
修饰键
-
'ctrl'/'control' -
'shift' -
'alt' -
'win'//'winleft''winright' -
'cmd'/'command'(苹果)
锁钥匙
-
'capslock' -
'numlock' -
'scrolllock'
标点
-
'.'/','/'?'/'!'/';'/':' -
'['//']''{''}' -
'('/')' -
'+'/'-'/'*'/'/'/'='
安全功能
故障安全模式
将鼠标移至屏幕任意角落即可中止所有自动化操作。
# Enable failsafe (enabled by default)
dc = DesktopController(failsafe=True)
暂停控制
# Pause all automation for 2 seconds
dc.pause(2.0)
# Check if automation is safe to proceed
if dc.is_safe():
dc.click(500, 500)
审批模式
执行操作前需要用户确认:
dc = DesktopController(require_approval=True)
# This will ask for confirmation
dc.click(500, 500) # Prompt: "Allow click at (500, 500)? [y/n]"
高级示例
示例 1:自动表单填写
dc = DesktopController()
# Click name field
dc.click(300, 200)
dc.type_text("John Doe", wpm=80)
# Tab to next field
dc.press('tab')
dc.type_text("john@example.com", wpm=80)
# Tab to password
dc.press('tab')
dc.type_text("SecurePassword123", wpm=60)
# Submit form
dc.press('enter')
示例 2:屏幕截图区域并保存
# Capture specific area
region = (100, 100, 800, 600) # left, top, width, height
img = dc.screenshot(region=region)
# Save with timestamp
import datetime
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
img.save(f"capture_{timestamp}.png")
示例 3:多文件选择
# Hold Ctrl and click multiple files
dc.key_down('ctrl')
dc.click(100, 200) # First file
dc.click(100, 250) # Second file
dc.click(100, 300) # Third file
dc.key_up('ctrl')
# Copy selected files
dc.hotkey('ctrl', 'c')
示例 4:窗户自动化
# Activate Calculator
dc.activate_window("Calculator")
time.sleep(0.5)
# Type calculation
dc.type_text("5+3=", interval=0.2)
time.sleep(0.5)
# Take screenshot of result
dc.screenshot(filename="calculation_result.png")
示例 5:拖放文件
# Drag file from source to destination
dc.drag(
start_x=200, start_y=300, # File location
end_x=800, end_y=500, # Folder location
duration=1.0 # Smooth 1-second drag
)
性能提示
-
利用瞬时动作提升速度:
duration=0 -
批量操作而非单独调用
-
**缓存屏幕位置,**而不是重新计算
-
为获得最佳性能,请禁用故障保护功能(请谨慎使用)
-
使用快捷键代替菜单导航
重要提示
-
屏幕坐标从左上角的 (0, 0) 开始
-
多显示器设置中,辅助显示器的坐标可能为负值。
-
Windows DPI 缩放可能会影响坐标精度
-
安全角点为:(0,0), (宽度-1, 0), (0, 高度-1), (宽度-1, 高度-1)
-
某些应用程序可能会阻止模拟输入(例如游戏、安全应用程序)。
故障排除
鼠标未移动到正确位置
-
检查 DPI 缩放设置
-
确认屏幕分辨率符合预期
-
用于
get_screen_size()确认尺寸
键盘输入不起作用
-
确保目标应用程序重点突出
-
某些应用需要管理员权限。
-
尝试提高
interval可靠性
意外触发了故障保护机制
-
提高屏幕边框容差
-
正常使用时,请将鼠标移离角落。
-
如有需要,请禁用:
DesktopController(failsafe=False)
权限错误
-
某些操作需要使用管理员权限运行 Python。
-
某些安全应用程序会阻止自动化
依赖项
-
PyAutoGUI - 核心自动化引擎
-
枕头- 图像处理
-
OpenCV(可选)- 图像识别
-
PyGetWindow - 窗口管理
全部安装:
pip install pyautogui pillow opencv-python pygetwindow
专为 OpenClaw 打造——终极桌面自动化助手![]()
下载zip,直接发送文件并与龙虾对话请帮我下载zip并unzip解压,帮我安装这个skills
链接:夸克网盘分享
提取码:2JVb