Nitsua

Hi All,

I am a new Visual Studios user and new C# programmer. Currently I am trying to figure out a way to automate a program install (hitting Next buttons, and then Finish basically) by the way of SendKeys, but I am running into problems.

Right the program brings the install window to the foreground and sometimes sends the Enter key correctly. Not really sure why it only works sometimes, other times it does nothing. The big problem is that after the Enter key is sent it moves onto the next part of the installation, and none of my further keys get sent correctly. I thought it was a timing problem so I tried to insert a for loop in order to delay the code execution for a bit...didn't work either.

Here is my code so far:

Code Snippet

using System;

using System.Runtime.InteropServices;

using System.Drawing;

using System.Windows.Forms;

namespace SimulateKeyPress

{

class Form1 : Form

{

private Button button1 = new Button();

[STAThread]

public static void Main()

{

Application.EnableVisualStyles();

Application.Run(new Form1());

}

public Form1()

{

button1.Location = new Point(10, 10);

button1.TabIndex = 0;

button1.Text = "Click to Automate Install";

button1.AutoSize = true;

button1.Click += new EventHandler(button1_Click);

this.DoubleClick += new EventHandler(Form1_DoubleClick);

this.Controls.Add(button1);

}

// Get a handle to an application window.

[DllImport("USER32.DLL")]

public static extern IntPtr FindWindow(string lpClassName,

string lpWindowName);

// Activate an application window.

[DllImport("USER32.DLL")]

public static extern bool SetForegroundWindow(IntPtr hWnd);

// Send a series of key presses to the Install application.

private void button1_Click(object sender, EventArgs e)

{

// Get a handle to the Install application. The window class

// and window name were obtained using the Spy++ tool.

IntPtr InstallHandle = FindWindow("InstallShield_Win", "CYMDIST 4.5 Rev 15");

// Verify that Calculator is a running process.

if (InstallHandle == IntPtr.Zero)

{

MessageBox.Show("Calculator is not running.");

return;

}

//SetForegroundWindow(InstallHandle);

IntPtr InstallHandle2 = FindWindow("#32770", "CYMDIST 4.5 Rev 15");

// Make Install the foreground application and send it

// a set of instructions.

SetForegroundWindow(InstallHandle2);

//SendKeys.SendWait("{TAB}");

SendKeys.SendWait("~");

for (int i = 0; i == 100000000; i++);

SendKeys.SendWait("~");

SendKeys.SendWait("~");

}

// Send a key to the button when the user double-clicks anywhere

// on the form.

private void Form1_DoubleClick(object sender, EventArgs e)

{

// Send the enter key to the button, which raises the click

// event for the button. This works because the tab stop of

// the button is 0.

SendKeys.Send("{ENTER}");

}

}

}

Any help is appreciated.

Thanks!



Re: Windows Forms General Need Help With SendKey Function

boban.s

I don't know why are you sending "~" key but anyhow. Wait a little after you send a key:
SendKeys.SendWait("{TAB}");
Thread.CurrentThread.Join(200);
SendKeys.SendWait("{TAB}");
Thread.CurrentThread.Join(200);
SendKeys.SendWait("{ENTER}");




Re: Windows Forms General Need Help With SendKey Function

Nitsua

I was using "~" because it also acts as the ENTER button. At least that was what I read and it had been working, but I switched "~" to "{ENTER}" just in case.

Thanks for the Treading suggestion, it worked perfectly. I am new to C# like I said and knew nothing about Threading until now.


Thanks Againt!