So I want to make my program automatically simulate a key press after a timer stops. I have the timer and every setup already I just need to somehow simulate a key press. After a download message pops up on a web browser in the application I just want the program to simulate as "ESC" key press to exit out of the download message popup. How would I simulate the key press?
3 Answers
I would use the SendKeys class, documented here.
To send an 'Esc' keypush, for example, you would write:
SendKeys.Send("{ESC}"); Comments
I would use the Windows Input Simulator as it is more reliable than Sendkeys.
5 Comments
Faizan
I have installed that package using nuget in my project. And I have also added using system.windows.input; on top of my code. but when i use this InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN); it says, windowsinputSimulator does not contain definiton of simulate key press. Can you please tell me what I am doing wrong?
OneFineDay
What platform are you using? WinForms?
Faizan
I am using WPF C#. I have a file called MainWindow.xaml.cs and yes I am using windows forms too inside.
OneFineDay
I just looked at my project where I used this and the import statement was
using WindowsInput; - not using system.windows.input;Michael
If anybody is interested, it works perfect with Unity3D :)