如何通过编程方式按下回车键?

8
我有一个C#控制台程序,它启动计算器并模拟按键。如何在程序中编程按下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?
    }

参考链接:https://dev59.com/nGPVa4cB1Zd3GeqP1wi6 - Jason Evans
1个回答

26

来自SendKeys.Send 方法

SendKeys.SendWait("~"); // How to press enter?
或者
SendKeys.SendWait("{ENTER}"); // How to press enter?

我相信文档中指出你需要的是 "{ENTER}" 而不是 "ENTER" - Jason Evans
1
@JasonEvans 它使用 {Enter} :) - nsgocev
你链接到Send(),但告诉我们使用SendWait()。为什么? - Maxter
我该如何使用dotnet core控制台应用程序实现这个功能? - nam vo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接