0

我有一个应用程序,根据用户请求将一些键盘命令发送到另一个程序。为此,我使用http://inputsimulator.codeplex.com/

问题是,目标应用程序时不时地不会收到键盘按下。

我写了一个模拟这个的测试程序,见下文。有些运行,它运行良好,有些运行它经常失败。这是什么原因造成的?我该如何预防?一种方法当然是在键盘按下之前添加延迟,但问题是延迟多长时间是合理的。理想情况下,我想避免延误。

private void SendKeyTest(IntPtr mainWindow, IntPtr receiverWindow)
{
    int index = 0;

    while (!_cancle)
    {
        // Set receiver application as active
        NativeMethods.SetForegroundWindow(receiverWindow);

        // Send the keyboard presses
        InputSimulator.SimulateTextEntry(string.Format("{0,5:N0}\n", index++));

        // Return to the main application
        NativeMethods.SetForegroundWindow(mainWindow);

        Thread.Sleep(500);
    }
}
4

0 回答 0