C# 在执行下一行代码之前等待 SendKeys 完成发送?

C# Wait for SendKeys to finish sending before executing next line of code?

我认为标题解释了我的问题。

我需要 SendKeysDoMouseClick 方法之前完成发送密钥,否则我的整个程序就没用了。

// Simulate the keyboard typing the value of 'i'
SendKeys.SendWait(i.ToString());

// Simulate mouse click so the Accept button in-game is clicked
DoMouseClick();

我尝试使用 Thread.Sleep,但我希望你们能就如何解决我的问题提出更好的建议。

我不确定这是否有帮助,但如果没有,请批评一下post,这样我可以尝试在您的情况下提供帮助。

 private void Form1_DoubleClick(object sender, MouseEventArgs e)
    {
      // Send the enter key; since the tab stop of Button1 is 0, this 
      // will trigger the click event.
      SendKeys.SendWait("{ENTER}");
      CallAfterSend();
    }

    private void CallAfterSend()
    {
      MessageBox.Show("Had to hit enter first");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
      MessageBox.Show("Click here!");
    }

使用Input Simulator。它比 SendKeys 好得多,并且在内部处理边缘情况。

Install-Package InputSimulator

var simulator = new InputSimulator();

//simulate what you need
simulator.Keyboard.KeyPress(VirtualKeyCode.VK_I);
simulator.Keyboard.TextEntry(i.ToString());

simulator.Mouse.LeftButtonDoubleClick();