Skip to main content

scapkit_computer_use

跨平台桌面自动化 Python 库,提供屏幕截图、屏幕与系统声音录制、鼠标控制、键盘输入、剪贴板和 subprocess 操作。

当前支持 macOS;库的最低系统版本为 macOS 13.0。 官方预编译 wheel 仅提供 macOS 15+ arm64 版本,这不是源码安装的系统或架构限制。 Windows 目前提供纯 Python subprocess 接口(源码安装);桌面控制扩展仍在开发中。

安装

需要 Python >= 3.10;支持 CPython 3.13/3.14 的 free-threaded 构建,通过 PyPI 安装:

pip install scapkit_computer_use

较早的 macOS(13.0+)或 Intel Mac 可从源码构建,需要安装 Xcode Command Line Tools,并使用包含 ScreenCaptureKit 的 macOS SDK:

pip install --no-binary=scapkit_computer_use scapkit_computer_use

旧系统和 Intel Mac 的实际运行尚未验证;当前 CI 运行环境为 macOS 15 / 26 arm64。

或使用 uv

uv add scapkit_computer_use

可选 MCP Server

源码新增了基于 FastAPI 的 MCP server,供 agent 通过标准 MCP 工具控制设备。 普通安装不引入 MCP/FastAPI 依赖。MCP 服务自 0.1.0 起提供,也可从源码运行:

uv sync --extra mcp
uv run --extra mcp scapkit-mcp

客户端连接 http://127.0.0.1:8000/mcp;也支持通过 python -m scapkit_computer_use_mcp --transport stdio 由客户端直接启动。 MCP 也提供 start_recordingstop_recordingrecording_status,将视频保存到服务端路径。 服务自动提供 agent instructions、操作指南 resource 和 prompt,说明权限、工具使用顺序、 Retina 坐标换算及操作后的验证流程。 可选 MCP 模块支持常规 Python 3.10+ 和 3.14t;上游 CFFI 不支持 3.13t, 这不影响基础库的 3.13t 支持。

详见 MCP 安装、客户端配置与工具说明agent 操作指南

权限

macOS 下需要授予以下系统权限:

  • 辅助功能 (Accessibility):鼠标、键盘控制
  • 屏幕录制 (Screen Recording):屏幕截图、屏幕与系统声音录制;不会采集麦克风
from scapkit_computer_use import check_permission, open_permission_settings

if not check_permission("Accessibility"):
    await open_permission_settings("Accessibility")

功能

显示器信息

所有坐标和尺寸均使用 point(逻辑分辨率),而非物理像素。物理像素 = point × scale_factor。API 中的所有位置参数(鼠标移动、点击等)同样使用 point 坐标。

from scapkit_computer_use import list_displays

displays = list_displays()
# [{"id": 2, "x": 0, "y": 0, "width": 1920, "height": 1080, "scale_factor": 2.0, "is_main": True}]
# width/height 为 point 单位,实际物理像素为 1920×2 = 3840, 1080×2 = 2160

鼠标控制

from scapkit_computer_use import (
    get_mouse_position, move_mouse, move_mouse_relative,
    mouse_click, mouse_scroll, mouse_drag
)

# 获取当前位置
pos = get_mouse_position()  # {"x": 100, "y": 200}

# 平滑移动(绝对坐标)
await move_mouse({"x": 500, "y": 300})

# 瞬间移动
await move_mouse({"x": 500, "y": 300}, smooth=False)

# 相对移动(生成 delta 事件,兼容游戏等指针锁定场景)
await move_mouse_relative({"dx": 100, "dy": 50})

# 瞬间相对移动
await move_mouse_relative({"dx": 100, "dy": 50}, smooth=False)

# 点击
await mouse_click("left")
await mouse_click("right")

# 滚动(方向为内容移动方向)
await mouse_scroll("down", 3)
await mouse_scroll("up", 3)

# 拖拽到目标位置
await mouse_drag({"x": 800, "y": 600})

注意: move_mouse 使用 CGWarpMouseCursorPosition,不会生成鼠标移动的 delta 事件,因此不适用于依赖原始鼠标 delta 的应用(如游戏中的指针锁定)。这类场景请使用 move_mouse_relative,它通过 CGEventCreateMouseEvent 发送包含 deltaX/deltaYkCGEventMouseMoved 事件。

键盘输入

使用跨平台的按键名称,无需关心底层键码:

from scapkit_computer_use import keyboard_click, key_combo

# 按下并释放一个键
await keyboard_click("a")
await keyboard_click("return")

# 组合键
await key_combo("c", {"command"})    # Cmd+C 复制
await key_combo("v", {"command"})    # Cmd+V 粘贴
await key_combo("z", {"command", "shift"})  # Cmd+Shift+Z 重做

支持的按键名称包括:a-z0-9returntabspacedeleteescapef1-f20up/down/left/right 等。完整列表见源码 screen_capture_kit/keys.py

剪贴板

from scapkit_computer_use import set_clipboard, get_clipboard, clipboard_paste

