""" Windows 화면 잠금 감지. `OpenInputDesktop`/`GetUserObjectInformation`을 사용해 현재 활성 데스크톱이 "Winlogon"(잠금/사용자 전환 화면)인지 확인. 5초 간격 polling으로 충분 — 노이즈 적고 가벼운 방식. """ from __future__ import annotations try: import ctypes from ctypes import wintypes _WIN_AVAILABLE = True except ImportError: _WIN_AVAILABLE = False def is_screen_locked() -> bool: """현재 화면이 잠금 상태(또는 사용자 전환 중)이면 True. Windows 외 플랫폼이거나 권한 부족 시 False (안전한 기본값). """ if not _WIN_AVAILABLE: return False user32 = ctypes.windll.user32 DESKTOP_SWITCHDESKTOP = 0x0100 UOI_NAME = 2 # 현재 입력 받는 데스크탑 핸들 handle = user32.OpenInputDesktop(0, False, DESKTOP_SWITCHDESKTOP) if not handle: return False try: buf = ctypes.create_unicode_buffer(256) needed = wintypes.DWORD(0) ok = user32.GetUserObjectInformationW( handle, UOI_NAME, buf, ctypes.sizeof(buf), ctypes.byref(needed) ) if not ok: return False # 잠금/사용자 전환 시 "Winlogon", 보통은 "Default" return buf.value.lower() != 'default' finally: user32.CloseDesktop(handle)