复核驱动输入与窗口补丁,修复接管互斥和异常清理

Browse Source
This commit is contained in:
yws
2026-10-22 06:52:39 +00:00
parent e2de126687
commit 46b5af01cd
10 changed files with 667 additions and 176 deletions
+64 -24
View File
@@ -2,18 +2,18 @@
"""罗技驱动级输入(logitech.driver.dll)。 """罗技驱动级输入(logitech.driver.dll)。
为什么需要它: 用 SendInput/mouse_event 注入的点击, 游戏侧仍可能识别为"模拟"。 为什么需要它: 用 SendInput/mouse_event 注入的点击, 游戏侧仍可能识别为"模拟"。
这个 DLL 通过罗技驱动的虚拟 HID 设备发事件, 系统层面等同真鼠标。 这个 DLL 通过罗技驱动的虚拟 HID 设备发事件。
⛔ 两个必须知道的前提(2026-10-22 实测): ⛔ 两个必须知道的前提(2026-10-22 实测):
1. **会话必须是物理 console** 1. **会话必须是物理 console**
RDP 新建的是独立会话, 和物理桌面隔离。在 RDP 会话里调 `moveR` 光标纹丝不动 RDP 新建的是独立会话, 和物理桌面隔离。在 RDP 会话里调 `moveR` 光标纹丝不动
(实测 22 次采样全程 (1043,758) 不动), 切到 console 会话后立刻正常。 (实测 22 次采样全程 (1043,758) 不动), 切到 console 会话后立刻正常。
判断方法: `GetForegroundWindow()` 返回 0 就说明当前会话没有可接收输入的前台窗口。 比较当前进程会话与 WTSGetActiveConsoleSessionId; 前台窗口句柄不能判定会话类型。
2. **必须关掉指针加速** 2. **必须关掉指针加速**
加速开着时 counts→像素是非线性的(实测 +100/+200/+400 对应 -45/-3/-3 像素), 加速开着时 counts→像素是非线性的(实测 +100/+200/+400 对应 -45/-3/-3 像素),
关掉后严格 1 count = 1 像素(实测比值 1.0000)。没关的话本模块会报警并拒绝定位, 当时的速度设置下关闭后比值为 1.0000。没关的话本模块会报警并拒绝定位,
而不是给出一个不准的位置。 而不是给出一个不准的位置。
接口只用 `device_open / moveR / mouse_down / mouse_up / key_down / key_up / scroll`, 接口只用 `device_open / moveR / mouse_down / mouse_up / key_down / key_up / scroll`,
@@ -22,6 +22,8 @@
import ctypes import ctypes
import os import os
import time import time
from ctypes import wintypes
from functools import lru_cache
# win32 SPI 常量 # win32 SPI 常量
SPI_GETMOUSE = 0x0003 SPI_GETMOUSE = 0x0003
@@ -34,34 +36,61 @@ class Cursor(ctypes.Structure):
_fields_ = [('x', ctypes.c_long), ('y', ctypes.c_long)] _fields_ = [('x', ctypes.c_long), ('y', ctypes.c_long)]
@lru_cache(maxsize=None)
def _win_library(name):
# 使用独立绑定, 避免 argtypes 污染 AALC/pyautogui 共用的 ctypes.windll。
return ctypes.WinDLL(name, use_last_error=True)
def _api(library, name, result, args):
fn = getattr(_win_library(library), name)
fn.restype, fn.argtypes = result, args
return fn
def get_cursor(): def get_cursor():
c = Cursor() c = Cursor()
ctypes.windll.user32.GetCursorPos(ctypes.byref(c)) fn = _api('user32', 'GetCursorPos', wintypes.BOOL, [ctypes.POINTER(Cursor)])
if not fn(ctypes.byref(c)):
raise OSError('GetCursorPos 失败')
return (c.x, c.y) return (c.x, c.y)
def foreground_window(): def foreground_window():
return ctypes.windll.user32.GetForegroundWindow() return _api('user32', 'GetForegroundWindow', wintypes.HWND, [])()
def is_console_session():
current = wintypes.DWORD()
query = _api('kernel32', 'ProcessIdToSessionId', wintypes.BOOL,
[wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)])
if not query(os.getpid(), ctypes.byref(current)):
raise OSError('ProcessIdToSessionId 失败')
console = _api('kernel32', 'WTSGetActiveConsoleSessionId', wintypes.DWORD, [])()
return console != 0xFFFFFFFF and current.value == console
def has_foreground(): def has_foreground():
"""当前会话有没有可接收输入的前台窗口。 """当前是否存在前台窗口; 激活切换时也可能短暂为 0。"""
RDP 会话里常常是 0 —— 那种情况下所有注入(键盘尤其是)都不会生效。
"""
return bool(foreground_window()) return bool(foreground_window())
def acceleration_on(): def acceleration_on():
"""指针加速是否开着。开着就别指望 counts 和像素成线性。""" """指针加速是否开着。开着就别指望 counts 和像素成线性。"""
a = (ctypes.c_int * 3)() a = (ctypes.c_int * 3)()
ctypes.windll.user32.SystemParametersInfoA(SPI_GETMOUSE, 0, ctypes.byref(a), 0) fn = _api('user32', 'SystemParametersInfoA', wintypes.BOOL,
[wintypes.UINT, wintypes.UINT, ctypes.c_void_p, wintypes.UINT])
if not fn(SPI_GETMOUSE, 0, ctypes.byref(a), 0):
raise OSError('读取指针加速设置失败')
return bool(a[2]) return bool(a[2])
def mouse_speed(): def mouse_speed():
s = ctypes.c_int() s = ctypes.c_int()
ctypes.windll.user32.SystemParametersInfoA(SPI_GETMOUSESPEED, 0, ctypes.byref(s), 0) fn = _api('user32', 'SystemParametersInfoA', wintypes.BOOL,
[wintypes.UINT, wintypes.UINT, ctypes.c_void_p, wintypes.UINT])
if not fn(SPI_GETMOUSESPEED, 0, ctypes.byref(s), 0):
raise OSError('读取指针速度失败')
return s.value return s.value
@@ -88,8 +117,10 @@ class DriverInput:
return False return False
try: try:
self._dll = ctypes.CDLL(self.dll_path) self._dll = ctypes.CDLL(self.dll_path)
for name in ('device_open', 'moveR', 'mouse_down', 'mouse_up'):
getattr(self._dll, name)
ok = self._dll.device_open() == 1 ok = self._dll.device_open() == 1
except OSError as e: except (OSError, AttributeError) as e:
self.log('[driver] 加载 DLL 失败: %s' % e) self.log('[driver] 加载 DLL 失败: %s' % e)
self._dll = None self._dll = None
return False return False
@@ -106,13 +137,16 @@ class DriverInput:
"""返回 (能否用, 原因)。用之前调一次, 把不能用的原因说清楚。""" """返回 (能否用, 原因)。用之前调一次, 把不能用的原因说清楚。"""
if not self.open(): if not self.open():
return False, '罗技驱动不可用(device_open 失败)' return False, '罗技驱动不可用(device_open 失败)'
if not has_foreground(): try:
return False, ('当前会话没有前台窗口(GetForegroundWindow=0) —— ' if not is_console_session():
'多半是 RDP 独立会话, 驱动级输入在这类会话里不生效, ' return False, '当前进程不在物理 console 会话, 不启用驱动输入'
'需要物理 console 会话') if not has_foreground():
if acceleration_on(): return False, '当前会话没有前台窗口(GetForegroundWindow=0), 暂不启用驱动输入'
return False, ('指针加速开着, counts 与像素不成比例, 定位会不准 —— ' if acceleration_on():
'关掉"提高指针精确度"后重试') return False, ('指针加速开着, counts 与像素不成比例, 定位会不准 —— '
'关掉"提高指针精确度"后重试')
except (OSError, AttributeError) as e:
return False, '无法确认输入环境: %s' % e
return True, 'ok' return True, 'ok'
# ---------------------------------------------------------------- 移动 # ---------------------------------------------------------------- 移动
@@ -160,9 +194,11 @@ class DriverInput:
% (sx, sy, cx, cy)) % (sx, sy, cx, cy))
return False return False
for _ in range(int(times)): for _ in range(int(times)):
self._dll.mouse_down(1) try:
time.sleep(0.03) self._dll.mouse_down(1)
self._dll.mouse_up(1) time.sleep(0.03)
finally:
self._dll.mouse_up(1)
return True return True
def key_down(self, key): def key_down(self, key):
@@ -180,7 +216,11 @@ class DriverInput:
def client_to_screen(hwnd, x, y): def client_to_screen(hwnd, x, y):
"""游戏客户区坐标 -> 屏幕坐标。""" """游戏客户区坐标 -> 屏幕坐标。"""
from ctypes import wintypes if not hwnd:
raise OSError('没有游戏窗口句柄')
pt = wintypes.POINT(int(x), int(y)) pt = wintypes.POINT(int(x), int(y))
ctypes.windll.user32.ClientToScreen(hwnd, ctypes.byref(pt)) fn = _api('user32', 'ClientToScreen', wintypes.BOOL,
[wintypes.HWND, ctypes.POINTER(wintypes.POINT)])
if not fn(hwnd, ctypes.byref(pt)):
raise OSError('ClientToScreen 失败, 未发送输入')
return (pt.x, pt.y) return (pt.x, pt.y)