版本比较
Zhl 锁屏软件
正文
增加 80 行删除 20 行
14 行未改动15161718191920212223858 行未改动全部展开8828838848858868868878888898905 行未改动8968978988999009019029039047 行未改动91291391491591691791891992092192262 行未改动9859869879889859899909919929939949959969979989991000100110021003100410051006993994995996997998999100010011002100310071008100910101011101210131014101510161017101810191020102110221023102410251006102610271028102910301031103210081009101010111012103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080533 行未改动全部展开161416151616161716181619162016211622299 行未改动全部展开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),如果觉得好用记得点赞收藏。蓝奏云下载链接,由于我没有 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优化 | 无鼠标转圈 | @Zhl2010# 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)
# ------------------------------- 快捷键管理器(使用 keyboard 库,稳定)------------------------------# ------------------------------- 快捷键管理器(修复版:自动恢复 + 异步回调)------------------------------class HotkeyManager: def __init__(self, callback): self.callback = callback self.current_hotkey = None self.listening = False self._lock = threading.Lock() self._health_thread = None self._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): if self.listening: self.stop() try: self.current_hotkey = hotkey_str self.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 = False with self._lock: if self.listening: self.stop() try: # 移除可能存在的旧钩子 keyboard.unhook_all() # 注册新热键,回调使用安全包装 keyboard.add_hotkey(hotkey_str, self._safe_callback) self.current_hotkey = hotkey_str self.listening = True logging.info(f"快捷键 {hotkey_str} 已注册") # 启动健康检查线程 self._start_health_check() except Exception as e: logging.error(f"注册快捷键失败: {e}") self.listening = False raise
def stop(self): if self.current_hotkey: with self._lock: if not self.listening: return self._stop_health.set() if self._health_thread and self._health_thread.is_alive(): self._health_thread.join(timeout=1) try: keyboard.remove_hotkey(self.current_hotkey) self.listening = False print(f"[INFO] 快捷键 {self.current_hotkey} 已移除") except: pass keyboard.unhook_all() logging.info(f"快捷键 {self.current_hotkey} 已移除") except Exception as e: logging.warning(f"移除快捷键时出错: {e}") self.listening = False self.current_hotkey = None self._last_trigger = 0
def 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: break now = time.time() # 如果从未触发过,或者距离上次触发超过 INACTIVE_TIMEOUT if self._last_trigger == 0 or (now - self._last_trigger) > INACTIVE_TIMEOUT: logging.warning("热键可能失效,尝试重新注册...") with self._lock: if not self.listening: break hotkey = self.current_hotkey if 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 行未改动全部展开