使用什么SendMessage直接将密钥发送到另一个窗口? [英] what SendMessage to use to send keys directly to another window?
问题描述
我正在尝试使用SendMessage
将键盘输入发送到另一个窗口.我知道缺点,但是我必须这样做,因为我必须发送几个键,而且我不能保证窗口将具有焦点-因此这必须在窗口没有焦点时起作用.
I'm trying to use SendMessage
to send keyboard input to another window. I know the drawbacks, but I have to do it since I have to send several keys and I can't guarantee that the window will have focus - so this has to work when the window doesnt have focus.
我正在通过尝试将密钥发送到记事本窗口进行测试.我尝试了以下变体,但没有一个起作用:
I'm testing it by trying to send keys to a notepad window. I've tried the following variations, and none have worked:
def post_keys1(hwnd):
win32api.SendMessage(
hwnd, win32con.WM_KEYDOWN, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.SendMessage(
hwnd, win32con.WM_CHAR, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.SendMessage(
hwnd, win32con.WM_KEYUP, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0xC0 << 24))
def post_keys2(hwnd):
win32api.PostMessage(
hwnd, win32con.WM_KEYDOWN, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.PostMessage(
hwnd, win32con.WM_CHAR, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0 << 24))
win32api.PostMessage(
hwnd, win32con.WM_KEYUP, ord('A'),
0 + (0 << 8) + (ord('A') << 16) + (0xC0 << 24))
def post_keys3(hwnd):
win32api.SendMessage(hwnd, win32con.WM_CHAR,
ord('A'), 0)
def post_keys4(hwnd):
win32api.PostMessage(hwnd, win32con.WM_CHAR,
ord('A'), 0)
def post_keys5(hwnd):
win32api.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('A'), 0)
win32api.PostMessage(hwnd, win32con.WM_CHAR, ord('A'), 0)
win32api.PostMessage(hwnd, win32con.WM_KEYUP, ord('A'), 0)
def post_keys6(hwnd):
win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, ord('A'), 0)
win32api.SendMessage(hwnd, win32con.WM_CHAR, ord('A'), 0)
win32api.SendMessage(hwnd, win32con.WM_KEYUP, ord('A'), 0)
推荐答案
在我写问题时,我理解SendKeys
是生成键盘输入的正确方法,并且这是在所有情况下均适用的唯一方法.但是,我不能使用SendKeys
,因为在我的程序运行时会主动使用正在运行我的程序的计算机,这意味着可以随时单击鼠标,这将改变窗口的焦点并使SendKeys
开始将输入发送到错误的窗口.
When I wrote the question, I understood that SendKeys
is the correct way to generate keyboard input, and that's the only one that works in all cases. However, I couldn't use SendKeys
, cause the computer my program is running on will be actively used while my program is running, meaning a mouse-click can happen at any time that will change the focus of the window and make SendKeys
start sending input to the wrong window.
我想知道的是,为什么我的代码尤其无法正常工作-我在发送的消息类型上做错什么了吗? Post
与Send
? WPARAM
应该是什么?等等...答案可能是因为我正在将消息发送到记事本"窗口,而不是发送到记事本"中找到的编辑控件-我怀疑这可以正常工作.
What I wanted to know was just why in particular my code wasn't working - was I doing something wrong with the types of messages I was sending? Post
vs. Send
? What should WPARAM
be? Etc... The answer was probably cause I was sending the messages to the Notepad window, and not to the edit control found inside Notepad - I suspect that will work.
无论如何,我尝试将输入发送到我希望它可以实际运行的应用程序,并且最终成功了:
Anyway, I tried sending input to the app I wanted it to actually work on, and this ended up working:
def send_input_hax(hwnd, msg):
for c in msg:
if c == "\n":
win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
else:
win32api.SendMessage(hwnd, win32con.WM_CHAR, ord(c), 0)
因此,答案是我没有在邮件类型或邮件内容方面做任何错,这只是到了错误的目的地.
So the answer is that I wasn't doing anything wrong in terms of the message types or the contents of the message, it was just to an incorrect destination.
这篇关于使用什么SendMessage直接将密钥发送到另一个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!