如何以编程方式按 Enter 键?

问题描述 投票:0回答:2

我有一个 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?
    }
c# keyboard sendkeys enter
2个回答
27
投票

取自SendKeys.Send方法

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

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

0
投票

如果您需要重新定位到某个路径,那么此时您需要该对话框的对话框句柄并使用如下所示的发送键:

`IntPtr dialogHandle = FindWindow("#32770", null);
if (dialogHandle != IntPtr.Zero)
{
string filepath = "path you wanted to relocate";
SendKeys.SendWait(Path.Combine(filePath,"{ENTER}"));
}`
© www.soinside.com 2019 - 2024. All rights reserved.