I am trying to make C# launch an application (in this case open office), and start sending that application keypresses such that it would appear as though someone is typing.
I did this using SetForegroundWindow and SendKeys.
I used it for this.
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public void SendText(IntPtr hwnd, string keys)
{
if (hwnd != IntPtr.Zero)
{
if (SetForegroundWindow(hwnd))
{
System.Windows.Forms.SendKeys.SendWait(keys);
}
}
}
This can be used as simply as this.
Process p = Process.Start("notepad.exe");
SendText(p.MainWindowHandle, "Hello, world");