195 lines
7.8 KiB
Python
195 lines
7.8 KiB
Python
# -*- 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'} |