2

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

Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
user431062
  • 23
  • 1
  • 3

1 Answers1

1

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
  • 18,608
  • 15
  • 75
  • 116