Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 == "
":
            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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...