This website requires JavaScript.
0
Issues
Pull Requests
Milestones
Explore
0
Create…
New Repository
New Migration
New Organization
public
Signed in as
public
Profile
Starred
Subscriptions
Settings
Help
Sign Out
yws
/
lc-mcp
Watch
1
Star
0
Fork
0
Code
Issues
Pull Requests
Actions
Packages
Projects
Releases
Wiki
Activity
Files
lc-mcp
/
tests
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- coding: utf-8 -*- """按键补丁(ISSUES I-05): 前台按键带扫描码和扩展键标志, 各模式按住再松开。 2026-11-14 镜牢键盘寻路 24/24 没进去。AALC 前台 key_press 是 pyautogui.press: 扫描码 0、方向键没有 扩展键标志、down/up 紧挨着; 上游后台模式的注释写明 Unity 6+ 会校验扫描码和扩展键标志。 """ import ast import sys import types import unittest from pathlib import Path from types import SimpleNamespace from unittest import mock REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO)) from lc_mcp import aalc_patches as ap # noqa: E402 INPUT = REPO / 'vendor' / 'aalc' / 'module' / 'automation' / 'input_handlers' / 'input.py' VK_UP, VK_P, VK_RETURN = 0x26, 0x50, 0x0D SCANS = {VK_UP: 0x48, VK_P: 0x19, VK_RETURN: 0x1C} pyautogui = SimpleNamespace(press=mock.Mock()) # 假 Input.key_press 的源码里要有 pyautogui.press class User32: def __init__(self, events): self.events = events def MapVirtualKeyW(self, vk, kind): assert kind == 0 return SCANS.get(vk, 0) def keybd_event(self, vk, scan, flags, extra): self.events.append(('keybd', vk, scan, flags)) class Handle: def __init__(self, active=True, activates=True): self.active = active self.activates = activates self.foreground_calls = 0 @property def isActive(self): return self.active def setForeground(self): self.foreground_calls += 1 self.active = self.activates def fake_input_module(events): class Input: def key_press(self, key): return pyautogui.press(key) class BackgroundInput: def set_active(self): events.append(('active',)) def key_down(self, key): events.append(('down', key)) def key_up(self, key): events.append(('up', key)) def key_press(self, key): self.set_active() self.key_down(key) self.key_up(key) class WindowMoveInput(BackgroundInput): def key_press(self, key): self.set_active() self.key_down(key) self.key_up(key) module = types.ModuleType('module.automation.input_handlers.input') module.key_list = {'up': VK_UP, 'p': VK_P, 'enter': VK_RETURN} module.EXTENDED_KEY_VKS = frozenset({VK_UP}) module.Input, module.BackgroundInput, module.WindowMoveInput = Input, BackgroundInput, WindowMoveInput return module class KeyHoldTest(unittest.TestCase): def setUp(self): self.events = [] self.lines = [] self.input_mod = fake_input_module(self.events) self.handle = Handle() pyautogui.press.reset_mock() sleep = mock.patch.object(ap.time, 'sleep', side_effect=lambda s: self.events.append(('sleep', s))) sleep.start() self.addCleanup(sleep.stop) def install(self, env=None): handlers = types.ModuleType('module.automation.input_handlers') handlers.input = self.input_mod modules = {'module': types.ModuleType('module'), 'module.automation': types.ModuleType('module.automation'), 'module.automation.input_handlers': handlers, 'module.automation.input_handlers.input': self.input_mod, 'module.game_and_screen': SimpleNamespace(screen=SimpleNamespace(handle=self.handle))} with mock.patch.dict(sys.modules, modules): return ap.install_key_hold(log=self.lines.append, env=env or {}, user32=User32(self.events)) def test_arrow_key_has_scan_code_extended_flag_and_is_held(self): self.assertEqual(sorted(self.install()), ['BackgroundInput', 'Input', 'WindowMoveInput']) self.input_mod.Input().key_press('UP') self.assertEqual(self.events, [('keybd', VK_UP, 0x48, 0x1), ('sleep', ap.KEY_HOLD), ('keybd', VK_UP, 0x48, 0x3)]) pyautogui.press.assert_not_called() def test_letter_key_has_scan_code_without_extended_flag(self): self.install() self.input_mod.Input().key_press('p') self.assertEqual(self.events, [('keybd', VK_P, 0x19, 0x0), ('sleep', ap.KEY_HOLD), ('keybd', VK_P, 0x19, 0x2)]) def test_inactive_game_window_is_activated_first(self): self.handle.active = False self.install() self.input_mod.Input().key_press('enter') self.assertEqual(self.handle.foreground_calls, 1) self.assertEqual([e for e in self.events if e[0] == 'keybd'], [('keybd', VK_RETURN, 0x1C, 0x0), ('keybd', VK_RETURN, 0x1C, 0x2)]) def test_key_is_not_sent_to_another_window(self): self.handle = Handle(active=False, activates=False) self.install() self.input_mod.Input().key_press('enter') self.assertEqual([e for e in self.events if e[0] == 'keybd'], []) pyautogui.press.assert_not_called() self.assertIn('按键 enter 没发: 游戏窗口不在前台', '\n'.join(self.lines)) def test_unknown_key_goes_to_upstream(self): self.install() self.input_mod.Input().key_press('f13') pyautogui.press.assert_called_once_with('f13') self.assertEqual(self.events, []) def test_background_modes_hold_between_down_and_up(self): self.install() for cls in (self.input_mod.BackgroundInput, self.input_mod.WindowMoveInput): self.events.clear() cls().key_press('up') self.assertEqual(self.events, [('active',), ('down', 'up'), ('sleep', ap.KEY_HOLD), ('up', 'up')]) def test_background_key_is_released_even_if_interrupted(self): self.install() with mock.patch.object(ap.time, 'sleep', side_effect=InterruptedError): with self.assertRaises(InterruptedError): self.input_mod.BackgroundInput().key_press('p') self.assertEqual(self.events[-1], ('up', 'p')) def test_foreground_key_is_released_even_if_interrupted(self): self.install() with mock.patch.object(ap.time, 'sleep', side_effect=InterruptedError): with self.assertRaises(InterruptedError): self.input_mod.Input().key_press('up') self.assertEqual(self.events[-1], ('keybd', VK_UP, 0x48, 0x3)) def test_env_zero_keeps_upstream(self): original = self.input_mod.Input.key_press self.assertIsNone(self.install(env={ap.KEY_HOLD_ENV: '0'})) self.assertIs(self.input_mod.Input.key_press, original) def test_changed_upstream_foreground_is_reported_not_patched(self): def key_press(self, key): return None self.input_mod.Input.key_press = key_press self.assertEqual(sorted(self.install()), ['BackgroundInput', 'WindowMoveInput']) self.assertIs(self.input_mod.Input.key_press, key_press) self.assertIn('[ERROR] [lc_mcp] 按键补丁: 上游前台 Input.key_press 不再用 pyautogui.press', '\n'.join(self.lines)) @unittest.skipUnless(INPUT.is_file(), 'vendor/aalc 子模块未检出') class UpstreamInputTest(unittest.TestCase): """补丁依赖的上游写法: 前台 pyautogui.press, 后台 set_active + key_down + key_up, 方向键在扩展键表里。""" def setUp(self): self.source = INPUT.read_text(encoding='utf-8') self.tree = ast.parse(self.source) def method_source(self, cls_name, name): cls = next(n for n in self.tree.body if isinstance(n, ast.ClassDef) and n.name == cls_name) node = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == name) return ast.get_source_segment(self.source, node) def test_foreground_uses_pyautogui_press(self): self.assertIn('pyautogui.press(key)', self.method_source('Input', 'key_press')) def test_background_modes_send_down_up_back_to_back(self): for cls in ('BackgroundInput', 'WindowMoveInput'): body = self.method_source(cls, 'key_press') self.assertIn('self.set_active()', body) self.assertLess(body.index('self.key_down(key)'), body.index('self.key_up(key)')) self.assertNotIn('sleep', body) def test_arrow_keys_are_extended(self): for vk in ('VK_UP', 'VK_DOWN', 'VK_LEFT', 'VK_RIGHT'): self.assertIn('win32con.%s' % vk, self.source.split('EXTENDED_KEY_VKS', 1)[1].split(')', 1)[0]) if __name__ == '__main__': unittest.main()
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
codex/feedback-unattended-setup-20261004
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel