I was wondering what the fastest way to send keystrokes using C# is. Currently I am using SendKeys.Send() and SendKeys.SendWait() with SendKeys.Flush().
I am using the following code to calculate the time of how long it takes for the both of them to work:
Stopwatch sw1 = new Stopwatch();
sw1.Start();
for (int a = 1; a <= 1000; a++)
{
SendKeys.Send("a");
SendKeys.Send("{ENTER}");
}
sw1.Stop();
And:
Stopwatch sw2 = new Stopwatch();
sw2.Start();
for (int b = 1; b <= 1000; b++)
{
SendKeys.SendWait("b");
SendKeys.SendWait("{ENTER}");
SendKeys.Flush();
}
sw2.Stop();
The results of the 2 are:
Result 1: 40119 milliseconds
Result 2: 41882 milliseconds
Now if we put the SendKeys.Flush() on the second test, outside of the loop we get:
Result 3: 46278 milliseconds
I was wondering why these changes in the code make the speed very different.
I was also wondering if there is a faster way of sending many keystrokes, as my application does it a lot. (These tests were done on a really slow netbook)
Thanks!
SendWait()is slower because it waits that the message has been processed by the target application. TheSend()function instead doesn’t wait and returns as soon as possible. If the application is somehow busy the difference can be even much more evident.If you call
Flush()you’ll stop your application to process all events related to the keyboard that are queued in the message queue. It doesn’t make too much sense if you sent them usingSendWait()and you’ll slow down a lot the application because it’s inside the loop (imagineFlush()as a selectiveDoEvents()– yes with all its drawbacks – and it’s called bySendWait()itself too).If you’re interested about its performance (but they’ll always be limited to the speed at which your application can process the messages) please read this on MSDN. In sum, you can change the
SendKeysclass to use theSendInputfunction, rather than a journal hook. As quick reference, simply add this setting to your app.config file:Anyway, the goal of the new implementation isn’t the speed but consistent behavior across different versions of Windows and and options (the increased performance is kind of a side effect, I guess).