关于 Desktop Control
Desktop Control 是供 AI Agent 使用的桌面自动化技能,通过 `uvx desktop-agent` 控制鼠标、键盘、屏幕和应用程序。它还支持 OCR、图像定位及用户对话框,命令输出结构化 JSON。
主要功能
- 01鼠标移动、点击与滚动
- 02键盘输入和快捷键
- 03屏幕截图与元素定位
- 04OCR 读取和定位文本
- 05打开、聚焦及列出应用窗口
- 06显示提示、确认和输入对话框
适用场景
- 01自动填写桌面应用中的表单
- 02截图并检查界面元素或文本
- 03启动应用并执行键盘操作
- 04在重要操作前请求用户确认
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ patrickporto/ desktop-agent --skill desktop-control安装后在 Claude Code 中输入/desktop-control确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
.gitignore.python-versionAGENTS.mdLICENSEREADME.md.agent/workflows/openspec-archive.mdopenspec-proposal.mdopenspec-apply.md.github/workflows/release.yml.opencode/command/openspec-proposal.mdopenspec-archive.mdopenspec-apply.mdcommands/__init__.pymessage.pyscreen.pymouse.pykeyboard.pydesktop_agent/__init__.py__main__.pydesktop_agent/commands/__init__.pymessage.pyscreen.pykeyboard.pymouse.pyapp.pydesktop_agent/specs/__init__.pydesktop_agent/specs/v1/schema.jsondesktop_agent/utils/__init__.pyresponse.pyerrors.py详细介绍
桌面控制技能
此技能通过 PyAutoGUI 提供全面的桌面自动化功能,让 AI Agent 能够控制鼠标和键盘、截取屏幕截图,并与桌面环境交互。
如何使用此技能
作为 AI Agent,你可以使用 uvx desktop-agent CLI 调用桌面自动化命令。
命令结构
所有命令均遵循以下模式:
uvx desktop-agent <category> <command> [arguments] [options]
类别:
mouse- 鼠标控制keyboard- 键盘输入screen- 截图和屏幕分析message- 用户对话框app- 应用程序控制(打开、聚焦、列出窗口)
可用命令
🖱️ 鼠标控制(mouse)
控制光标移动和点击。
# Move cursor to coordinates
uvx desktop-agent mouse move <x> <y> [--duration SECONDS]
# Click at current position or specific coordinates
uvx desktop-agent mouse click [x] [y] [--button left|right|middle] [--clicks N]
# Specialized clicks
uvx desktop-agent mouse double-click [x] [y]
uvx desktop-agent mouse right-click [x] [y]
uvx desktop-agent mouse middle-click [x] [y]
# Drag to coordinates
uvx desktop-agent mouse drag <x> <y> [--duration SECONDS] [--button BUTTON]
# Scroll (positive=up, negative=down)
uvx desktop-agent mouse scroll <clicks> [x] [y]
# Get current mouse position
uvx desktop-agent mouse position
示例:
# Move to center of 1920x1080 screen
uvx desktop-agent mouse move 960 540 --duration 0.5
# Right-click at specific location
uvx desktop-agent mouse right-click 500 300
# Scroll down 5 clicks
uvx desktop-agent mouse scroll -5
⌨️ 键盘控制(keyboard)
输入文本并执行键盘快捷键。
# Type text
uvx desktop-agent keyboard write "<text>" [--interval SECONDS]
# Press keys
uvx desktop-agent keyboard press <key> [--presses N] [--interval SECONDS]
# Execute hotkey combination (comma-separated)
uvx desktop-agent keyboard hotkey "<key1>,<key2>,..."
# Hold/release keys
uvx desktop-agent keyboard keydown <key>
uvx desktop-agent keyboard keyup <key>
示例:
# Type text with natural delay
uvx desktop-agent keyboard write "Hello World" --interval 0.05
# Copy selected text
uvx desktop-agent keyboard hotkey "ctrl,c"
# Open Task Manager
uvx desktop-agent keyboard hotkey "ctrl,shift,esc"
# Press Enter 3 times
uvx desktop-agent keyboard press enter --presses 3
常见按键名称:
- 修饰键:
ctrl、shift、alt、win - 特殊键:
enter、tab、esc、space、backspace、delete - 功能键:
f1至f12 - 方向键:
up、down、left、right
🖼️ 屏幕与截图(screen)
截取屏幕截图并分析屏幕内容。支持指定特定窗口。
# Take screenshot
uvx desktop-agent screen screenshot <filename> [--region "x,y,width,height"] [--window <title>] [--active]
# Locate image on screen or within window
uvx desktop-agent screen locate <image_path> [--confidence 0.0-1.0] [--window <title>] [--active]
uvx desktop-agent screen locate-center <image_path> [--confidence 0.0-1.0] [--window <title>] [--active]
# Locate text using OCR within window
uvx desktop-agent screen locate-text-coordinates <text> [--window <title>] [--active]
uvx desktop-agent screen read-all-text [--window <title>] [--active]
# Utility commands
uvx desktop-agent screen pixel <x> <y>
uvx desktop-agent screen size
uvx desktop-agent screen on-screen <x> <y>
示例:
# Screenshot of active window
uvx desktop-agent screen screenshot active.png --active
# Screenshot of a specific application
uvx desktop-agent screen screenshot chrome.png --window "Google Chrome"
# Locate image within Notepad
uvx desktop-agent screen locate-center button.png --window "Notepad"
💬 消息对话框(message)
显示用户交互对话框。
# Show alert
uvx desktop-agent message alert "<text>" [--title TITLE] [--button BUTTON]
# Show confirmation dialog
uvx desktop-agent message confirm "<text>" [--title TITLE] [--buttons "OK,Cancel"]
# Prompt for input
uvx desktop-agent message prompt "<text>" [--title TITLE] [--default TEXT]
# Password input
uvx desktop-agent message password "<text>" [--title TITLE] [--mask CHAR]
示例:
# Simple alert
uvx desktop-agent message alert "Task completed!"
# Get user confirmation
uvx desktop-agent message confirm "Continue with operation?"
# Ask for user input
uvx desktop-agent message prompt "Enter your name:"
📱 应用程序控制(app)
在 Windows、macOS 和 Linux 上控制应用程序。
# Open an application by name
uvx desktop-agent app open <name> [--arg ARGS...]
# Focus on a window by title/name
uvx desktop-agent app focus <name>
# List all visible windows
uvx desktop-agent app list
示例:
# Windows: Open Notepad
uvx desktop-agent app open notepad
# Windows: Open Chrome with a URL
uvx desktop-agent app open "chrome" --arg "https://google.com"
# macOS: Open Safari
uvx desktop-agent app open "Safari"
# Focus on a specific window
uvx desktop-agent app focus "Untitled - Notepad"
# List all open windows
uvx desktop-agent app list
常见自动化工作流
工作流 1:打开应用程序并输入文本
# Open notepad directly (cross-platform)
uvx desktop-agent app open notepad
# Wait for app to open, then focus it
uvx desktop-agent app focus notepad
# Type some text
uvx desktop-agent keyboard write "Hello from Desktop Skill!"
工作流 2:截图 + 分析
# Get screen size first
uvx desktop-agent screen size
# Take full screenshot
uvx desktop-agent screen screenshot current_screen.png
# Check if specific UI element is visible
uvx desktop-agent screen locate save_button.png
工作流 3:填写表单
# Click first field
uvx desktop-agent mouse click 300 200
# Fill field
uvx desktop-agent keyboard write "John Doe"
# Tab to next field
uvx desktop-agent keyboard press tab
# Fill second field
uvx desktop-agent keyboard write "[email protected]"
# Submit form (Enter)
uvx desktop-agent keyboard press enter
工作流 4:复制/粘贴操作
# Select all text
uvx desktop-agent keyboard hotkey "ctrl,a"
# Copy
uvx desktop-agent keyboard hotkey "ctrl,c"
# Click destination
uvx desktop-agent mouse click 500 600
# Paste
uvx desktop-agent keyboard hotkey "ctrl,v"
安全注意事项
使用此技能时,AI Agent 应当:
- 确认坐标:点击前使用
screen size和on-screen - 添加延迟:在命令之间加入适当的延迟,确保 UI 响应
- 验证图像:使用
locate命令前,确保图像文件存在 - 处理失败情况:窗口变化或元素移动时,命令可能会失败
- 用户安全:始终通过
message confirm向用户确认破坏性操作
故障排除
PyAutoGUI 故障保护
PyAutoGUI 具有故障保护机制:将鼠标移至屏幕角落会中止操作。这是一项安全功能。
未找到图像
使用 screen locate 时,请确保:
- 图像文件存在且路径正确
- 调整
--confidence(尝试 0.7-0.9) - 图像与屏幕上的实际显示完全一致(分辨率、颜色)
获取帮助
# Show all available commands
uvx desktop-agent --help
# Show commands for specific category
uvx desktop-agent mouse --help
uvx desktop-agent keyboard --help
uvx desktop-agent screen --help
uvx desktop-agent message --help
# Show help for specific command
uvx desktop-agent mouse move --help
面向 AI Agent 的集成技巧
- 使用绝对坐标时,务必先检查屏幕尺寸
- 尽可能使用相对定位(例如,获取当前位置并计算偏移量)
- 组合命令以完成复杂工作流
- 执行前进行验证(例如,检查图像是否显示在屏幕上)
- 通过消息对话框向用户反馈重要操作
- 妥善处理错误——UI 状态变化时,命令可能会失败
性能说明
- 使用
--duration的鼠标移动会以动画方式进行,因此需要时间 - 在大屏幕上定位图像(
locate)可能较慢,尽可能使用区域 - 键盘命令通常执行较快(< 100ms)
- 截图效果取决于屏幕分辨率和区域大小
输出格式
默认情况下,所有命令都会输出结构化 JSON,非常适合 AI Agent 以编程方式使用:
uvx desktop-agent mouse position
# Output: {"success": true, "command": "mouse.position", "timestamp": "2026-01-31T10:00:00Z", "duration_ms": 5, "data": {"position": {"x": 960, "y": 540}}}
响应架构
所有 JSON 响应均遵循以下架构:
{
"success": true,
"command": "category.command",
"timestamp": "2026-01-31T10:00:00Z",
"duration_ms": 150,
"data": { ... },
"error": null
}
错误响应架构
{
"success": false,
"command": "category.command",
"timestamp": "2026-01-31T10:00:00Z",
"duration_ms": 50,
"data": null,
"error": {
"code": "image_not_found",
"message": "Image file 'button.png' not found",
"details": {},
"recoverable": true
}
}
错误代码
| 代码 | 描述 |
|---|---|
success | 命令执行成功 |
invalid_argument | 命令参数无效 |
coordinates_out_of_bounds | 坐标超出屏幕范围 |
image_not_found | 未找到图像文件或屏幕上不存在该图像 |
window_not_found | 未找到目标窗口 |
ocr_failed | OCR 操作失败 |
application_not_found | 未找到应用程序 |
permission_denied | 权限不足 |
platform_not_supported | 不支持该平台 |
timeout | 操作超时 |
unknown_error | 未知错误 |
鼠标移动:
uvx desktop-agent mouse move 960 540
{"success": true, "command": "mouse.move", "timestamp": "...", "duration_ms": 150, "data": {"x": 960, "y": 540, "duration": 0}, "error": null}
屏幕尺寸:
uvx desktop-agent screen size
{"success": true, "command": "screen.size", "timestamp": "...", "duration_ms": 5, "data": {"size": {"width": 1920, "height": 1080}}, "error": null}
查找图像:
uvx desktop-agent screen locate button.png
{"success": true, "command": "screen.locate", "timestamp": "...", "duration_ms": 250, "data": {"image_found": true, "bounding_box": {"left": 100, "top": 200, "width": 50, "height": 30, "center_x": 125, "center_y": 215}}, "error": null}
列出窗口:
uvx desktop-agent app list
{"success": true, "command": "app.list", "timestamp": "...", "duration_ms": 100, "data": {"windows": ["Untitled - Notepad", "Google Chrome", "Visual Studio Code"]}, "error": null}
错误示例:
uvx desktop-agent screen locate missing.png
{"success": false, "command": "screen.locate", "timestamp": "...", "duration_ms": 50, "data": null, "error": {"code": "image_not_found", "message": "Image file 'missing.png' not found", "details": {}, "recoverable": true}}
AI Agent 高效使用指南
本节将介绍 AI Agent 如何通过优化的命令序列和最佳实践,有效使用此技能。
🎯 核心策略:先观察,再操作
执行操作前务必了解当前状态。这样可以避免点击错误的坐标或在错误的窗口中输入内容。
推荐的初始操作序列:
# 1. Get screen dimensions to understand your workspace
uvx desktop-agent screen size
uvx desktop-agent app list
uvx desktop-agent mouse position
📋 按任务划分的推荐命令序列
打开并与应用程序交互
# ✅ CORRECT: Open, wait, verify, then interact
uvx desktop-agent app open notepad # Step 1: Open app
uvx desktop-agent app list
uvx desktop-agent app focus "Notepad"
uvx desktop-agent keyboard write "Hello World" # Step 4: Now safe to type
# ❌ WRONG: Type immediately without verification
uvx desktop-agent app open notepad
uvx desktop-agent keyboard write "Hello World" # May type in wrong window!
查找并点击 UI 元素(基于图像)
# ✅ CORRECT: Locate first, click if found
uvx desktop-agent screen locate-center button.png --confidence 0.8
# Check if success=true and coordinates are valid
uvx desktop-agent mouse click 125 215 # Use returned coordinates
# ❌ WRONG: Click without verifying element exists
uvx desktop-agent mouse click 125 215 # Might click wrong area!
查找并点击 UI 元素(基于 OCR 文本)
# ✅ CORRECT: Read screen text, then locate specific text
uvx desktop-agent screen read-all-text --active
uvx desktop-agent screen locate-text-coordinates "Save" --active
# Use returned coordinates to click
# For window-specific OCR:
uvx desktop-agent screen locate-text-coordinates "OK" --window "Dialog Title"
填写包含多个字段的表单
# ✅ CORRECT: Click each field explicitly before typing
uvx desktop-agent mouse click 300 200 # Click first field
uvx desktop-agent keyboard write "John Doe"
uvx desktop-agent mouse click 300 250 # Click second field (more reliable)
uvx desktop-agent keyboard write "[email protected]"
uvx desktop-agent mouse click 300 300 # Click third field
uvx desktop-agent keyboard write "555-1234"
# OR use Tab navigation (less reliable if field order changes)
uvx desktop-agent mouse click 300 200
uvx desktop-agent keyboard write "John Doe"
uvx desktop-agent keyboard press tab
uvx desktop-agent keyboard write "[email protected]"
uvx desktop-agent keyboard press tab
uvx desktop-agent keyboard write "555-1234"
uvx desktop-agent keyboard press enter # Submit
截取指定区域的屏幕截图以供分析
# ✅ CORRECT: Screenshot specific windows for faster processing
uvx desktop-agent app list --json # Find exact window title
uvx desktop-agent screen screenshot app.png --window "Google Chrome"
# For active window only
uvx desktop-agent screen screenshot active.png --active
# Full screen only when necessary (slower, larger file)
uvx desktop-agent screen size
uvx desktop-agent screen screenshot full.png
安全地拖放
# ✅ CORRECT: Move to start, verify position, then drag
uvx desktop-agent mouse move 100 200 # Move to source
uvx desktop-agent mouse position # Verify position
uvx desktop-agent mouse drag 500 400 --duration 0.5 # Drag to destination
# For precision, use slower duration
uvx desktop-agent mouse drag 500 400 --duration 1.0
🔄 错误恢复模式
未找到窗口时
# Pattern: List windows, find closest match, retry
uvx desktop-agent app focus "Chrome" # Fails with window_not_found
uvx desktop-agent app list # See actual window titles
# Output shows: "Google Chrome - My Page"
uvx desktop-agent app focus "Google Chrome" # Use correct title
未找到图像时
# Pattern: Adjust confidence or take new screenshot
uvx desktop-agent screen locate button.png --confidence 0.9
uvx desktop-agent screen locate button.png --confidence 0.7
# If still failing, capture current state for analysis
uvx desktop-agent screen screenshot current.png --active
点击似乎未命中时
# Pattern: Verify coordinates are on screen
uvx desktop-agent screen size # Get screen bounds
uvx desktop-agent screen on-screen 1500 900 # Check if coords are valid
uvx desktop-agent mouse move 1500 900 # Move first to visualize
uvx desktop-agent mouse click # Then click at current position
⚡ 性能优化
尽量减少屏幕截图
# ✅ GOOD: Screenshot only the region you need
uvx desktop-agent screen screenshot button_area.png --region "100,200,200,100"
# ✅ GOOD: Screenshot specific window instead of full screen
uvx desktop-agent screen screenshot chrome.png --window "Google Chrome"
# ❌ SLOW: Full screen capture when you only need a small area
uvx desktop-agent screen screenshot full.png
批量输入键盘内容
# ✅ FASTER: Write entire text at once
uvx desktop-agent keyboard write "This is a complete sentence with all the text."
# ❌ SLOWER: Multiple write commands
uvx desktop-agent keyboard write "This is "
uvx desktop-agent keyboard write "a complete "
uvx desktop-agent keyboard write "sentence."
尽可能使用快捷键代替鼠标
# ✅ FASTER: Use keyboard shortcuts
uvx desktop-agent keyboard hotkey "ctrl,s" # Save
uvx desktop-agent keyboard hotkey "ctrl,a" # Select all
uvx desktop-agent keyboard hotkey "ctrl,shift,s" # Save as
# ❌ SLOWER: Navigate menu with mouse
uvx desktop-agent mouse click 50 30 # Click File menu
uvx desktop-agent mouse click 60 80 # Click Save option
🛡️ 防御式编程模式
始终验证关键操作
# Before destructive action, confirm with user
uvx desktop-agent message confirm "This will delete all files. Continue?" --title "Warning"
# Check output: if "Cancel" was clicked, abort operation
使用 JSON 模式以可靠地解析
# ✅ RELIABLE: Parse structured JSON output
uvx desktop-agent screen locate button.png
# Parse: {"success": true, "data": {"center_x": 125, "center_y": 215}}
# ❌ FRAGILE: Parse text output
uvx desktop-agent screen locate button.png
# Parse: "Found at: Box(left=100, top=200, width=50, height=30)"
多步骤操作前进行验证
# Multi-step file operation with validation
uvx desktop-agent app list
uvx desktop-agent screen locate-text-coordinates "File" --active
uvx desktop-agent mouse click <returned_x> <returned_y>
uvx desktop-agent screen locate-text-coordinates "Save As" --active
uvx desktop-agent mouse click <returned_x> <returned_y>
🎮 平台特定注意事项
Windows
# Common Windows shortcuts
uvx desktop-agent keyboard hotkey "win,d" # Show desktop
uvx desktop-agent keyboard hotkey "win,e" # Open Explorer
uvx desktop-agent keyboard hotkey "alt,tab" # Switch windows
uvx desktop-agent keyboard hotkey "win,r" # Run dialog
# Open apps by name
uvx desktop-agent app open notepad
uvx desktop-agent app open calc
uvx desktop-agent app open mspaint
macOS
# Common macOS shortcuts (use 'command' for Cmd key)
uvx desktop-agent keyboard hotkey "command,space" # Spotlight
uvx desktop-agent keyboard hotkey "command,tab" # App switcher
uvx desktop-agent keyboard hotkey "command,q" # Quit app
uvx desktop-agent keyboard hotkey "command,shift,3" # Screenshot
# Open apps
uvx desktop-agent app open "Safari"
uvx desktop-agent app open "TextEdit"
Linux
# Open apps (uses xdg-open or direct command)
uvx desktop-agent app open firefox
uvx desktop-agent app open gedit
# Common shortcuts may vary by DE
uvx desktop-agent keyboard hotkey "alt,f2" # Run dialog (many DEs)
📊 决策树:选择合适的命令
Want to interact with an app?
├── App not running → `app open <name>`
├── App running but not focused → `app focus <name>`
└── Need to verify windows → `app list`
Want to find a UI element?
├── Have reference image → `screen locate-center <image>`
├── Know the text label → `screen locate-text-coordinates "<text>"`
└── Need to see all text → `screen read-all-text --active`
Want to click something?
├── Know exact coordinates → `mouse click <x> <y>`
├── Need to find first → Use locate commands above, then click returned coords
└── Not sure if on screen → `screen on-screen <x> <y>` first
Want to type something?
├── Regular text → `keyboard write "<text>"`
├── Keyboard shortcut → `keyboard hotkey "<key1>,<key2>"`
├── Single key press → `keyboard press <key>`
└── Multiple of same key → `keyboard press <key> --presses N`
面向 AI Agent 的集成技巧
- 使用绝对坐标时,务必先检查屏幕尺寸
- 尽可能使用相对定位(例如,获取当前位置并计算偏移量)
- 组合命令以处理复杂工作流
- 执行前先验证(例如,检查屏幕上是否存在图像)
- 使用消息对话框为重要操作向用户提供反馈
- 妥善处理错误——UI 状态发生变化时,命令可能会失败
评论
支持 Markdown