按"应该在输入层接管"的思路做的: 不是只改 lc_mcp 自己的 click, 而是把 AALC 任务流里
所有鼠标点击都转到罗技驱动上 —— 这样镜牢/日常的点击也走物理输入。
一、lc_mcp/driver_input.py(新)
DriverInput 封装 logitech.driver.dll(device_open/moveR/mouse_down/mouse_up)。
定位用**闭环**: 发相对位移 -> 读回实际坐标 -> 不够再发, 收敛到 <=1px 才认。
不依赖任何标定常数, 也不怕偶发坐标跳变。
preflight() 有三条硬性拒绝, 每条都对应一个实测过的失败条件:
* device_open() != 1 -> 罗技驱动没就绪
* GetForegroundWindow() == 0 -> 当前不是物理交互会话(RDP 独立会话里驱动级无效;
实测那时 22 次采样光标纹丝不动, 切到 console 立刻正常)
* 指针加速开着 -> counts 与像素不成比例(实测 +100/+200/+400 对应
-45/-3/-3 像素), 关掉后严格 1:1
二、aalc_patches.py: DriverClickProxy + install_driver_click
只接管 mouse_click / mouse_down / mouse_up, 其余(拖拽/滑动/滚轮/等待)一律透传。
拖拽类不接管: 那些行为差异大, 用相对位移复现更容易出错。
⛔ 关键细节: automation.py 初始化时把方法绑成了别名
(self.mouse_click = self.input_handler.mouse_click), 所以**换 handler 不够**,
必须把别名一起重绑, 否则任务流里调的还是老方法。测试里专门守这一条。
条件不满足时**一个都不改** —— 宁可继续用原来的注入方式, 也不要半吊子接管。
三、aalc_task.py: 环境变量 LCP_DRIVER_INPUT=1 时才安装; 默认关闭。
验证(真机, 物理 console 会话):
preflight: True | ok 前台窗口存在 True 指针加速 False
move_to (400,300) / (1200,700) / (800,500) -> 误差全部 (0,0)
click(600,400) -> True
测试 15 个, 重点在"各种条件不满足时必须拒绝"。全套 399/399 通过。
187 lines
6.3 KiB
Python
187 lines
6.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""罗技驱动级输入(logitech.driver.dll)。
|
|
|
|
为什么需要它: 用 SendInput/mouse_event 注入的点击, 游戏侧仍可能识别为"模拟"。
|
|
这个 DLL 通过罗技驱动的虚拟 HID 设备发事件, 系统层面等同真鼠标。
|
|
|
|
⛔ 两个必须知道的前提(2026-10-22 实测):
|
|
|
|
1. **会话必须是物理 console**
|
|
RDP 新建的是独立会话, 和物理桌面隔离。在 RDP 会话里调 `moveR` 光标纹丝不动
|
|
(实测 22 次采样全程 (1043,758) 不动), 切到 console 会话后立刻正常。
|
|
判断方法: `GetForegroundWindow()` 返回 0 就说明当前会话没有可接收输入的前台窗口。
|
|
|
|
2. **必须关掉指针加速**
|
|
加速开着时 counts→像素是非线性的(实测 +100/+200/+400 对应 -45/-3/-3 像素),
|
|
关掉后严格 1 count = 1 像素(实测比值 1.0000)。没关的话本模块会报警并拒绝定位,
|
|
而不是给出一个不准的位置。
|
|
|
|
接口只用 `device_open / moveR / mouse_down / mouse_up / key_down / key_up / scroll`,
|
|
这是 logitech.test.py 里验证过的那几个。
|
|
"""
|
|
import ctypes
|
|
import os
|
|
import time
|
|
|
|
# win32 SPI 常量
|
|
SPI_GETMOUSE = 0x0003
|
|
SPI_GETMOUSESPEED = 0x0070
|
|
|
|
DEFAULT_DLL = r'E:\罗技键鼠驱动\logitech.driver.dll'
|
|
|
|
|
|
class Cursor(ctypes.Structure):
|
|
_fields_ = [('x', ctypes.c_long), ('y', ctypes.c_long)]
|
|
|
|
|
|
def get_cursor():
|
|
c = Cursor()
|
|
ctypes.windll.user32.GetCursorPos(ctypes.byref(c))
|
|
return (c.x, c.y)
|
|
|
|
|
|
def foreground_window():
|
|
return ctypes.windll.user32.GetForegroundWindow()
|
|
|
|
|
|
def has_foreground():
|
|
"""当前会话有没有可接收输入的前台窗口。
|
|
|
|
RDP 会话里常常是 0 —— 那种情况下所有注入(键盘尤其是)都不会生效。
|
|
"""
|
|
return bool(foreground_window())
|
|
|
|
|
|
def acceleration_on():
|
|
"""指针加速是否开着。开着就别指望 counts 和像素成线性。"""
|
|
a = (ctypes.c_int * 3)()
|
|
ctypes.windll.user32.SystemParametersInfoA(SPI_GETMOUSE, 0, ctypes.byref(a), 0)
|
|
return bool(a[2])
|
|
|
|
|
|
def mouse_speed():
|
|
s = ctypes.c_int()
|
|
ctypes.windll.user32.SystemParametersInfoA(SPI_GETMOUSESPEED, 0, ctypes.byref(s), 0)
|
|
return s.value
|
|
|
|
|
|
class DriverInput:
|
|
"""罗技驱动级鼠标/键盘。
|
|
|
|
定位采用**闭环**: 发相对位移 -> 读回实际位置 -> 不够就再发, 直到误差 <= tol。
|
|
这样不依赖任何标定常数, 也能对抗偶发的坐标跳变。
|
|
"""
|
|
|
|
def __init__(self, dll_path=None, log=print):
|
|
self.dll_path = dll_path or os.environ.get('LCP_LOGITECH_DLL') or DEFAULT_DLL
|
|
self.log = log
|
|
self._dll = None
|
|
self._open = False
|
|
|
|
# ---------------------------------------------------------------- 生命周期
|
|
|
|
def open(self):
|
|
if self._open:
|
|
return True
|
|
if not os.path.exists(self.dll_path):
|
|
self.log('[driver] 找不到 %s' % self.dll_path)
|
|
return False
|
|
try:
|
|
self._dll = ctypes.CDLL(self.dll_path)
|
|
ok = self._dll.device_open() == 1
|
|
except OSError as e:
|
|
self.log('[driver] 加载 DLL 失败: %s' % e)
|
|
self._dll = None
|
|
return False
|
|
self._open = bool(ok)
|
|
if not ok:
|
|
self.log('[driver] device_open() != 1, 罗技驱动未就绪')
|
|
return self._open
|
|
|
|
@property
|
|
def ready(self):
|
|
return self._open and self._dll is not None
|
|
|
|
def preflight(self):
|
|
"""返回 (能否用, 原因)。用之前调一次, 把不能用的原因说清楚。"""
|
|
if not self.open():
|
|
return False, '罗技驱动不可用(device_open 失败)'
|
|
if not has_foreground():
|
|
return False, ('当前会话没有前台窗口(GetForegroundWindow=0) —— '
|
|
'多半是 RDP 独立会话, 驱动级输入在这类会话里不生效, '
|
|
'需要物理 console 会话')
|
|
if acceleration_on():
|
|
return False, ('指针加速开着, counts 与像素不成比例, 定位会不准 —— '
|
|
'关掉"提高指针精确度"后重试')
|
|
return True, 'ok'
|
|
|
|
# ---------------------------------------------------------------- 移动
|
|
|
|
def move_relative(self, dx, dy):
|
|
if not self.ready:
|
|
return False
|
|
if dx == 0 and dy == 0:
|
|
return True
|
|
self._dll.moveR(int(dx), int(dy), True)
|
|
return True
|
|
|
|
def move_to(self, sx, sy, tol=1, iters=10, settle=0.03):
|
|
"""把光标精确移到屏幕坐标 (sx, sy)。返回是否到位。"""
|
|
if not self.ready:
|
|
return False
|
|
for _ in range(iters):
|
|
cx, cy = get_cursor()
|
|
dx, dy = int(sx) - cx, int(sy) - cy
|
|
if abs(dx) <= tol and abs(dy) <= tol:
|
|
return True
|
|
if self.move_relative(dx, dy) is False:
|
|
return False
|
|
time.sleep(settle)
|
|
cx, cy = get_cursor()
|
|
return abs(int(sx) - cx) <= tol and abs(int(sy) - cy) <= tol
|
|
|
|
# ---------------------------------------------------------------- 按键
|
|
|
|
def down(self, button=1):
|
|
if self.ready:
|
|
self._dll.mouse_down(int(button))
|
|
|
|
def up(self, button=1):
|
|
if self.ready:
|
|
self._dll.mouse_up(int(button))
|
|
|
|
def click(self, sx, sy, times=1, tol=1):
|
|
"""先确认到位置, 再按下抬起。没到位就不点, 免得点错地方。"""
|
|
if not self.ready:
|
|
return False
|
|
if not self.move_to(sx, sy, tol=tol):
|
|
cx, cy = get_cursor()
|
|
self.log('[driver] 定位失败: 目标(%d,%d) 实际(%d,%d), 不点击'
|
|
% (sx, sy, cx, cy))
|
|
return False
|
|
for _ in range(int(times)):
|
|
self._dll.mouse_down(1)
|
|
time.sleep(0.03)
|
|
self._dll.mouse_up(1)
|
|
return True
|
|
|
|
def key_down(self, key):
|
|
if self.ready:
|
|
self._dll.key_down(key)
|
|
|
|
def key_up(self, key):
|
|
if self.ready:
|
|
self._dll.key_up(key)
|
|
|
|
def scroll(self, amount):
|
|
if self.ready:
|
|
self._dll.scroll(int(amount))
|
|
|
|
|
|
def client_to_screen(hwnd, x, y):
|
|
"""游戏客户区坐标 -> 屏幕坐标。"""
|
|
from ctypes import wintypes
|
|
pt = wintypes.POINT(int(x), int(y))
|
|
ctypes.windll.user32.ClientToScreen(hwnd, ctypes.byref(pt))
|
|
return (pt.x, pt.y)
|