正文
增加 80 行删除 20 行
14 行未改动151617181920212223858 行未改动全部展开8828838848858868878888898905 行未改动8968978988999009019029037 行未改动91191291391491591691791862 行未改动9819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021533 行未改动全部展开15551556155715581559156015611562299 行未改动全部展开14 行未改动注意:`face_detection_yunet_2023mar.onnx` 和 `face_recognition_sface_2021dec.onnx` 这两个库需要自己下,文件有点大,放不进 Github。---蓝奏云下载链接,由于我没有 VIP,就分三个放了:[第一个](https://www.ilanzou.com/s/a7ewu0vD?code=2010),[第二个](https://www.ilanzou.com/s/hlswuZy0?code=2010),[第三个](https://www.ilanzou.com/s/59TwuZNe?code=2010),如果觉得好用记得点赞收藏。858 行未改动全部展开::::::::info[main.py]```py# main.py - 快捷键修复版 | CPU优化 | 无鼠标转圈 | @Zhl2010import osimport sysimport jsonimport hashlib5 行未改动import ctypesfrom ctypes import wintypesimport timeimport gc# 使用 keyboard 库(稳定且 CPU 占用低)try:import keyboard7 行未改动except ImportError:print("请先安装 pystray: pip install pystray")sys.exit(1)# ------------------------------- 路径与目录 -------------------------------LOG_DIR = "log"PASSWORD_FILE = os.path.join(LOG_DIR, "pass")SETTINGS_FILE = os.path.join(LOG_DIR, "settings.json")62 行未改动def save(cls, settings):with open(cls.SETTINGS_FILE, 'w', encoding='utf-8') as f:json.dump(settings, f, indent=4, ensure_ascii=False)# ------------------------------- 快捷键管理器(使用 keyboard 库,稳定)------------------------------class HotkeyManager:def __init__(self, callback):self.callback = callbackself.current_hotkey = Noneself.listening = Falsedef start(self, hotkey_str):if self.listening:self.stop()try:self.current_hotkey = hotkey_strself.listening = True# 使用 keyboard 的 add_hotkey,回调直接调用keyboard.add_hotkey(hotkey_str, self.callback)print(f"[INFO] 快捷键 {hotkey_str} 已注册")except Exception as e:print(f"[ERROR] 注册快捷键失败: {e}")self.listening = Falsedef stop(self):if self.current_hotkey:try:keyboard.remove_hotkey(self.current_hotkey)self.listening = Falseprint(f"[INFO] 快捷键 {self.current_hotkey} 已移除")except:passdef change_hotkey(self, new_hotkey):self.stop()self.start(new_hotkey)# ------------------------------- 主界面 -------------------------------class MainApp(ctk.CTk):def __init__(self):super().__init__()533 行未改动全部展开if __name__ == "__main__":app = MainApp()app.mainloop()```::::::::info[recognize.py]```py299 行未改动全部展开14 行未改动151617181920212223858 行未改动全部展开8828838848858868878888898905 行未改动8968978988999009019029039047 行未改动91291391491591691791891992092192262 行未改动985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080533 行未改动全部展开161416151616161716181619162016211622299 行未改动全部展开14 行未改动注意:`face_detection_yunet_2023mar.onnx` 和 `face_recognition_sface_2021dec.onnx` 这两个库需要自己下,文件有点大,放不进 Github。---蓝奏云下载链接,由于我没有 VIP,就分三个放了:[第一个](https://www.ilanzou.com/s/49CwSfcv?code=2010),[第二个](https://www.ilanzou.com/s/hlswuZy0?code=2010),[第三个](https://www.ilanzou.com/s/59TwuZNe?code=2010),如果觉得好用记得点赞收藏。858 行未改动全部展开::::::::info[main.py]```py# main.py - 快捷键稳定版 | CPU优化 | 无鼠标转圈 | @Zhl2010import osimport sysimport jsonimport hashlib5 行未改动import ctypesfrom ctypes import wintypesimport timeimport gcimport logging# 使用 keyboard 库(稳定且 CPU 占用低)try:import keyboard7 行未改动except ImportError:print("请先安装 pystray: pip install pystray")sys.exit(1)# 配置简单日志,便于排查热键问题logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')# ------------------------------- 路径与目录 -------------------------------LOG_DIR = "log"PASSWORD_FILE = os.path.join(LOG_DIR, "pass")SETTINGS_FILE = os.path.join(LOG_DIR, "settings.json")62 行未改动def save(cls, settings):with open(cls.SETTINGS_FILE, 'w', encoding='utf-8') as f:json.dump(settings, f, indent=4, ensure_ascii=False)# ------------------------------- 快捷键管理器(修复版:自动恢复 + 异步回调)------------------------------class HotkeyManager:def __init__(self, callback):self.callback = callbackself.current_hotkey = Noneself.listening = Falseself._lock = threading.Lock()self._health_thread = Noneself._stop_health = threading.Event()self._last_trigger = 0 # 最后触发时间戳,用于健康检查def _safe_callback(self):"""在独立线程中执行回调,避免阻塞钩子线程"""self._last_trigger = time.time()# 启动新线程执行用户回调(启动锁屏等)threading.Thread(target=self.callback, daemon=True).start()def start(self, hotkey_str):with self._lock:if self.listening:self.stop()try:# 移除可能存在的旧钩子keyboard.unhook_all()# 注册新热键,回调使用安全包装keyboard.add_hotkey(hotkey_str, self._safe_callback)self.current_hotkey = hotkey_strself.listening = Truelogging.info(f"快捷键 {hotkey_str} 已注册")# 启动健康检查线程self._start_health_check()except Exception as e:logging.error(f"注册快捷键失败: {e}")self.listening = Falseraisedef stop(self):with self._lock:if not self.listening:returnself._stop_health.set()if self._health_thread and self._health_thread.is_alive():self._health_thread.join(timeout=1)try:keyboard.unhook_all()logging.info(f"快捷键 {self.current_hotkey} 已移除")except Exception as e:logging.warning(f"移除快捷键时出错: {e}")self.listening = Falseself.current_hotkey = Noneself._last_trigger = 0def change_hotkey(self, new_hotkey):self.stop()self.start(new_hotkey)def _start_health_check(self):"""启动健康检查线程,定期验证热键是否有效"""if self._health_thread and self._health_thread.is_alive():self._stop_health.set()self._health_thread.join(timeout=0.5)self._stop_health.clear()self._health_thread = threading.Thread(target=self._health_loop, daemon=True)self._health_thread.start()def _health_loop(self):"""每隔10秒检查热键是否工作,若长时间未触发则尝试重新注册"""CHECK_INTERVAL = 10 # 检查间隔(秒)INACTIVE_TIMEOUT = 60 # 超过60秒无任何触发则认为可能失效while not self._stop_health.wait(CHECK_INTERVAL):if not self.listening:breaknow = time.time()# 如果从未触发过,或者距离上次触发超过 INACTIVE_TIMEOUTif self._last_trigger == 0 or (now - self._last_trigger) > INACTIVE_TIMEOUT:logging.warning("热键可能失效,尝试重新注册...")with self._lock:if not self.listening:breakhotkey = self.current_hotkeyif hotkey:try:keyboard.unhook_all()keyboard.add_hotkey(hotkey, self._safe_callback)logging.info(f"热键 {hotkey} 已重新注册")except Exception as e:logging.error(f"重新注册失败: {e}")# ------------------------------- 主界面 -------------------------------class MainApp(ctk.CTk):def __init__(self):super().__init__()533 行未改动全部展开if __name__ == "__main__":app = MainApp()app.mainloop()```::::::::info[recognize.py]```py299 行未改动全部展开