2

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?

user3026404
  • 21
  • 1
  • 1
  • 2

3 Answers3

5

I would use the SendKeys class, documented here.

To send an 'Esc' keypush, for example, you would write:

SendKeys.Send("{ESC}");
Baldrick
  • 11,712
  • 2
  • 31
  • 35
3

I would use the Windows Input Simulator as it is more reliable than Sendkeys.

OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • 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? – Faizan May 19 '15 at 13:40
  • What platform are you using? WinForms? – OneFineDay May 19 '15 at 18:39
  • I am using WPF C#. I have a file called MainWindow.xaml.cs and yes I am using windows forms too inside. – Faizan May 19 '15 at 19:24
  • I just looked at my project where I used this and the import statement was `using WindowsInput;` - not `using system.windows.input;` – OneFineDay May 20 '15 at 03:11
  • 1
    If anybody is interested, it works perfect with Unity3D :) – Michael Sep 23 '17 at 19:32
-2

on the client side, use the jQuery.trigger command

edepperson
  • 1,035
  • 1
  • 14
  • 33