合并 main 的合并前复核修复到镜牢选队分支
分支从8a9495b分出,缺 main 上a74fda0的急停与驱动复核修复。 两边都保留,不取单边: - 急停:沿用分支的 GetAsyncKeyState 轮询与 End 键(pynput 钩子在该 机器收不到键,F12 是 Steam 截图键);移植 main 的 on_stop 释放驱动 按住的鼠标键、250 毫秒强退兜底、listening() 管理监听生命周期。 按键状态改为独立绑定(SHORT 返回值),接口不可用时启动即报告。 - 驱动输入:同时保留句柄级前台校验、press_ms/settle 与 main 的 停止标志、输入锁和按键记录;点击经 down()/up() 以便急停释放。 前台校验改用本模块的类型化 _api 绑定。 - 代理每次点击按当前句柄重新 bind_window:代理复用时不再拿安装时 的旧句柄拒绝游戏重启后的全部点击。 - aalc_task:listening() 包住整个任务,内部安装两个选队补丁。 - 分支新增测试改为替换模块接口,不再直接 patch ctypes.windll (Linux 上 9 项报错);README 急停键改为 End。 验证:Linux / Python 3.12 全套 474 项通过;git diff --check 通过。 Windows 热键、驱动和选队仍需实机回归。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+42
-16
@@ -22,6 +22,7 @@
|
|||||||
import ctypes
|
import ctypes
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import threading
|
||||||
from ctypes import wintypes
|
from ctypes import wintypes
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
@@ -60,6 +61,14 @@ def foreground_window():
|
|||||||
return _api('user32', 'GetForegroundWindow', wintypes.HWND, [])()
|
return _api('user32', 'GetForegroundWindow', wintypes.HWND, [])()
|
||||||
|
|
||||||
|
|
||||||
|
def window_title(hwnd, limit=64):
|
||||||
|
"""窗口标题, 只用于报错说明; 取不到返回空串。"""
|
||||||
|
buf = ctypes.create_unicode_buffer(limit)
|
||||||
|
_api('user32', 'GetWindowTextW', ctypes.c_int,
|
||||||
|
[wintypes.HWND, wintypes.LPWSTR, ctypes.c_int])(hwnd, buf, limit)
|
||||||
|
return buf.value
|
||||||
|
|
||||||
|
|
||||||
def is_console_session():
|
def is_console_session():
|
||||||
current = wintypes.DWORD()
|
current = wintypes.DWORD()
|
||||||
query = _api('kernel32', 'ProcessIdToSessionId', wintypes.BOOL,
|
query = _api('kernel32', 'ProcessIdToSessionId', wintypes.BOOL,
|
||||||
@@ -106,6 +115,9 @@ class DriverInput:
|
|||||||
self.log = log
|
self.log = log
|
||||||
self._dll = None
|
self._dll = None
|
||||||
self._open = False
|
self._open = False
|
||||||
|
self._stopped = False
|
||||||
|
self._input_lock = threading.RLock()
|
||||||
|
self._pressed = set()
|
||||||
# 目标窗口句柄。有它才能做句柄级前台校验 —— 几何校验在窗口重叠时没用:
|
# 目标窗口句柄。有它才能做句柄级前台校验 —— 几何校验在窗口重叠时没用:
|
||||||
# 实测把浏览器切到前台, 点游戏里同一个坐标, 只比矩形的话校验照样通过。
|
# 实测把浏览器切到前台, 点游戏里同一个坐标, 只比矩形的话校验照样通过。
|
||||||
self.hwnd = 0
|
self.hwnd = 0
|
||||||
@@ -139,7 +151,14 @@ class DriverInput:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def ready(self):
|
def ready(self):
|
||||||
return self._open and self._dll is not None
|
return self._open and self._dll is not None and not self._stopped
|
||||||
|
|
||||||
|
def emergency_stop(self):
|
||||||
|
"""拒绝后续鼠标输入并释放本实例按住的按钮。急停线程另有退出超时。"""
|
||||||
|
self._stopped = True
|
||||||
|
with self._input_lock:
|
||||||
|
for button in tuple(self._pressed):
|
||||||
|
self.up(button)
|
||||||
|
|
||||||
def preflight(self):
|
def preflight(self):
|
||||||
"""返回 (能否用, 原因)。用之前调一次, 把不能用的原因说清楚。"""
|
"""返回 (能否用, 原因)。用之前调一次, 把不能用的原因说清楚。"""
|
||||||
@@ -160,6 +179,7 @@ class DriverInput:
|
|||||||
# ---------------------------------------------------------------- 移动
|
# ---------------------------------------------------------------- 移动
|
||||||
|
|
||||||
def move_relative(self, dx, dy):
|
def move_relative(self, dx, dy):
|
||||||
|
with self._input_lock:
|
||||||
if not self.ready:
|
if not self.ready:
|
||||||
return False
|
return False
|
||||||
if dx == 0 and dy == 0:
|
if dx == 0 and dy == 0:
|
||||||
@@ -185,12 +205,19 @@ class DriverInput:
|
|||||||
# ---------------------------------------------------------------- 按键
|
# ---------------------------------------------------------------- 按键
|
||||||
|
|
||||||
def down(self, button=1):
|
def down(self, button=1):
|
||||||
if self.ready:
|
with self._input_lock:
|
||||||
self._dll.mouse_down(int(button))
|
if not self.ready:
|
||||||
|
return False
|
||||||
|
button = int(button)
|
||||||
|
self._pressed.add(button)
|
||||||
|
self._dll.mouse_down(button)
|
||||||
|
return True
|
||||||
|
|
||||||
def up(self, button=1):
|
def up(self, button=1):
|
||||||
if self.ready:
|
with self._input_lock:
|
||||||
|
if self._open and self._dll is not None:
|
||||||
self._dll.mouse_up(int(button))
|
self._dll.mouse_up(int(button))
|
||||||
|
self._pressed.discard(int(button))
|
||||||
|
|
||||||
def click(self, sx, sy, times=1, tol=1, require_foreground=True,
|
def click(self, sx, sy, times=1, tol=1, require_foreground=True,
|
||||||
press_ms=60, settle=0.05):
|
press_ms=60, settle=0.05):
|
||||||
@@ -222,11 +249,12 @@ class DriverInput:
|
|||||||
time.sleep(settle)
|
time.sleep(settle)
|
||||||
for _ in range(int(times)):
|
for _ in range(int(times)):
|
||||||
try:
|
try:
|
||||||
self._dll.mouse_down(1)
|
if not self.down(1):
|
||||||
|
return False
|
||||||
time.sleep(press_ms / 1000.0)
|
time.sleep(press_ms / 1000.0)
|
||||||
finally:
|
finally:
|
||||||
self._dll.mouse_up(1)
|
self.up(1)
|
||||||
return True
|
return not self._stopped
|
||||||
|
|
||||||
def key_down(self, key):
|
def key_down(self, key):
|
||||||
if self.ready:
|
if self.ready:
|
||||||
@@ -254,22 +282,20 @@ def click_point_is_on_foreground(sx, sy, hwnd=None):
|
|||||||
所以: 给了 hwnd 就要求前台窗口**就是它**(或是它的子窗口); 没给 hwnd 时
|
所以: 给了 hwnd 就要求前台窗口**就是它**(或是它的子窗口); 没给 hwnd 时
|
||||||
退化成"有前台窗口且点在它矩形内", 可用性有限, 仅作兜底。
|
退化成"有前台窗口且点在它矩形内", 可用性有限, 仅作兜底。
|
||||||
"""
|
"""
|
||||||
user32 = ctypes.windll.user32
|
fg = foreground_window()
|
||||||
fg = user32.GetForegroundWindow()
|
|
||||||
if not fg:
|
if not fg:
|
||||||
return False, '当前没有前台窗口(GetForegroundWindow=0)'
|
return False, '当前没有前台窗口(GetForegroundWindow=0)'
|
||||||
if hwnd:
|
if hwnd:
|
||||||
if fg == hwnd or user32.IsChild(hwnd, fg):
|
if fg == hwnd or _api('user32', 'IsChild', wintypes.BOOL,
|
||||||
|
[wintypes.HWND, wintypes.HWND])(hwnd, fg):
|
||||||
return True, 'ok'
|
return True, 'ok'
|
||||||
buf = ctypes.create_unicode_buffer(64)
|
|
||||||
user32.GetWindowTextW(fg, buf, 64)
|
|
||||||
other = ctypes.create_unicode_buffer(64)
|
|
||||||
user32.GetWindowTextW(hwnd, other, 64)
|
|
||||||
return False, ('前台不是目标窗口(前台=%r, 目标=%r) —— 这次点击会打到别的窗口'
|
return False, ('前台不是目标窗口(前台=%r, 目标=%r) —— 这次点击会打到别的窗口'
|
||||||
% (buf.value, other.value))
|
% (window_title(fg), window_title(hwnd)))
|
||||||
pt = wintypes.POINT(int(sx), int(sy))
|
pt = wintypes.POINT(int(sx), int(sy))
|
||||||
rect = wintypes.RECT()
|
rect = wintypes.RECT()
|
||||||
if not user32.GetWindowRect(fg, ctypes.byref(rect)):
|
get_rect = _api('user32', 'GetWindowRect', wintypes.BOOL,
|
||||||
|
[wintypes.HWND, ctypes.POINTER(wintypes.RECT)])
|
||||||
|
if not get_rect(fg, ctypes.byref(rect)):
|
||||||
return False, '取不到前台窗口的区域'
|
return False, '取不到前台窗口的区域'
|
||||||
if not (rect.left <= pt.x < rect.right and rect.top <= pt.y < rect.bottom):
|
if not (rect.left <= pt.x < rect.right and rect.top <= pt.y < rect.bottom):
|
||||||
return False, ('点击点(%d,%d)不在前台窗口内(区域 %d,%d-%d,%d)'
|
return False, ('点击点(%d,%d)不在前台窗口内(区域 %d,%d-%d,%d)'
|
||||||
|
|||||||
Reference in New Issue
Block a user