await set_clipboard("你好世界")
text = await get_clipboard()  # "你好世界"

# 直接粘贴到当前输入框(模拟 Cmd+V)
await clipboard_paste()

屏幕截图

基于 macOS ScreenCaptureKit,支持全分辨率 Retina 截图:

from scapkit_computer_use import (
    list_displays, start_capture, stop_capture,
    current_frame_jpg, current_frame_bgra
)

displays = list_displays()
main = next(d for d in displays if d["is_main"])

# 启动截图流
handle = await start_capture(main["id"])

# 获取 JPEG 格式(可设置质量 0-100)
jpg_bytes = await current_frame_jpg(handle, quality=80)

# 获取原始 BGRA 像素数据
frame = await current_frame_bgra(handle)
# {"data": bytes, "width": 3840, "height": 2160, "bytes_per_row": 15360}

# 停止截图
await stop_capture(handle)

屏幕与系统声音录制

指定显示器和保存路径,输出 QuickTime 兼容的 H.264/AAC MP4:

from scapkit_computer_use import start_recording, stop_recording

handle = await start_recording(display_id, "/path/to/recording.mp4", fps=30)
try:
    await do_something()
finally:
    result = await stop_recording(handle)
print(result.path, result.size_bytes, result.duration_s)

默认固定 30 fps,支持指定整数帧率;画面静止时继续使用缓存帧,停止时补齐最后一个 帧区间。只录制系统播放声音,不采集麦克风。VideoToolbox 要求硬件 H.264 编码,视频 默认 video_quality=0.75,可传入其他 0~1 数值调整质量,或用 None 保留编码器策略。 编码器按内容和分辨率分配码率。输出父目录必须存在,已有文件不会被覆盖。 详见 录制接口、生命周期与异常说明

执行 subprocess

run_subprocess() 是异步函数。可执行文件与参数分开传入;执行 shell 命令时, 把 shell 作为可执行文件,并传入 -c/c 等参数。

import sys
from scapkit_computer_use import run_subprocess

result = await run_subprocess(
    sys.executable,
    ["-c", "import os; print(os.getenv('EXAMPLE'))"],
    cwd="/path/to/workdir",
    env={"EXAMPLE": "你好"},
)
print(result.returncode, result.stdout, result.stderr)
# 也可以解包:returncode, stdout, stderr = result

result = await run_subprocess("/bin/zsh", ["-c", "printf '%s' hello"])
# Windows 示例:await run_subprocess("cmd.exe", ["/c", "echo hello"])
参数 含义
executable 可执行文件名称或路径;名称通过加载后的环境 PATH 查找
args=() 参数序列,保留空格和特殊字符;不会自动拼接成 shell 命令
cwd=None 工作目录,默认当前工作目录
env=None 在 shell 环境上新增或覆盖的变量,均为字符串
use_stream=False 默认等待退出,返回退出码和两路文本;为 True 时返回运行中的 SubprocessStream
encoding=None macOS 默认 UTF-8;Windows 默认控制台输出代码页,无控制台时用系统 OEM 代码页
errors="strict" 解码错误默认抛异常;可指定 "replace" 保留其他可解码内容

环境来自重新加载的系统/用户 shell 配置,不继承当前 Python 进程的环境变量。 macOS 读取账户登录 shell 的 login/interactive 配置;Windows 从系统和用户配置构造环境, 再执行 cmd AutoRun。调用方传入的 env 最后合并。详情见 进程执行说明

流式 stdin 接收字符串,stdout/stderr 支持异步 read()readline() 和逐行迭代:

import asyncio

process = await run_subprocess(
    sys.executable, ["-u", "-c", "import sys; print(input()); print('done', file=sys.stderr)"],
    use_stream=True,
)

async def feed():
    process.stdin.write("你好\n")
    await process.stdin.drain()
    process.stdin.close()

async def consume(stream):
    async for line in stream:
        print(line, end="")

await asyncio.gather(feed(), consume(process.stdout), consume(process.stderr))
returncode = await process.wait()
# 或:stdout, stderr = await process.communicate("你好\n")

Windows 程序可能输出 UTF-8、GBK、ANSI 或 UTF-16;无法通用地自动判断。 例如使用 encoding="utf-8"encoding="gbk"encoding="utf-16-le" 明确指定。 同一编码用于该进程的三路文本流;跨块中文由增量解码器处理,换行统一为 \n

开发

# 构建 C 扩展
uv run setup.py build_ext --inplace

# 构建带合成测试支持的扩展,运行不操作桌面的测试
SCAPKIT_TESTING=1 uv run setup.py build_ext --inplace --force
uv run pytest tests/test_native_validation.py tests/test_native_arguments.py tests/test_native_capture.py tests/test_capture_lifecycle.py tests/test_async_safety.py tests/test_process.py

构建环境、并发约定和测试边界见 开发文档, 自动化发布见 发布文档,Codex 接手约定见 AGENTS.md, 本次质量检查和验证边界见 审查记录

