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

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

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

我需要

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

// 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
,但我希望你们对如何解决我的问题有一些更好的建议。

c# .net sendkeys
2个回答
7
投票

使用输入模拟器。它比

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();

0
投票

我不确定这是否有帮助,但如果没有的话请批评这篇文章,以便我可以尽力帮助您解决问题。

 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!");
    }
© www.soinside.com 2019 - 2024. All rights reserved.