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.

Share:
12,761

Related videos on Youtube

user3026404
Author by

user3026404

Updated on June 04, 2022

Comments

  • user3026404
    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
    Faizan almost 9 years
    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
    OneFineDay almost 9 years
    What platform are you using? WinForms?
  • Faizan
    Faizan almost 9 years
    I am using WPF C#. I have a file called MainWindow.xaml.cs and yes I am using windows forms too inside.
  • OneFineDay
    OneFineDay almost 9 years
    I just looked at my project where I used this and the import statement was using WindowsInput; - not using system.windows.input;
  • Michael
    Michael over 6 years
    If anybody is interested, it works perfect with Unity3D :)