How do I programmatically press Enter?

29,091

Taken from SendKeys.Send Method

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

or

SendKeys.SendWait("{ENTER}"); // How to press enter?
Share:
29,091
user3231442
Author by

user3231442

Updated on July 09, 2022

Comments

  • user3231442
    user3231442 almost 2 years

    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?
        }
    
  • Jason Evans
    Jason Evans over 9 years
    I believe the documentation states that it's "{ENTER}" you need, not "ENTER"
  • nsgocev
    nsgocev over 9 years
    @JasonEvans it uses {Enter} :)
  • Maxter
    Maxter over 3 years
    You link to Send(), but tell us to use SendWait(). Why?
  • nam vo
    nam vo about 2 years
    How can I do that with dotnet core console app ?