34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
"""Windows utility functions: idle time, workstation lock, etc."""
|
|
|
|
import ctypes
|
|
from ctypes import wintypes
|
|
import time
|
|
from datetime import datetime
|
|
|
|
# Windows API: LASTINPUTINFO struct for idle detection
|
|
class LASTINPUTINFO(ctypes.Structure):
|
|
_fields_ = [("cbSize", wintypes.UINT), ("dwTime", wintypes.DWORD)]
|
|
|
|
user32 = ctypes.windll.user32
|
|
kernel32 = ctypes.windll.kernel32
|
|
|
|
def get_idle_time_seconds() -> float:
|
|
"""Return seconds since last user input (keyboard or mouse)."""
|
|
last_input = LASTINPUTINFO()
|
|
last_input.cbSize = ctypes.sizeof(LASTINPUTINFO)
|
|
if user32.GetLastInputInfo(ctypes.byref(last_input)):
|
|
millis = kernel32.GetTickCount() - last_input.dwTime
|
|
return millis / 1000.0
|
|
return 0.0
|
|
|
|
def lock_workstation() -> bool:
|
|
"""Lock the current workstation (same as Win+L)."""
|
|
return bool(user32.LockWorkStation())
|
|
|
|
def get_timestamp() -> str:
|
|
"""ISO-like timestamp for filenames and logs."""
|
|
return datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:-3]
|
|
|
|
def format_event(event_type: str, details: str) -> str:
|
|
"""Simple event string."""
|
|
return f"[{datetime.now().isoformat()}] {event_type}: {details}" |