Simulating Key Press in C#
12,761
Solution 1
I would use the SendKeys class, documented here.
To send an 'Esc' keypush, for example, you would write:
SendKeys.Send("{ESC}");
Solution 2
I would use the Windows Input Simulator as it is more reliable than Sendkeys
.
Related videos on Youtube
Author by
user3026404
Updated on June 04, 2022Comments
-
user3026404 almost 2 years
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?
-
Faizan almost 9 yearsI 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 almost 9 yearsWhat platform are you using? WinForms?
-
Faizan almost 9 yearsI am using WPF C#. I have a file called MainWindow.xaml.cs and yes I am using windows forms too inside.
-
OneFineDay almost 9 yearsI just looked at my project where I used this and the import statement was
using WindowsInput;
- notusing system.windows.input;
-
Michael over 6 yearsIf anybody is interested, it works perfect with Unity3D :)