好的,我正在 WatiN 中运行测试,并且使用 SendKeys 方法。根据MSDN网站我可以输入:
System.Windows.Forms.SendKeys.SendWait("{LEFT 2}");
这将向左输入两次。然而,我相信这不起作用,因为应用程序在每次按键之间需要时间。我为了做我需要程序做的事情,我在每次按键之间使用 Thread.Sleep 以确保它们被读取。 有更有效/正确的方法吗?这是我当前的方法代码:
System.Windows.Forms.SendKeys.SendWait("{LEFT}");
Thread.Sleep(500);
System.Windows.Forms.SendKeys.SendWait("{LEFT}");
Thread.Sleep(500);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
最佳答案
不幸的是,我不相信有这样的事情。根据MSDN SendKeys 存在计时问题:
The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds.
The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation.
As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process. If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.
<appSettings>
<add key="SendKeys" value="SendInput"/>
</appSettings>
To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.
您可以尝试更改实现,看看结果是否有变化。
或者,根据这个post只是使用 Thread.Sleep(0);
您的输入应该起作用后。这不是最优雅的解决方案,但如果它有效,它将比 500 毫秒的暂停更快。
关于c# - 发送多个按键 WatiN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8577427/