简体   繁体   中英

How to simulate Keyboard Events in Code in not active window?

How do I use SendWait() to send key strokes to a window without using SetForegroundWindow() to make the target window active?

Here is the SendWait example on the MSDN site: http://msdn.microsoft.com/en-us/library/ms171548.aspx

See this thread . Basically given some handle to a window, you need to use p/invoke and call PostMessage with the WM_KEYDOWN message:

private const int VK_RETURN = 0x0D;
private const uint WM_KEYDOWN = 0x0100;

[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);    

public void SendKeysToWindow(IntPtr hWnd)
{
    PostMessage(hWnd, WM_KEYDOWN, new IntPtr(VK_RETURN), IntPtr.Zero);
}

Here's the list of Virtual Keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM