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
/
lc_mcp
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- coding: utf-8 -*- """全局急停热键。 为什么需要: 驱动级点击是闭环定位的 —— 手一动,下一次点击前会读到新位置、 算出偏差、再把你推回目标点。所以任务运行期间鼠标和人是"拔河"状态,用户没法 用鼠标去点停止按钮。键盘不受影响,所以用键盘热键做急停。 ⛔ AALC 自带的 pause_hotkey 帮不上忙: 那个配置项只在 GUI(app/farming_interface.py) 里注册, lc_mcp 走的是命令行子进程, 那条路径上没有热键监听。 默认 F12, 可用环境变量 LCP_KILL_KEY 改(取 pynput 的键名, 如 f11 / pause / esc)。 按下后终止任务子进程, 游戏保留。硬退出不会执行任务的 finally; 临时配置由 TaskRunner 的恢复文件机制处理。退出前给驱动释放鼠标的短暂机会, 清理卡住时仍强制退出。 """ import os import threading from contextlib import contextmanager DEFAULT_KEY = 'f12' EXIT_CODE = 130 # 与 SIGINT 一致, 便于上层识别"被用户中止" STOP_TIMEOUT = 0.25 def _key_spec(name): """'f12' -> '<f12>',pynput 的 GlobalHotKeys 要求尖括号形式。""" name = (name or '').strip().lower() or DEFAULT_KEY if name.startswith('<') and name.endswith('>'): return name return '<%s>' % name def install(log=print, key=None, on_stop=None): """启动急停监听线程。返回监听器对象(可用于测试), 失败返回 None。 任何异常都不向外抛 —— 急停装不上不该让任务起不来, 但必须把原因说清楚。 """ key = key or os.environ.get('LCP_KILL_KEY') or DEFAULT_KEY spec = _key_spec(key) try: from pynput import keyboard except Exception as e: # pynput 缺失/无显示环境 log('[lc_mcp] 急停热键不可用(pynput 加载失败: %s: %s), 请用 Ctrl+Alt+Del 强制停止' % (type(e).__name__, e)) return None state = {'fired': False} def on_activate(): if state['fired']: return state['fired'] = True # Windows 在系统输入线程上调用此函数, 不在这里写日志或等待清理。 # 即使日志锁/驱动清理卡住, 也必须按时退出。 timer = threading.Timer(STOP_TIMEOUT, os._exit, args=(EXIT_CODE,)) timer.daemon = True def shutdown(): try: if on_stop is not None: on_stop() finally: try: os._exit(EXIT_CODE) finally: timer.cancel() # 仅供 _exit 被测试替换的情况 try: timer.start() worker = threading.Thread(target=shutdown, name='lc-mcp-emergency-stop', daemon=True) state['worker'] = worker worker.start() except Exception: timer.cancel() os._exit(EXIT_CODE) try: listener = keyboard.GlobalHotKeys({spec: on_activate}) listener.daemon = True listener.start() except Exception as e: log('[lc_mcp] 急停热键注册失败(%s: %s), 请用 Ctrl+Alt+Del 强制停止' % (type(e).__name__, e)) return None log('[lc_mcp] 急停监听已启动: 按 %s 终止任务, 游戏窗口保留; 可用 run_task 重新开始' % key.upper()) listener._lc_state = state return listener def stop(listener): """停止监听(任务正常结束时调用, 免得线程残留)。""" if listener is None: return try: listener.stop() except Exception: pass @contextmanager def listening(log=print, on_stop=None): listener = install(log=log, on_stop=on_stop) try: yield listener finally: stop(listener)
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
codex/local-wip-20261022
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel