lc-mcp v0.5: 语义化操控(MCP stdio) — OCR文本化+语义点击, win/linux/mac 截图与输入后端
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""macOS 输入后端:Quartz CGEvent (需 PyObjC: pip install pyobjc-framework-Quartz)。"""
|
||||
import time
|
||||
|
||||
|
||||
class CGBackend:
|
||||
|
||||
def __init__(self):
|
||||
try:
|
||||
import Quartz
|
||||
self.q = Quartz
|
||||
except ImportError:
|
||||
raise RuntimeError('PyObjC Quartz not installed (pip install pyobjc-framework-Quartz)')
|
||||
|
||||
def _post(self, event):
|
||||
self.q.CGEventPost(self.q.kCGHIDEventTap, event)
|
||||
time.sleep(0.02)
|
||||
|
||||
def move(self, x, y):
|
||||
e = self.q.CGEventCreateMouseEvent(None, self.q.kCGEventMouseMoved, (float(x), float(y)), self.q.kCGMouseButtonLeft)
|
||||
self._post(e)
|
||||
return {'ok': True}
|
||||
|
||||
def click(self, x, y, button='left', times=1):
|
||||
btn = self.q.kCGMouseButtonLeft if button == 'left' else self.q.kCGMouseButtonRight
|
||||
self.move(x, y)
|
||||
for _ in range(times):
|
||||
down = self.q.CGEventCreateMouseEvent(None, self.q.kCGEventLeftMouseDown if button == 'left' else self.q.kCGEventRightMouseDown, (float(x), float(y)), btn)
|
||||
up = self.q.CGEventCreateMouseEvent(None, self.q.kCGEventLeftMouseUp if button == 'left' else self.q.kCGEventRightMouseUp, (float(x), float(y)), btn)
|
||||
self._post(down)
|
||||
self._post(up)
|
||||
return {'ok': True}
|
||||
|
||||
def keypress(self, key):
|
||||
mapping = {'enter': 36, 'esc': 53, 'space': 49, 'tab': 48, 'f4': 96,
|
||||
'ctrl': 59, 'shift': 56, 'alt': 58, 'backspace': 51}
|
||||
k = key.lower()
|
||||
if k in mapping:
|
||||
self._key(mapping[k])
|
||||
return {'ok': True}
|
||||
if len(k) == 1 and 'a' <= k <= 'z':
|
||||
# mac 虚拟键码: a=0, b=11, ... 非连续, 用简单表
|
||||
table = {'a': 0, 'b': 11, 'c': 8, 'd': 2, 'e': 14, 'f': 3, 'g': 5, 'h': 4,
|
||||
'i': 34, 'j': 38, 'k': 40, 'l': 37, 'm': 46, 'n': 45, 'o': 31,
|
||||
'p': 35, 'q': 12, 'r': 15, 's': 1, 't': 17, 'u': 32, 'v': 9,
|
||||
'w': 13, 'x': 7, 'y': 16, 'z': 6}
|
||||
if k in table:
|
||||
self._key(table[k])
|
||||
return {'ok': True}
|
||||
return {'ok': False, 'error': 'unsupported key %s' % key}
|
||||
|
||||
def _key(self, vk):
|
||||
down = self.q.CGEventCreateKeyboardEvent(None, vk, True)
|
||||
up = self.q.CGEventCreateKeyboardEvent(None, vk, False)
|
||||
self._post(down)
|
||||
self._post(up)
|
||||
|
||||
def type_text(self, text):
|
||||
for ch in text:
|
||||
r = self.keypress(ch)
|
||||
if not r.get('ok'):
|
||||
return r
|
||||
time.sleep(0.03)
|
||||
return {'ok': True}
|
||||
Reference in New Issue
Block a user