Reputation: 620
I have a C# console program which starts calculator and simulates key presses. How do I programmatically press Enter?
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
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 Calculator application.
private void StartCalculator()
{
Process.Start("calc.exe");
IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
if (calculatorHandle == IntPtr.Zero)
{
return;
}
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
SendKeys.SendWait(" ");//how press enter?
}
Upvotes: 10
Views: 40705
Reputation: 91
If you need to relocate to a path then that time you need a dialog handle for that dialog box and use sendkeys like the below:
IntPtr dialogHandle = FindWindow("#32770", null);
if (dialogHandle != IntPtr.Zero)
{
string filepath = "path you wanted to relocate";
SendKeys.SendWait(Path.Combine(filePath,"{ENTER}"));
}
Upvotes: 0
Reputation: 4470
Taken from SendKeys.Send Method
SendKeys.SendWait("~"); // How to press enter?
or
SendKeys.SendWait("{ENTER}"); // How to press enter?
Upvotes: 30