43 lines
1.0 KiB
Python
43 lines
1.0 KiB
Python
"""Test message-based input against Notepad (WM_CHAR to edit control)."""
|
|
import ctypes, sys, time
|
|
from ctypes import wintypes
|
|
|
|
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
|
WM_CHAR = 0x0102
|
|
WM_KEYDOWN = 0x0100
|
|
WM_KEYUP = 0x0101
|
|
|
|
|
|
def find_child_edit(hwnd):
|
|
found = []
|
|
|
|
@ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
|
|
def cb(h, lparam):
|
|
cls = ctypes.create_unicode_buffer(64)
|
|
user32.GetClassNameW(h, cls, 64)
|
|
if cls.value == 'Edit':
|
|
found.append(h)
|
|
return False
|
|
return True
|
|
|
|
user32.EnumChildWindows(hwnd, cb, 0)
|
|
return found[0] if found else None
|
|
|
|
|
|
def post_text(hwnd, text):
|
|
ok = 0
|
|
for ch in text:
|
|
if user32.PostMessageW(hwnd, WM_CHAR, ord(ch), 0x00000001):
|
|
ok += 1
|
|
time.sleep(0.005)
|
|
return ok
|
|
|
|
|
|
if __name__ == '__main__':
|
|
hwnd = int(sys.argv[1])
|
|
edit = find_child_edit(hwnd)
|
|
print('notepad:', hwnd, 'edit:', edit)
|
|
if edit:
|
|
print('posted chars:', post_text(edit, 'MSGOK'))
|
|
time.sleep(0.5)
|