lc-mcp v0.5: 语义化操控(MCP stdio) — OCR文本化+语义点击, win/linux/mac 截图与输入后端
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""输入后端接口。每种后端实现同一套动作:click/move/keypress/type_text。"""
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class InputBackend(ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def click(self, x, y, button='left', clicks=1):
|
||||||
|
...
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def move(self, x, y):
|
||||||
|
...
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def keypress(self, key):
|
||||||
|
...
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def type_text(self, text):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def backend_factory(name, **kwargs):
|
||||||
|
if name == 'mock':
|
||||||
|
from .mock import MockBackend
|
||||||
|
return MockBackend(**kwargs)
|
||||||
|
if name == 'sendinput':
|
||||||
|
from .sendinput_win import SendInputBackend
|
||||||
|
return SendInputBackend(**kwargs)
|
||||||
|
if name == 'interception':
|
||||||
|
from .interception_win import InterceptionBackend
|
||||||
|
return InterceptionBackend(**kwargs)
|
||||||
|
raise ValueError('unknown backend: %s' % name)
|
||||||
@@ -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}
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Interception 物理输入后端(Windows + Interception 驱动)。
|
||||||
|
|
||||||
|
输入在驱动层转为真实 HID 事件(对应用透明,非 SendInput 虚拟输入)。
|
||||||
|
需要: install-interception.exe /install 后重启系统,且本 dll 与驱动共存。
|
||||||
|
"""
|
||||||
|
import ctypes
|
||||||
|
import time
|
||||||
|
|
||||||
|
from ctypes import wintypes
|
||||||
|
|
||||||
|
# ---- Interception 常量 ----
|
||||||
|
MOUSE_LEFTBUTTON_DOWN = 0x001
|
||||||
|
MOUSE_LEFTBUTTON_UP = 0x002
|
||||||
|
MOUSE_RIGHTBUTTON_DOWN = 0x004
|
||||||
|
MOUSE_RIGHTBUTTON_UP = 0x008
|
||||||
|
MOUSE_MIDDLEBUTTON_DOWN = 0x010
|
||||||
|
MOUSE_MIDDLEBUTTON_UP = 0x020
|
||||||
|
|
||||||
|
MOUSE_MOVE_RELATIVE = 0x000
|
||||||
|
MOUSE_MOVE_ABSOLUTE = 0x001
|
||||||
|
MOUSE_VIRTUAL_DESKTOP = 0x002
|
||||||
|
|
||||||
|
KEY_DOWN = 0x00
|
||||||
|
KEY_UP = 0x01
|
||||||
|
KEY_E0 = 0x02
|
||||||
|
|
||||||
|
# ---- Set 1 扫描码 ----
|
||||||
|
SCAN_ALPHA = {chr(c): 0x1E + i for i, c in enumerate(range(ord('A'), ord('Z') + 1))}
|
||||||
|
# 上面的映射只在 A-P 连续,Q-Z 不连续,改为显式表
|
||||||
|
SCAN = {
|
||||||
|
'a': 0x1E, 'b': 0x30, 'c': 0x2E, 'd': 0x20, 'e': 0x12, 'f': 0x21,
|
||||||
|
'g': 0x22, 'h': 0x23, 'i': 0x17, 'j': 0x24, 'k': 0x25, 'l': 0x26,
|
||||||
|
'm': 0x32, 'n': 0x31, 'o': 0x18, 'p': 0x19, 'q': 0x10, 'r': 0x13,
|
||||||
|
's': 0x1F, 't': 0x14, 'u': 0x16, 'v': 0x2F, 'w': 0x11, 'x': 0x2D,
|
||||||
|
'y': 0x15, 'z': 0x2C,
|
||||||
|
'1': 0x02, '2': 0x03, '3': 0x04, '4': 0x05, '5': 0x06,
|
||||||
|
'6': 0x07, '7': 0x08, '8': 0x09, '9': 0x0A, '0': 0x0B,
|
||||||
|
'enter': 0x1C, 'return': 0x1C, 'esc': 0x01, 'space': 0x39, 'tab': 0x0F,
|
||||||
|
'ctrl': 0x1D, 'alt': 0x38, 'lshift': 0x2A, 'rshift': 0x36, 'shift': 0x2A,
|
||||||
|
'f1': 0x3B, 'f2': 0x3C, 'f3': 0x3D, 'f4': 0x3E, 'f5': 0x3F, 'f6': 0x40,
|
||||||
|
'f7': 0x41, 'f8': 0x42, 'f9': 0x43, 'f10': 0x44, 'f11': 0x57, 'f12': 0x58,
|
||||||
|
',': 0x33, '.': 0x34, '/': 0x35, ';': 0x27, "'": 0x28, '[': 0x1A,
|
||||||
|
']': 0x1B, '-': 0x0C, '=': 0x0D, '`': 0x29, '\\': 0x2B,
|
||||||
|
}
|
||||||
|
UPPER_SCAN = { # 大写所需符号的扫描码(按住 Shift 时)
|
||||||
|
'!': 0x02, '@': 0x03, '#': 0x04, '$': 0x05, '%': 0x06, '^': 0x07,
|
||||||
|
'&': 0x08, '*': 0x09, '(': 0x0A, ')': 0x0B, '_': 0x0C, '+': 0x0D,
|
||||||
|
'{': 0x1A, '}': 0x1B, ':': 0x27, '"': 0x28, '<': 0x33, '>': 0x34,
|
||||||
|
'?': 0x35, '~': 0x29, '|': 0x2B,
|
||||||
|
}
|
||||||
|
|
||||||
|
MAX_DEVICES = 64 # 键盘下游设备数量通常 <= 16,鼠标 <= 16;放宽到 64 覆盖异常
|
||||||
|
|
||||||
|
|
||||||
|
class MouseStroke(ctypes.Structure):
|
||||||
|
_fields_ = [("state", ctypes.c_ushort), ("flags", ctypes.c_ushort),
|
||||||
|
("rolling", ctypes.c_short), ("x", ctypes.c_ulong),
|
||||||
|
("y", ctypes.c_ulong), ("information", ctypes.c_ulong)]
|
||||||
|
|
||||||
|
|
||||||
|
class KeyStroke(ctypes.Structure):
|
||||||
|
_fields_ = [("code", ctypes.c_ushort), ("state", ctypes.c_ushort),
|
||||||
|
("information", ctypes.c_ulong)]
|
||||||
|
|
||||||
|
|
||||||
|
class InterceptionBackend:
|
||||||
|
|
||||||
|
def __init__(self, dll_path=None):
|
||||||
|
if dll_path is None:
|
||||||
|
dll_path = r'C:\Program Files\Interception\interception.dll'
|
||||||
|
try:
|
||||||
|
self.dll = ctypes.CDLL(dll_path)
|
||||||
|
except OSError:
|
||||||
|
# 兜底:尝试系统 PATH / 常见目录
|
||||||
|
self.dll = ctypes.CDLL('interception.dll')
|
||||||
|
self._api()
|
||||||
|
self._user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||||
|
self.ctx = self.dll.interception_create_context()
|
||||||
|
if not self.ctx:
|
||||||
|
raise RuntimeError('interception_create_context failed (驱动未安装/未重启?)')
|
||||||
|
self._kbd_devs, self._mou_devs = self._discover()
|
||||||
|
self._scr_w = self._user32.GetSystemMetrics(0)
|
||||||
|
self._scr_h = self._user32.GetSystemMetrics(1)
|
||||||
|
|
||||||
|
def _api(self):
|
||||||
|
d = self.dll
|
||||||
|
d.interception_create_context.restype = ctypes.c_void_p
|
||||||
|
d.interception_create_context.argtypes = []
|
||||||
|
d.interception_destroy_context.argtypes = [ctypes.c_void_p]
|
||||||
|
d.interception_open_device.restype = ctypes.c_bool
|
||||||
|
d.interception_open_device.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
||||||
|
d.interception_close_device.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
||||||
|
d.interception_get_hardware_id.restype = ctypes.c_int
|
||||||
|
d.interception_get_hardware_id.argtypes = [ctypes.c_void_p, ctypes.c_int,
|
||||||
|
ctypes.c_char_p, ctypes.c_int]
|
||||||
|
d.interception_send.restype = ctypes.c_int
|
||||||
|
d.interception_send.argtypes = [ctypes.c_void_p, ctypes.c_int,
|
||||||
|
ctypes.c_void_p, ctypes.c_size_t]
|
||||||
|
|
||||||
|
def _discover(self):
|
||||||
|
kbd, mou = [], []
|
||||||
|
buf = ctypes.create_string_buffer(256)
|
||||||
|
for dev in range(1, MAX_DEVICES + 1):
|
||||||
|
if not self.dll.interception_open_device(self.ctx, dev):
|
||||||
|
continue
|
||||||
|
n = self.dll.interception_get_hardware_id(self.ctx, dev, buf, 256)
|
||||||
|
hid = buf.value.decode('ascii', 'replace').lower() if n > 0 else ''
|
||||||
|
if 'keyboard' in hid:
|
||||||
|
kbd.append(dev)
|
||||||
|
elif 'mouse' in hid:
|
||||||
|
mou.append(dev)
|
||||||
|
return kbd, mou
|
||||||
|
|
||||||
|
# ---- 底层发送 ----
|
||||||
|
def _kbd_stroke(self, code, state):
|
||||||
|
ks = KeyStroke()
|
||||||
|
ks.code = ctypes.c_ushort(code)
|
||||||
|
ks.state = ctypes.c_ushort(state)
|
||||||
|
for dev in self._kbd_devs or [1]:
|
||||||
|
try:
|
||||||
|
self.dll.interception_send(self.ctx, dev, ctypes.byref(ks), ctypes.sizeof(ks))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _mouse_stroke(self, state=0, flags=0, x=0, y=0):
|
||||||
|
ms = MouseStroke()
|
||||||
|
ms.state = ctypes.c_ushort(state)
|
||||||
|
ms.flags = ctypes.c_ushort(flags)
|
||||||
|
ms.x = ctypes.c_ulong(int(x))
|
||||||
|
ms.y = ctypes.c_ulong(int(y))
|
||||||
|
for dev in self._mou_devs or [17]:
|
||||||
|
try:
|
||||||
|
self.dll.interception_send(self.ctx, dev, ctypes.byref(ms), ctypes.sizeof(ms))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# ---- 公开动作 ----
|
||||||
|
def move(self, x, y):
|
||||||
|
nx = int(int(x) * 65535 / self._scr_w) if self._scr_w else 0
|
||||||
|
ny = int(int(y) * 65535 / self._scr_h) if self._scr_h else 0
|
||||||
|
self._mouse_stroke(state=0, flags=MOUSE_MOVE_ABSOLUTE, x=nx, y=ny)
|
||||||
|
time.sleep(0.03)
|
||||||
|
return {'ok': True, 'backend': 'interception'}
|
||||||
|
|
||||||
|
def click(self, x, y, button='left', clicks=1):
|
||||||
|
self.move(x, y)
|
||||||
|
time.sleep(0.05)
|
||||||
|
if button == 'left':
|
||||||
|
down, up = MOUSE_LEFTBUTTON_DOWN, MOUSE_LEFTBUTTON_UP
|
||||||
|
else:
|
||||||
|
down, up = MOUSE_RIGHTBUTTON_DOWN, MOUSE_RIGHTBUTTON_UP
|
||||||
|
for _ in range(max(1, int(clicks))):
|
||||||
|
self._mouse_stroke(state=down)
|
||||||
|
time.sleep(0.04)
|
||||||
|
self._mouse_stroke(state=up)
|
||||||
|
time.sleep(0.05)
|
||||||
|
return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'interception'}
|
||||||
|
|
||||||
|
def _scancode(self, key):
|
||||||
|
k = str(key).lower()
|
||||||
|
return SCAN.get(k)
|
||||||
|
|
||||||
|
def keypress(self, key):
|
||||||
|
k = str(key)
|
||||||
|
code = SCAN.get(k.lower())
|
||||||
|
if code is None:
|
||||||
|
return {'ok': False, 'error': 'unknown key %s' % key}
|
||||||
|
self._kbd_stroke(code, KEY_DOWN)
|
||||||
|
time.sleep(0.04)
|
||||||
|
self._kbd_stroke(code, KEY_UP)
|
||||||
|
time.sleep(0.02)
|
||||||
|
return {'ok': True, 'key': k, 'backend': 'interception'}
|
||||||
|
|
||||||
|
def type_text(self, text):
|
||||||
|
for ch in str(text):
|
||||||
|
low = ch.lower()
|
||||||
|
if ch.isupper() and low in SCAN:
|
||||||
|
self._kbd_stroke(SCAN['shift'], KEY_DOWN)
|
||||||
|
self._kbd_stroke(SCAN[low], KEY_DOWN)
|
||||||
|
self._kbd_stroke(SCAN[low], KEY_UP)
|
||||||
|
self._kbd_stroke(SCAN['shift'], KEY_UP)
|
||||||
|
elif ch.lower() in SCAN:
|
||||||
|
code = SCAN[low]
|
||||||
|
self._kbd_stroke(code, KEY_DOWN)
|
||||||
|
self._kbd_stroke(code, KEY_UP)
|
||||||
|
elif ch in UPPER_SCAN:
|
||||||
|
self._kbd_stroke(SCAN['shift'], KEY_DOWN)
|
||||||
|
self._kbd_stroke(UPPER_SCAN[ch], KEY_DOWN)
|
||||||
|
self._kbd_stroke(UPPER_SCAN[ch], KEY_UP)
|
||||||
|
self._kbd_stroke(SCAN['shift'], KEY_UP)
|
||||||
|
else:
|
||||||
|
return {'ok': False, 'error': 'untypable char %r' % ch}
|
||||||
|
time.sleep(0.015)
|
||||||
|
return {'ok': True, 'typed': str(text), 'backend': 'interception'}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Mock 后端:记录动作,无真实输入。开发/测试用。"""
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class MockBackend:
|
||||||
|
|
||||||
|
def __init__(self, log=None):
|
||||||
|
self.log = log if log is not None else []
|
||||||
|
self._screen = (1920, 1080)
|
||||||
|
|
||||||
|
def click(self, x, y, button='left', clicks=1):
|
||||||
|
self.log.append({'act': 'click', 'x': int(x), 'y': int(y),
|
||||||
|
'button': button, 'clicks': int(clicks)})
|
||||||
|
time.sleep(0.02)
|
||||||
|
return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'mock'}
|
||||||
|
|
||||||
|
def move(self, x, y):
|
||||||
|
self.log.append({'act': 'move', 'x': int(x), 'y': int(y)})
|
||||||
|
time.sleep(0.01)
|
||||||
|
return {'ok': True, 'backend': 'mock'}
|
||||||
|
|
||||||
|
def keypress(self, key):
|
||||||
|
self.log.append({'act': 'keypress', 'key': str(key)})
|
||||||
|
time.sleep(0.01)
|
||||||
|
return {'ok': True, 'key': str(key), 'backend': 'mock'}
|
||||||
|
|
||||||
|
def type_text(self, text):
|
||||||
|
self.log.append({'act': 'type_text', 'text': str(text)})
|
||||||
|
time.sleep(0.01 * len(text))
|
||||||
|
return {'ok': True, 'typed': str(text), 'backend': 'mock'}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""SendInput 后端(Windows,ctypes user32)。
|
||||||
|
当前线上 lc_mcp.py 用的机制;作为 interception 不可用时的兜底。
|
||||||
|
"""
|
||||||
|
import ctypes
|
||||||
|
import time
|
||||||
|
|
||||||
|
from ctypes import wintypes
|
||||||
|
|
||||||
|
INPUT_MOUSE = 0
|
||||||
|
INPUT_KEYBOARD = 1
|
||||||
|
KEYEVENTF_KEYUP = 0x0002
|
||||||
|
KEYEVENTF_UNICODE = 0x0004
|
||||||
|
MOUSEEVENTF_LEFTDOWN = 0x0002
|
||||||
|
MOUSEEVENTF_LEFTUP = 0x0004
|
||||||
|
MOUSEEVENTF_RIGHTDOWN = 0x0008
|
||||||
|
MOUSEEVENTF_RIGHTUP = 0x0010
|
||||||
|
|
||||||
|
|
||||||
|
class MOUSEINPUT(ctypes.Structure):
|
||||||
|
_fields_ = [("dx", wintypes.LONG), ("dy", wintypes.LONG),
|
||||||
|
("mouseData", wintypes.DWORD), ("dwFlags", wintypes.DWORD),
|
||||||
|
("time", wintypes.DWORD), ("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
|
||||||
|
|
||||||
|
|
||||||
|
class KEYBDINPUT(ctypes.Structure):
|
||||||
|
_fields_ = [("wVk", wintypes.WORD), ("wScan", wintypes.WORD),
|
||||||
|
("dwFlags", wintypes.DWORD), ("time", wintypes.DWORD),
|
||||||
|
("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
|
||||||
|
|
||||||
|
|
||||||
|
class INPUT(ctypes.Structure):
|
||||||
|
class _I(ctypes.Union):
|
||||||
|
_fields_ = [("mi", MOUSEINPUT), ("ki", KEYBDINPUT)]
|
||||||
|
_anonymous_ = ("i",)
|
||||||
|
_fields_ = [("type", wintypes.DWORD), ("i", _I)]
|
||||||
|
|
||||||
|
|
||||||
|
KEYMAP = {'enter': 0x0D, 'return': 0x0D, 'esc': 0x1B, 'space': 0x20,
|
||||||
|
'tab': 0x09, 'ctrl': 0x11, 'alt': 0x12, 'shift': 0x10,
|
||||||
|
'p': 0x50, 'f4': 0x73, 'f5': 0x74, 'f8': 0x77, 'f12': 0x7B,
|
||||||
|
'1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34, '5': 0x35,
|
||||||
|
'6': 0x36, '7': 0x37, '8': 0x38, '9': 0x39, '0': 0x30}
|
||||||
|
|
||||||
|
|
||||||
|
class SendInputBackend:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||||
|
|
||||||
|
def _send_input(self, *inputs):
|
||||||
|
n = len(inputs)
|
||||||
|
arr = (INPUT * n)(*inputs)
|
||||||
|
return self._user32.SendInput(n, arr, ctypes.sizeof(INPUT))
|
||||||
|
|
||||||
|
def click(self, x, y, button='left', clicks=1):
|
||||||
|
self._user32.SetCursorPos(int(x), int(y))
|
||||||
|
time.sleep(0.03)
|
||||||
|
down_flag = MOUSEEVENTF_LEFTDOWN if button == 'left' else MOUSEEVENTF_RIGHTDOWN
|
||||||
|
up_flag = MOUSEEVENTF_LEFTUP if button == 'left' else MOUSEEVENTF_RIGHTUP
|
||||||
|
for _ in range(max(1, int(clicks))):
|
||||||
|
down = INPUT(); down.type = INPUT_MOUSE; down.mi.dwFlags = down_flag
|
||||||
|
up = INPUT(); up.type = INPUT_MOUSE; up.mi.dwFlags = up_flag
|
||||||
|
self._send_input(down, up)
|
||||||
|
time.sleep(0.05)
|
||||||
|
return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'sendinput'}
|
||||||
|
|
||||||
|
def move(self, x, y):
|
||||||
|
self._user32.SetCursorPos(int(x), int(y))
|
||||||
|
time.sleep(0.01)
|
||||||
|
return {'ok': True, 'backend': 'sendinput'}
|
||||||
|
|
||||||
|
def keypress(self, key):
|
||||||
|
name = str(key).lower()
|
||||||
|
vk = KEYMAP.get(name)
|
||||||
|
if vk is None and len(name) == 1:
|
||||||
|
vk = ord(name.upper()) if name.isalpha() else ord(name)
|
||||||
|
if vk is None:
|
||||||
|
return {'ok': False, 'error': 'unknown key %s' % key}
|
||||||
|
self._press(vk)
|
||||||
|
return {'ok': True, 'key': str(key), 'backend': 'sendinput'}
|
||||||
|
|
||||||
|
def _press(self, vk):
|
||||||
|
ki = INPUT(); ki.type = INPUT_KEYBOARD; ki.ki.wVk = vk
|
||||||
|
self._send_input(ki)
|
||||||
|
time.sleep(0.04)
|
||||||
|
ki2 = INPUT(); ki2.type = INPUT_KEYBOARD; ki2.ki.wVk = vk
|
||||||
|
ki2.ki.dwFlags = KEYEVENTF_KEYUP
|
||||||
|
self._send_input(ki2)
|
||||||
|
|
||||||
|
def type_text(self, text):
|
||||||
|
for ch in str(text):
|
||||||
|
ki = INPUT(); ki.type = INPUT_KEYBOARD
|
||||||
|
ki.ki.wScan = ord(ch); ki.ki.dwFlags = KEYEVENTF_UNICODE
|
||||||
|
ki2 = INPUT(); ki2.type = INPUT_KEYBOARD
|
||||||
|
ki2.ki.wScan = ord(ch)
|
||||||
|
ki2.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP
|
||||||
|
self._send_input(ki, ki2)
|
||||||
|
time.sleep(0.01)
|
||||||
|
return {'ok': True, 'typed': str(text), 'backend': 'sendinput'}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Linux uinput 物理输入后端(虚拟内核 evdev 设备,需 root 或 uinput 组权限)。
|
||||||
|
比 xdotool 更接近硬件:内核层面人为物理设备,无 X 层痕迹。
|
||||||
|
依赖: python3-uinput 或手工 ioctl。
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import struct
|
||||||
|
|
||||||
|
try:
|
||||||
|
import uinput
|
||||||
|
HAVE_UINPUT = True
|
||||||
|
except ImportError:
|
||||||
|
HAVE_UINPUT = False
|
||||||
|
|
||||||
|
|
||||||
|
class UinputBackend:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if not HAVE_UINPUT:
|
||||||
|
raise RuntimeError('python3-uinput not installed')
|
||||||
|
self._ctx = None
|
||||||
|
|
||||||
|
def _ensure(self):
|
||||||
|
if self._ctx is None:
|
||||||
|
from uinput import Device, KEY_A, KEY_Z, KEY_ENTER, KEY_ESC, KEY_SPACE, KEY_TAB, KEY_F4, KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_BACKSPACE, REL_X, REL_Y, BTN_LEFT, BTN_RIGHT
|
||||||
|
events = (
|
||||||
|
# 全键盘范围(够用): A-Z + 数字 + 常用
|
||||||
|
*[key for key in range(KEY_A, KEY_Z + 1)],
|
||||||
|
KEY_ENTER, KEY_ESC, KEY_SPACE, KEY_TAB, KEY_F4,
|
||||||
|
KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_BACKSPACE,
|
||||||
|
REL_X, REL_Y, BTN_LEFT, BTN_RIGHT,
|
||||||
|
)
|
||||||
|
self._dev = Device(events)
|
||||||
|
return self._dev
|
||||||
|
|
||||||
|
def _key(self, code):
|
||||||
|
dev = self._ensure()
|
||||||
|
dev.emit(code, 1)
|
||||||
|
dev.emit(code, 0)
|
||||||
|
time.sleep(0.03)
|
||||||
|
|
||||||
|
def move(self, x, y):
|
||||||
|
# uinput 只有相对位移; 需要知道当前位置。先简单相对 0 通过 xdotool 定位, 或 https 轮询
|
||||||
|
# 简化: 用 python-xlib 查指针位置 -> 计算 delta
|
||||||
|
try:
|
||||||
|
from Xlib import display
|
||||||
|
d = display.Display()
|
||||||
|
root = d.screen().root
|
||||||
|
qp = root.query_pointer()
|
||||||
|
curx, cury = qp.root_x, qp.root_y
|
||||||
|
dx, dy = int(x) - curx, int(y) - cury
|
||||||
|
dev = self._ensure()
|
||||||
|
dev.emit(REL_X, dx)
|
||||||
|
dev.emit(REL_Y, dy)
|
||||||
|
d.sync()
|
||||||
|
except Exception:
|
||||||
|
return {'ok': False, 'error': 'uinput move needs python-xlib'}
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
def click(self, x, y, button='left', times=1):
|
||||||
|
self.move(x, y)
|
||||||
|
dev = self._ensure()
|
||||||
|
btn = BTN_LEFT if button == 'left' else BTN_RIGHT
|
||||||
|
for _ in range(times):
|
||||||
|
dev.emit(btn, 1)
|
||||||
|
time.sleep(0.05)
|
||||||
|
dev.emit(btn, 0)
|
||||||
|
time.sleep(0.05)
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
def keypress(self, key):
|
||||||
|
mapping = {'enter': KEY_ENTER, 'esc': KEY_ESC, 'space': KEY_SPACE,
|
||||||
|
'tab': KEY_TAB, 'f4': KEY_F4, 'ctrl': KEY_LEFTCTRL,
|
||||||
|
'shift': KEY_LEFTSHIFT, 'alt': KEY_LEFTALT, 'backspace': KEY_BACKSPACE}
|
||||||
|
k = key.lower()
|
||||||
|
if k in mapping:
|
||||||
|
self._key(mapping[k])
|
||||||
|
return {'ok': True}
|
||||||
|
if len(k) == 1 and 'a' <= k <= 'z':
|
||||||
|
from uinput import KEY_A
|
||||||
|
self._key(KEY_A + (ord(k) - ord('a')))
|
||||||
|
return {'ok': True}
|
||||||
|
return {'ok': False, 'error': 'unsupported key %s' % key}
|
||||||
|
|
||||||
|
def type_text(self, text):
|
||||||
|
for ch in text:
|
||||||
|
if 'a' <= ch.lower() <= 'z':
|
||||||
|
self.keypress(ch.lower())
|
||||||
|
elif ch == ' ':
|
||||||
|
self.keypress('space')
|
||||||
|
else:
|
||||||
|
return {'ok': False, 'error': 'unsupported char %r' % ch}
|
||||||
|
time.sleep(0.05)
|
||||||
|
return {'ok': True}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Linux X11 输入后端:xdotool(X 事件注入,xdotool 需已安装)。
|
||||||
|
物理化升级路径: uinput 后端(虚拟内核 evdev 设备)。
|
||||||
|
"""
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def _run(args):
|
||||||
|
return subprocess.run(args, capture_output=True, text=True, timeout=30)
|
||||||
|
|
||||||
|
|
||||||
|
class XdotoolBackend:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if not shutil.which('xdotool'):
|
||||||
|
raise RuntimeError('xdotool not found (apt install xdotool)')
|
||||||
|
|
||||||
|
def _screen(self):
|
||||||
|
try:
|
||||||
|
r = _run(['xdotool', 'getdisplaygeometry'])
|
||||||
|
w, h = r.stdout.strip().split()
|
||||||
|
return int(w), int(h)
|
||||||
|
except Exception:
|
||||||
|
return 1920, 1080
|
||||||
|
|
||||||
|
def move(self, x, y):
|
||||||
|
_run(['xdotool', 'mousemove', str(int(x)), str(int(y))])
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
def click(self, x, y, button='left', times=1):
|
||||||
|
btn = {'left': '1', 'right': '3', 'middle': '2'}.get(button, '1')
|
||||||
|
_run(['xdotool', 'mousemove', str(int(x)), str(int(y))])
|
||||||
|
_run(['xdotool', 'click', '--repeat', str(times), '--delay', '60', btn])
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
def keypress(self, key):
|
||||||
|
key = key.lower()
|
||||||
|
# xdotool 键名映射
|
||||||
|
mapping = {'enter': 'Return', 'esc': 'Escape', 'space': 'space',
|
||||||
|
'tab': 'Tab', 'alt': 'Alt_L', 'f4': 'F4', 'ctrl': 'Control_L',
|
||||||
|
'shift': 'Shift_L', 'backspace': 'BackSpace'}
|
||||||
|
k = mapping.get(key, key)
|
||||||
|
_run(['xdotool', 'key', k])
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
def type_text(self, text):
|
||||||
|
_run(['xdotool', 'type', '--delay', '60', text])
|
||||||
|
return {'ok': True}
|
||||||
Reference in New Issue
Block a user