Files

33 lines
821 B
Python

"""Message-based input (PostMessage) — works without a foreground/input desktop."""
import ctypes, time
from ctypes import wintypes
user32 = ctypes.WinDLL('user32', use_last_error=True)
WM_CHAR = 0x0102
WM_KEYDOWN = 0x0100
WM_KEYUP = 0x0101
WM_LBUTTONDOWN = 0x0201
WM_LBUTTONUP = 0x0202
WM_SETCURSOR = 0x0020
def post_char(hwnd, ch):
user32.PostMessageW(hwnd, WM_CHAR, ord(ch), 0x00000001)
def post_text(hwnd, text):
for ch in text:
post_char(hwnd, ch)
time.sleep(0.01)
def post_key(hwnd, vk):
user32.PostMessageW(hwnd, WM_KEYDOWN, vk, 0x00000001)
user32.PostMessageW(hwnd, WM_KEYUP, vk, 0xC0000001)
def post_click(hwnd, x, y):
lparam = (y << 16) | (x & 0xFFFF)
user32.PostMessageW(hwnd, WM_LBUTTONDOWN, 1, lparam)
user32.PostMessageW(hwnd, WM_LBUTTONUP, 0, lparam)