Fastest way to send C # keystrokes

I was wondering what is the fastest way to send keystrokes using C #. I am currently using SendKeys.Send() and SendKeys.SendWait() with SendKeys.Flush() .

I use the following code to calculate how long they both 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(); 

Results 2:

 Result 1: 40119 milliseconds Result 2: 41882 milliseconds 

Now, if we put SendKeys.Flush() in the second test, outside the loop we get:

 Result 3: 46278 milliseconds 

I was wondering why these changes in the code make the speed completely different.

I was also wondering if there is a faster way to send many keystrokes as my application does this a lot. (These tests were done on a really slow netbook)

Thanks!

+6
source share
2 answers

SendWait() is slower because it expects the message to be processed by the target application. The Send() function instead does not wait and returns as soon as possible. If the application is somehow busy, the difference may be even more obvious.

If you call Flush() , you will stop your application to handle all events related to the keyboard queued in the message queue. This doesn't make much sense if you sent them using SendWait() , and you slow down the application a lot because it is inside the loop (imagine Flush() as selective DoEvents() - with all its flaws - and it calls SendWait() itself SendWait() ).

If you are interested in its performance (but they will always be limited by the speed with which your application can process messages), please read this on MSDN . In general, you can change the SendKeys class to use the SendInput function, rather than a log hook. As a quick link, just add this parameter to the app.config file:

 <appSettings> <add key="SendKeys" value="SendInput"/> </appSettings> 

In any case, the goal of the new implementation is not speed, but consistent behavior in different versions of Windows and options (increased performance is most likely a side effect).

+9
source

If you have a lot of text for clicking on the client, you may notice that SendKeys is really sluggish. You can significantly speed up the work by using the clipboard. The idea is to put the text that you want to "type" in the target text field on the clipboard, and then send CTRL-V to the target application to paste this text. Here is an illustration:

 Clipboard.Clear(); // Always clear the clipboard first Clipboard.SetText(TextToSend); SendKeys.SendWait("^v"); // Paste 

I found that it worked for me with a wireless barcode scanner that talks via WiFi with a host application that sends long barcodes to a web application running on Google Chrome. He walked away from the tedious expiration of 30 digits in about 4 seconds to instantly insert everything in a second.

The obvious drawback is that this can ruin the user's use of the clipboard. Another thing is that this will not help if you intend to send control codes, such as TAB or F5, instead of plain old text.

+9
source

Source: https://habr.com/ru/post/915073/


All Articles