使用什么 SendMessage 将密钥直接发送到另一个窗口?

发布于 2024-10-18 09:00:30 字数 1752 浏览 1 评论 0原文

我正在尝试使用 SendMessage 将键盘输入发送到另一个窗口。我知道缺点,但我必须这样做,因为我必须发送几个键,并且我不能保证窗口将具有焦点 - 所以当窗口没有焦点时,这必须起作用。

我正在通过尝试将密钥发送到记事本窗口来测试它。我尝试过以下变体,但没有一个有效:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-10-25 09:00:30

当我写这个问题时,我明白 SendKeys 是生成键盘输入的正确方法,并且这是唯一在所有情况下都有效的方法。但是,我无法使用 SendKeys,因为我的程序运行所在的计算机将在我的程序运行时被主动使用,这意味着鼠标单击可能随时发生,从而改变程序的焦点窗口并使 SendKeys 开始将输入发送到错误的窗口。

我想知道的是为什么我的代码无法正常工作——我发送的消息类型是否有问题? 发布发送WPARAM 应该是什么?等等...答案可能是因为我将消息发送到记事本窗口,而不是发送到记事本内的编辑控件 - 我怀疑这会起作用。

不管怎样,我尝试将输入发送到我希望它实际工作的应用程序,这最终成功了:

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)

所以答案是,我在消息类型或消息内容方面没有做任何错误,它是只是到了一个错误的目的地。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文