import ctypes
import time

import win32con
import win32service

h_desk = win32service.OpenDesktop("Default", 0, False, win32con.GENERIC_ALL)
ctypes.windll.user32.SetThreadDesktop(int(h_desk))

VK_F9 = 0x78
# Clear state
ctypes.windll.user32.GetAsyncKeyState(VK_F9)

# Simulate key press
ctypes.windll.user32.keybd_event(VK_F9, 0, 0, 0)
time.sleep(0.02)
ctypes.windll.user32.keybd_event(VK_F9, 0, 2, 0)
time.sleep(0.05)

state = ctypes.windll.user32.GetAsyncKeyState(VK_F9)
print(f"GetAsyncKeyState(VK_F9) state: {hex(state)} (Hit detected: {bool(state & 1 or state & 0x8000)})")
