首页 > 解决方案 > 在 Python 中通过 windows api 将键盘输入发送到 Notepad++

问题描述

我知道并非所有 Windows 都处理键盘输入这一事实。但是,我的 Notepad++ 树的一个窗口应该对编辑器产生影响,即写下我要发送的“A”。我已经尝试了所有子窗口,但没有任何效果:

def mycallback(hwnd, data):
    data[hwnd] = win32gui.GetClassName(hwnd)
    win32gui.EnumChildWindows(hwnd, mycallback, data)
    return True

mywindows = {}
win32gui.EnumChildWindows(197788, mycallback, mywindows) # 197788 is the notepad++ handle whose parent is 0

# at this point, mywindows contains: {'#32770': 66908, 'Button': 66922, 'Static': 66924, 'SysTabControl32': 132272, 'Scintilla': 132276, 'splitterContainer': 263444, 'wespliter': 328982, 'msctls_statusbar32': 394476, 'dockingManager': 394474, 'wedockspliter': 132328, 'nsdockspliter': 132336, 'ReBarWindow32': 66906, 'ToolbarWindow32': 66902, 'Edit': 66914}

for v in mywindows.values():
 win32api.SendMessage(v, win32con.WM_CHAR, 0x41, 0) # trying to send an 'A' to notepad++ without knowing to which window I need to send it to. I had already noticed before that sending the 'A' to the "Edit" window doesn't work (the EXACT same win32api.SendMessage command works for the "Edit" window of the Microsoft-Windows-notepad-editor by the way)
# the return values of this loop are: 0,0,0,0,0,0,0,0,0,0,0,0,0,1

只有“编辑”窗口返回 1。1 表示错误,所以我假设“编辑”是正确的目标窗口,但为什么它返回错误?

顺便说一句:Microsoft-Windows-notepad-editor 的“编辑”窗口也返回 1,但它会将“A”打印到编辑器。

标签: pythonwinapipywin32win32gui

解决方案


推荐阅读