Failing to send key presses to a running process in C#

Posted by Waffles on Stack Overflow See other posts from Stack Overflow or by Waffles
Published on 2010-06-01T01:16:06Z Indexed on 2010/06/01 1:23 UTC
Read the original article Hit count: 293

Filed under:
|
|

I'm using the following code to put the focus on a window (in this case, a notepad window), and sending some key presses to it everytime button 2 is clicked. However, when I press button 2, nothing happens. Can anyone tell my why my sendkeys command is failing?

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    private Process s;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.s = new Process();
        s.StartInfo.FileName = "notepad";
        s.Start();
        s.WaitForInputIdle();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        ShowWindow(s.MainWindowHandle, 1);
        SendKeys.SendWait("Hello");
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms