版本比较
Zhl 锁屏软件
正文
增加 82 行删除 21 行
14 行未改动15161718191920212223242325262728854 行未改动全部展开8838848858868868878888898908915 行未改动8978988999009019029039049057 行未改动91391491591691791891992092192292362 行未改动98698798898998599099199299399499599699799899910001001100210031004100510061007993994995996997998999100010011002100310081009101010111012101310141015101610171018101910201021102210231024102510261006102710281029103010311032103310081009101010111012103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081533 行未改动全部展开161516161617161816191620162116221623299 行未改动全部展开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),如果觉得好用记得点赞收藏。
有 Bug 记得私信,很多 Bug 可以解决,[这里](https://www.luogu.com.cn/article/7opdjt2s) 会放常见问题。
等之后更新了可以考虑做安装包。
---
### Tips- 这是开源项目,代码如果不符合你的喜好,可以自己改(显然可以用 AI)。854 行未改动全部展开::::
::::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 行未改动全部展开