PostMessage WM_KEYDOWN send multiply keys?

感情迁移 提交于 2020-01-09 05:33:26

问题


I have this code:

    public static void Next()
    {
        Process[] processes = Process.GetProcessesByName("test");

        foreach (Process proc in processes)
            PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_RIGHT, 0);
    }

This code sents the Right Arrow key, i want to sent ALT+CTRL+RIGHT i tried this:

    public static void Forward()
    {
        Process[] processes = Process.GetProcessesByName("test");

        foreach (Process proc in processes)
        {
            PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_CONTROL, 0);
            PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_ALT, 0);
            PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_RIGHT, 0);
        }
    }

But it doesn't work...

Any ideas?


回答1:


You can't simulate keyboard input with PostMessage, at least not reliably use SendInput instead.




回答2:


I have tried this so many times and it's hit or miss if it works. What you want to do is try to use WM_SYSKEYDOWN instead of WM_KEYDOWN for "system" type keys. This also means you have to use WM_SYSKEYUP. Something like this might work:

PostMessage(proc.MainWindowHandle, WM_SYSKEYDOWN, VK_CONTROL, 0); 
PostMessage(proc.MainWindowHandle, WM_SYSKEYDOWN, VK_ALT, 0); 
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_RIGHT, 0); 
PostMessage(proc.MainWindowHandle, WM_SYSKEYUP, VK_ALT, 0); 
PostMessage(proc.MainWindowHandle, WM_SYSKEYUP, VK_CONTROL, 0); 

Update:

I've it only to simulate key presses for single keys, it works great even for minimized applications :). When using it as a combination key for "shift" states is where it's hit or miss. The problem is most windows applications have a control and each control has it's on handle so sending a key to the window doesn't have the desired affect, you have to send ALT+S to the "Menu" handle to make a file save (in say Notepad), which also works.



来源:https://stackoverflow.com/questions/7732633/postmessage-wm-keydown-send-multiply-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!