修复 v0.5 审查问题: platform 改名, 协议健壮性, CJK 匹配, VBS 引号, interception API
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+31
-26
@@ -4,11 +4,24 @@
|
||||
"""
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from ._util import parse_combo
|
||||
|
||||
# 内部键名 -> X keysym
|
||||
KEYSYM = {'enter': 'Return', 'esc': 'Escape', 'space': 'space', 'tab': 'Tab',
|
||||
'backspace': 'BackSpace', 'alt': 'Alt_L', 'ralt': 'Alt_R', 'ctrl': 'Control_L',
|
||||
'rctrl': 'Control_R', 'shift': 'Shift_L', 'lshift': 'Shift_L', 'rshift': 'Shift_R',
|
||||
'lwin': 'Super_L', 'up': 'Up', 'down': 'Down', 'left': 'Left', 'right': 'Right',
|
||||
'home': 'Home', 'end': 'End', 'pageup': 'Prior', 'pagedown': 'Next',
|
||||
'insert': 'Insert', 'delete': 'Delete'}
|
||||
KEYSYM.update({'f%d' % i: 'F%d' % i for i in range(1, 13)})
|
||||
|
||||
|
||||
def _run(args):
|
||||
return subprocess.run(args, capture_output=True, text=True, timeout=30)
|
||||
r = subprocess.run(args, capture_output=True, text=True, timeout=30)
|
||||
if r.returncode != 0:
|
||||
raise RuntimeError('%s failed: %s' % (' '.join(args[:2]), (r.stderr or '').strip()[-200:]))
|
||||
return r
|
||||
|
||||
|
||||
class XdotoolBackend:
|
||||
@@ -17,34 +30,26 @@ class XdotoolBackend:
|
||||
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}
|
||||
_run(['xdotool', 'mousemove', '--sync', str(int(x)), str(int(y))])
|
||||
return {'ok': True, 'backend': 'xdotool'}
|
||||
|
||||
def click(self, x, y, button='left', times=1):
|
||||
def click(self, x, y, button='left', clicks=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}
|
||||
_run(['xdotool', 'mousemove', '--sync', str(int(x)), str(int(y)),
|
||||
'click', '--repeat', str(max(1, int(clicks))), '--delay', '60', btn])
|
||||
return {'ok': True, 'clicked': [int(x), int(y)], 'backend': 'xdotool'}
|
||||
|
||||
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}
|
||||
try:
|
||||
parts = parse_combo(key)
|
||||
except ValueError as e:
|
||||
return {'ok': False, 'error': str(e)}
|
||||
combo = '+'.join(KEYSYM.get(p, p) for p in parts)
|
||||
_run(['xdotool', 'key', '--', combo])
|
||||
return {'ok': True, 'key': str(key), 'backend': 'xdotool'}
|
||||
|
||||
def type_text(self, text):
|
||||
_run(['xdotool', 'type', '--delay', '60', text])
|
||||
return {'ok': True}
|
||||
# '--' 防止以 '-' 开头的文本被当成选项
|
||||
_run(['xdotool', 'type', '--delay', '60', '--', str(text)])
|
||||
return {'ok': True, 'typed': str(text), 'backend': 'xdotool'}
|
||||
|
||||
Reference in New Issue
Block a user