Saturday, May 29, 2010

Simulate Key Press

For simulating key strokes in C# there is aswell an easy function.
The class SendKeys provides corresponding possibilites.
The functions Send() and SendWait() exist. Both functions expect a string as a parameter, which is then send to the active program, as if the user had pressed that key.
SendWait() waits in contrast to Send() for the procession of the input.
The following code can for example be copied in the Click - Event of a button.
If the code is executed, the program waits for 3 seconds, the user can switch to a different program (e.g. a chat program - do not abuse, haha) and the application sends the message "WICHTIG!" 100 times:
for (int i = 0; i < 100; i++)
    SendKeys.Send("WICHTIG! \n");

"\n" adds a line break to the text, but key presses of special keys like the Enter key can also be simulated, Enter for example with "{ENTER}" (as a string, meaning with quotation marks like a normal text).


  1. I am very happy to locate your website. I just wanted to thank you for the time you spent on this great article. I definitely enjoyed reading it and I have you bookmarked to check out new stuff you post.


  2. I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.