what SendMessage to use to send keys directly to another window?

后端 未结 1 1765
深忆病人
深忆病人 2020-12-17 07:20

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\'

相关标签:
1条回答
  • 2020-12-17 07:33

    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.

    0 讨论(0)
提交回复
热议问题