2015-07-19 135 views
2

我认为标题解释了我所要求的。C#等待SendKeys在执行下一行代码之前完成发送?

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

+0

我们可以计算出'SendKeys'在另一个线程和'加入()'这个线程。 http://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net –

+0

我以为'SendKeys'在返回之前做了等待吗? MSDN:_“[使用SendWait将击键或击键组合发送给活动应用程序**并等待按键消息被处理**。您可以使用此方法将击键发送到应用程序,并等待任何如果其他应用程序必须在您的应用程序可以继续之前完成,这可能很重要。](https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys。 sendwait(v = vs.110).aspx)“_ – MickyD

+0

大约是文档的一半:另外,当SendKeys类使用新的实现时,SendWait方法不会等待消息在发送给另一个时处理处理。 ' – Xenolightning

回答

3

使用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(); 
-1

我不确定这是否有帮助,但如果不批评这篇文章,所以我可以尝试帮助你的情况。

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!"); 
    } 
+1

感谢发布!但我没有看到这会产生什么差别,这基本上已经是我的代码看起来像。 – DooMLaZyPro

+0

所以你下了这个票。那很便宜。无论如何,也许你应该尝试发布为什么不起作用。这个问题对我来说似乎很模糊。它的工作原理应该如此。你打算如何使用它? –

+0

为了记录我不是那个推倒你的人。 – DooMLaZyPro

相关问题