Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically press Enter?

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?
    }
like image 901
user3231442 Avatar asked Aug 20 '14 14:08

user3231442


People also ask

How do you press Enter key programmatically in react?

Using the onKeypress event The onKeyPress event is fired when a user presses the key on a keyboard, so that by using this we can trigger the button click by pressing a Enter key. The keyCode for the Enter key is 13.


1 Answers

Taken from SendKeys.Send Method

SendKeys.SendWait("~"); // How to press enter?

or

SendKeys.SendWait("{ENTER}"); // How to press enter?
like image 154
nsgocev Avatar answered Sep 24 '22 03:09

nsgocev