How to send Ctrl/Shift/Alt + Key combinations to an application window? (via SendMessage)

后端 未结 5 638
日久生厌
日久生厌 2021-02-13 18:24

I can successfully send any single key message to an application, but I don\'t know how to send combinations of keys (like Ctrl+F12, Shift+

5条回答
  •  醉酒成梦
    2021-02-13 19:10

    If you want to simulate keystrokes to minimized windows you can do something like this:

    uint windowThreadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
    uint myThreadId = GetCurrentThreadId();
    AttachThreadInput(myThreadId, windowThreadId, true);
    

    Next step is to use GetKeyboardState function to retrieve an array of all keys states of the window thread. Modify the state of SHIFT or CTRL or ALT keys to being pressed using their virtual key codes. Then call SetKeyboardState to apply these states. Press the F12 key:

    SendMessage(hwnd, WM_KEYDOWN, Keys.F12, 0);
    SendMessage(hwnd, WM_KEYUP, Keys.F12, 0);
    

    Modify back the states of SHIFT, CTRL or ALT keys as being released. Call SetKeyboardState again. Finally, detach from window thread:

    AttachThreadInput(myThreadId, windowThreadId, false);
    

提交回复
热议问题