import time
import ctypes

user32 = ctypes.windll.user32
hwinsta = user32.OpenWindowStationW("WinSta0", False, 0x10000000)
if hwinsta: user32.SetProcessWindowStation(hwinsta)
hdesk = user32.OpenDesktopW("Default", 0, False, 0x10000000)
if hdesk: user32.SetThreadDesktop(hdesk)

hwnd = user32.FindWindowW("POEWindowClass", None)
user32.ShowWindow(hwnd, 9)
user32.SetForegroundWindow(hwnd)
user32.BringWindowToTop(hwnd)
time.sleep(0.3)

# Click title bar to give physical input focus
user32.SetCursorPos(1280, 5)
time.sleep(0.05)
user32.mouse_event(0x0002, 0, 0, 0, 0)
time.sleep(0.05)
user32.mouse_event(0x0004, 0, 0, 0, 0)
time.sleep(0.3)

print("Sending ESC (VK 0x1B, scan 0x01)...")
user32.keybd_event(0x1B, 0x01, 0, 0)
time.sleep(0.08)
user32.keybd_event(0x1B, 0x01, 2, 0)
time.sleep(0.8)

import sys
sys.path.insert(0, r"C:\Projects\AutoPOE2")
from tools.autonomous_map_runner import ScreenCapturerGDI
c = ScreenCapturerGDI()
c.capture(r"C:\Projects\AutoPOE2\captures\test_esc_verified.png")
print("Captured test_esc_verified.png")
