views:

48

answers:

1

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

How to use SendWait() without SetForegroundWindow()?

SendWait example: http://msdn.microsoft.com/en-us/library/ms171548.aspx

A: 

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.

TheCloudlessSky