【免费下载链接】vibe-mouse
Core open-source Vibecoding interactive mouse project, belonging to a self-developed open-source system including the nation's first open-source robot actuator and stepless mobile intelligent monitoring solution.
vibe-mouse 是一款 Vibecoding 交互鼠标开源项目,采用可插拔适配器架构。本文将带你通过继承BaseAdapter抽象基类,用不到 30 行 Python 代码把任意新外设(智能按键、旋钮、脚踏、IoT 设备)接入 vibe-mouse,全程无需改动核心逻辑。
一、30 秒搞懂 vibe-mouse 的外设架构
vibe-mouse 的核心数据流只有一句话:
外设事件 → 适配器(Adapter)→ 统一 Event → 外设管理器 → 执行器(发送快捷键)
也就是说,每一种外设只需实现自己的"适配器",把原始信号翻译成统一的Event对象上报即可,后续的路由、快捷键发送、反馈(声音/LED/浮窗)全部由框架接管。
| 角色 | 文件 | 职责 |
|---|---|---|
| 适配器基类 | core/adapters/base.py | 定义start/stop/_run/_emit标准接口 |
| 统一事件 | core/event.py | Event、DeviceType、InputType数据模型 |
| 适配器注册表 | core/adapters/init.py | ADAPTER_REGISTRY把设备类型映射到适配器类 |
| 外设管理器 | core/device_manager.py | 注册、启动适配器,按device_mappings路由事件 |
BaseAdapter已经帮你处理了线程管理(start()在独立守护线程中运行_run())、运行状态(self._running)、事件上报(self._emit(event)自动容错)。你唯一必须实现的是_run()方法。
二、第一步:继承 BaseAdapter 并实现 _run()
以一个"串口智能按键"为例,完整适配器大约 30 行:
# core/adapters/button_adapter.py import time from .base import BaseAdapter from ..event import Event, DeviceType, InputType class ButtonAdapter(BaseAdapter): """智能按键适配器:监听串口,按键按下时上报事件""" def _run(self): import serial port = self.config.get("port", "COM3") # 从配置读取串口 ser = serial.Serial(port, 9600, timeout=1) while self._running: # 必须检查 _running 才能被 stop() 优雅退出 line = ser.readline().decode("utf-8", "ignore").strip() if line == "PRESS": self._emit(Event( device_type=DeviceType.HID_GENERIC, device_id=self.device_id, input_type=InputType.BUTTON, input_id="press", # 这个 id 会用于配置映射 value=1, )) time.sleep(0.05) ser.close()这里有 4 个关键点,直接决定了你的适配器能否正常工作:
- 继承
BaseAdapter:构造函数签名由基类固定——(device_id, config, on_event),你不需要重写__init__,但也不能漏传参数(框架会自动传入)。 - 只写
_run(),不写start()/stop():基类已实现线程启停逻辑,参见 core/adapters/base.py#L42-L56。 - 循环里检查
self._running:这是唯一的退出条件,stop()通过置位该标志来结束监听。 - 用
self._emit(Event(...))上报:input_id(如"press")是你和config.json之间约定的"暗号",配置里用它绑定动作。raw_data字段可保留原始数据方便调试。
Event的完整字段(device_type/input_type/input_id/value等)定义在 core/event.py#L39-L58。input_type支持BUTTON、AXIS(摇杆/扳机)、DIAL(旋钮)、VOICE、SCAN等多种输入形态,选一个最贴近你外设的即可。
三、第二步:把适配器登记进注册表
vibe-mouse 通过ADAPTER_REGISTRY(设备类型字符串 → 适配器类)动态查找适配器。打开 core/adapters/init.py,只需两行:
from .button_adapter import ButtonAdapter ADAPTER_REGISTRY = { # ... 已有 mouse / keyboard / gamepad 等 ... "button": ButtonAdapter, # 新增一行即可 }DeviceManager.register_device()会在启动时调用get_adapter_class(device_type)找到你的类,并自动完成实例化与启动,见 core/device_manager.py#L32-L58。
四、第三步:在配置中启用并绑定动作
把新设备加到config.json(可参考示例 config.example.json),或用浏览器打开可视化配置工具 config-tool.html 图形化完成:
{ "devices": [ {"id": "desk_button", "type": "button", "config": {"port": "COM3"}, "enabled": true} ], "device_mappings": { "desk_button": {"press": "inline_edit"} } }devices中的type必须与注册表里的 key 完全一致;config会原样传给你_run()里的self.config。device_mappings把input_id映射到内置动作(inline_edit、toggle_chat、accept_diff等),也可以映射到 Skill,格式为"skill:<名称>"。
保存后启动python core/main.py,看到日志[ButtonAdapter] Started / 已启动: desk_button即代表接入成功——按下物理按键,vibe-mouse 会自动向当前 IDE 发送对应快捷键。
五、常见坑与最佳实践
- 依赖用延迟导入:项目惯例是在
_run()内部用importlib.import_module(...)导入第三方库(如 pyserial、bleak),未安装时打印友好提示而不是让整个程序崩溃,可参考 core/adapters/ir_adapter.py#L76-L81 与 core/adapters/network_adapter.py#L41-L47。 - 一个设备一个线程,别自己开线程:
_run()本身就运行在独立线程中,内部只做阻塞式轮询/接收即可。 - 异常不要裸抛:解析失败时
print后继续循环,保证监听器长期存活(见 _emit 的容错写法)。 - 想接入网络类外设?优先看 core/adapters/network_adapter.py,MQTT / WebSocket / HTTP 三种模式都已实现,通常直接配置即可,无需新写适配器。
- 多模式外设怎么组织?参考 IR 适配器的做法:
_run()里读self.config["mode"]分发到_run_serial()/_run_lirc()等子方法,见 core/adapters/ir_adapter.py#L62-L74。
六、小结
接入一个新外设的标准流程只有三步:
- 新建文件,继承
BaseAdapter,实现_run()并用self._emit(Event(...))上报; - 在 core/adapters/init.py 的
ADAPTER_REGISTRY登记一行; - 在
config.json的devices+device_mappings中启用并绑定动作。
得益于这套可插拔架构,无论你想接入脚踏开关、BLE 旋钮还是 MQTT 智能开关,都不需要触碰 vibe-mouse 的任何核心代码——这正是该项目"新增外设无需改动核心逻辑"设计理念的体现。更多架构细节可继续阅读 README.md 的"核心架构"章节。
【免费下载链接】vibe-mouse
Core open-source Vibecoding interactive mouse project, belonging to a self-developed open-source system including the nation's first open-source robot actuator and stepless mobile intelligent monitoring solution.
相关推荐
Jev 聊天助手扩展开发:如何用 30 行代码接入一个新的聊天 App
Jev 聊天助手扩展开发:如何用 30 行代码接入一个新的聊天 App Jev 聊天助手(jev chat jarvis)是一款装在手机上、只读屏幕的「对话副驾
AI 应用大模型交互助手RAGLan Mouse 开发者指南:如何为项目贡献代码和扩展新后端
Lan Mouse 开发者指南:如何为项目贡献代码和扩展新后端 Lan Mouse 是一款跨平台的鼠标和键盘共享软件,类似于 Apple 设备上的 Univer
桌面应用终极对比:test_resnet.r160_in1k与ConvNeXt/ViT模型的参数效率之战
终极对比:test_resnet.r160_in1k与ConvNeXt/ViT模型的参数效率之战 在深度学习模型快速发展的今天, 参数效率 已成为衡量模型实用性
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考