stop_capture() 可以重复调用;停止后的新读帧返回 None。启动/停止超时抛出 TimeoutError。多线程读取和停止同一句柄受原生同步保护,但多步键鼠操作需要调用方串行安排。

许可证

MIT

Release files for scapkit-computer-use 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scapkit-computer-use 0.1.1
File Size Uploaded
scapkit_computer_use-0.1.1.tar.gz 68.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for scapkit-computer-use 0.1.1
File
scapkit_computer_use-0.1.1-cp314-cp314t-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 free-threading macOS 15.0+ ARM64 Details
scapkit_computer_use-0.1.1-cp314-cp314-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 macOS 15.0+ ARM64 Details
scapkit_computer_use-0.1.1-cp313-cp313t-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 free-threading macOS 15.0+ ARM64 Details
scapkit_computer_use-0.1.1-cp313-cp313-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 macOS 15.0+ ARM64 Details
scapkit_computer_use-0.1.1-cp312-cp312-macosx_15_0_arm64.whl CPython 3.12 CPython 3.12 macOS 15.0+ ARM64 Details
scapkit_computer_use-0.1.1-cp311-cp311-macosx_15_0_arm64.whl CPython 3.11 CPython 3.11 macOS 15.0+ ARM64 Details
scapkit_computer_use-0.1.1-cp310-cp310-macosx_15_0_arm64.whl CPython 3.10 CPython 3.10 macOS 15.0+ ARM64 Details

Total release size: 717.5 kB

Release files / scapkit_computer_use-0.1.1.tar.gz

Download URL scapkit_computer_use-0.1.1.tar.gz
Size 68.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ae5692b3dbd9d689102d9cf568e888024e704fb9eb2ffc83558cbbf39e542553
BLAKE2b-256 checksum
How to use checksums
dd01c80a5cf6404d1092d7b5cfb2018e9e29338479553f79c80c55caf7996aba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp314-cp314t-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp314-cp314t-macosx_15_0_arm64.whl
Size 93.0 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
ac56f09a4abcae71c7af489521953d0d284b946e4d3c8f030a34b87934c50cd3
BLAKE2b-256 checksum
How to use checksums
c4e04d76844a562943eabc3b8f71ee59d3b60c5ebe5861fd9f6ebc51e00af28f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp314-cp314-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp314-cp314-macosx_15_0_arm64.whl
Size 92.6 kB
Tags CPython 3.14 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
91353681e79d632dd4239f5309f6d1c77334ff9f97c0de07c794a8483e56a20f
BLAKE2b-256 checksum
How to use checksums
326d017ead4207c8c4021952391e66354a67da75d26eae91740c2c5cdcf194c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp313-cp313t-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp313-cp313t-macosx_15_0_arm64.whl
Size 93.0 kB
Tags CPython 3.13 CPython 3.13 free-threading macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
3471c973823613f9ce1f0e884907f42c4ce2af9bdb3a27c279b58b040ce28e33
BLAKE2b-256 checksum
How to use checksums
87aaab09bad4c6439883b4f7deaab257e7f2612efce728da9170a4add0cea6b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp313-cp313-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp313-cp313-macosx_15_0_arm64.whl
Size 92.6 kB
Tags CPython 3.13 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
037b49ef6e66649cb338c5d0efcc357c614f58a74319a009d5a9ade05089573c
BLAKE2b-256 checksum
How to use checksums
4db6a7cd0677ad8ee14e37997e77b4a594660619e22182722f7d808310c92190
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp312-cp312-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp312-cp312-macosx_15_0_arm64.whl
Size 92.7 kB
Tags CPython 3.12 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
df14e127e61499f19f3cbb23e1298a2139ca3550c452636ef08e74ca992b6670
BLAKE2b-256 checksum
How to use checksums
9cc63d450866f1074e99c0496fca8865f6dd0120fad873d652c833a2fe03bf7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp311-cp311-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp311-cp311-macosx_15_0_arm64.whl
Size 92.7 kB
Tags CPython 3.11 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
4b6ceed3cfca023852c3a16c384e2f9706b1f6b8ce1d0cb3d99b033bed3c65f4
BLAKE2b-256 checksum
How to use checksums
22556ed6bb1dceea16fa78facca91e77ce48b2df81352946a4f81861fc20b0e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release files / scapkit_computer_use-0.1.1-cp310-cp310-macosx_15_0_arm64.whl

Download URL scapkit_computer_use-0.1.1-cp310-cp310-macosx_15_0_arm64.whl
Size 92.7 kB
Tags CPython 3.10 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
70f0566ea0ec26f64391985bd9af4bdd7fb711daedaf46d12003833235f17ce0
BLAKE2b-256 checksum
How to use checksums
53123ab0ff9c21a8c20e5d4ad752a0e249af75f02b22e5fb0401e945709cbc3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.0

22 release files

0.1.2

22 release files

This release

0.1.1 This release

8 release files

0.1.0

8 release files

0.0.3

8 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page