Skip to content

Instantly share code, notes, and snippets.

@Inndy
Created June 13, 2023 10:40
Show Gist options
  • Save Inndy/0edb13315138371468c2c3c07916bfc4 to your computer and use it in GitHub Desktop.
Save Inndy/0edb13315138371468c2c3c07916bfc4 to your computer and use it in GitHub Desktop.
import ctypes
import ctypes.wintypes as wintypes
import time
import win32con
user32 = ctypes.CDLL('user32')
user32.MapVirtualKeyW.restype = wintypes.UINT
user32.MapVirtualKeyW.argtypes = (wintypes.UINT, wintypes.UINT)
user32.PostMessageW.restype = wintypes.BOOL
user32.PostMessageW.argtypes = (wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM)
user32.MapVirtualKeyA.restype = ctypes.c_uint32
user32.MapVirtualKeyA.argtypes = (ctypes.c_uint32, ctypes.c_uint32)
user32.FindWindowExW.restype = wintypes.HWND
user32.FindWindowExW.argtypes = (wintypes.HWND, wintypes.HWND, wintypes.LPWSTR, wintypes.LPWSTR)
user32.GetWindowTextW.restype = ctypes.c_int
user32.GetWindowTextW.argtypes = (wintypes.HWND, wintypes.LPWSTR, ctypes.c_int)
user32.GetClassNameW.restype = ctypes.c_int
user32.GetClassNameW.argtypes = (wintypes.HWND, wintypes.LPWSTR, ctypes.c_int)
user32.GetParent.restype = wintypes.HWND
user32.GetParent.argtypes = (wintypes.HWND,)
def enum_hwnds(parent=0):
curr = 0
while True:
val = user32.FindWindowExW(parent, curr, None, None)
if val:
yield val
curr = val
else:
break
def get_window_text(hwnd):
buf = (ctypes.c_wchar * 1024)()
n = user32.GetWindowTextW(hwnd, buf, 1024)
return buf[:n]
def get_window_class(hwnd):
buf = (ctypes.c_wchar * 1024)()
n = user32.GetClassNameW(hwnd, buf, 1024)
return buf[:n]
def press_key(hwnd, key):
user32.PostMessageW(hwnd, win32con.WM_KEYDOWN, key, 0x0000001 | (user32.MapVirtualKeyW(key, 0) & 0xFF) << 16)
user32.PostMessageW(hwnd, win32con.WM_KEYUP, key, 0xC000001 | (user32.MapVirtualKeyW(key, 0) & 0xFF) << 16)
for hwnd in enum_hwnds():
break
title = get_window_text(hwnd)
className = get_window_class(hwnd)
parent = user32.GetParent(hwnd) or 0
if parent or \
not title or \
className == 'Windows.Internal.Shell.TabProxyWindow' or \
((title, className) == ('Default IME', 'IME')):
continue
print('%.8x [%.8x] - %r / %r' % (hwnd, parent, title, className))
hwnd = user32.FindWindowExW(0, 0, 'vwr::CDesktopWin', None)
assert hwnd
print(hwnd)
for i in b'AAA':
press_key(hwnd, i)
press_key(hwnd, win32con.VK_RETURN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment