This commit is contained in:
Mikan
2026-03-09 22:05:48 +03:00
parent c635b0261b
commit 01cf0f87d0
5 changed files with 282 additions and 28 deletions

View File

@@ -18,11 +18,26 @@ def register_session_listener(callback: Callable[[str], None]) -> None:
"""Register a callback for Windows session lock/unlock events"""
def session_event_handler(hwnd, msg, wparam, lparam):
if msg == win32con.WM_WTSSESSION_CHANGE:
if wparam == win32con.WTS_SESSION_LOCK:
callback("locked")
elif wparam == win32con.WTS_SESSION_UNLOCK:
callback("unlocked")
# Check if the constant exists before using it
try:
if msg == win32con.WM_WTSSESSION_CHANGE:
if wparam == getattr(win32con, 'WTS_SESSION_LOCK', 0x1):
callback("locked")
elif wparam == getattr(win32con, 'WTS_SESSION_UNLOCK', 0x2):
callback("unlocked")
except AttributeError:
# Some versions of pywin32 might not have these constants
# Use numeric values as fallback
WM_WTSSESSION_CHANGE = 0x02B1
WTS_SESSION_LOCK = 0x1
WTS_SESSION_UNLOCK = 0x2
if msg == WM_WTSSESSION_CHANGE:
if wparam == WTS_SESSION_LOCK:
callback("locked")
elif wparam == WTS_SESSION_UNLOCK:
callback("unlocked")
return win32gui.DefWindowProc(hwnd, msg, wparam, lparam)
def run_message_loop():
@@ -39,17 +54,17 @@ def register_session_listener(callback: Callable[[str], None]) -> None:
# Corrected CreateWindow call with all required parameters
hwnd = win32gui.CreateWindow(
wndclass.lpszClassName, # lpClassName
"Session Watcher", # lpWindowName
0, # dwStyle
0, # x
0, # y
0, # nWidth
0, # nHeight
0, # hWndParent
0, # hMenu
hinst, # hInstance
None # lParam
wndclass.lpszClassName, # lpClassName
"Session Watcher", # lpWindowName
0, # dwStyle
0, # x
0, # y
0, # nWidth
0, # nHeight
0, # hWndParent
0, # hMenu
hinst, # hInstance
None # lParam
)
# Register for session notifications