import ctypes
import time

VK_F15 = 0x7E
KEYEVENTF_KEYUP = 0x2


SM_REMOTESESSION = 0x1000


def _is_remote_session() -> bool:
    return ctypes.windll.user32.GetSystemMetrics(SM_REMOTESESSION) != 0


def command_keep_awake() -> None:
    """每 4 分钟模拟一次 F15 按键，阻止系统休眠（仅远程桌面会话生效）"""
    print("防休眠已启动，每 4 分钟检测一次（Ctrl+C 退出）")
    try:
        while True:
            if _is_remote_session():
                ctypes.windll.user32.keybd_event(VK_F15, 0, 0, 0)
                ctypes.windll.user32.keybd_event(VK_F15, 0, KEYEVENTF_KEYUP, 0)
            time.sleep(240)
    except KeyboardInterrupt:
        print("\n已停止。")
