How to simulate keyboard events (Windows Forms .NET)

Windows Forms provides a few options for programmatically simulating keyboard input. This article provides an overview of these options.

Use SendKeys

Windows Forms provides the System.Windows.Forms.SendKeys class for sending keystrokes to the active application. There are two methods to send keystrokes to an application: SendKeys.Send and SendKeys.SendWait. The difference between the two methods is that SendWait blocks the current thread when the keystroke is sent, waiting for a response, while Send doesn't. For more information about SendWait, see To send a keystroke to a different application.

Caution

If your application is intended for international use with a variety of keyboards, the use of SendKeys.Send could yield unpredictable results and should be avoided.

Behind the scenes, SendKeys uses an older Windows implementation for sending input, which may fail on modern Windows where it's expected that the application isn't running with administrative rights. If the older implementation fails, the code automatically tries the newer Windows implementation for sending input. Additionally, when the SendKeys class uses the new implementation, the SendWait method no longer blocks the current thread when sending keystrokes to another application.

Important

If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.

<appSettings>
  <add key="SendKeys" value="SendInput"/>
</appSettings>

To force the SendKeys class to only use the previous implementation, use the value "JournalHook" instead.

To send a keystroke to the same application

Call the SendKeys.Send or SendKeys.SendWait method of the SendKeys class. The specified keystrokes will be received by the active control of the application.

The following code example uses Send to simulate pressing the ALT and DOWN keys together. These keystrokes cause the ComboBox control to display its dropdown. This example assumes a Form with a Button and ComboBox.

private void button1_Click(object sender, EventArgs e)
{
    comboBox1.Focus();
    SendKeys.Send("%+{DOWN}");
}
Private Sub Button1_Click(sender As Object, e As EventArgs)
    ComboBox1.Focus()
    SendKeys.Send("%+{DOWN}")
End Sub

To send a keystroke to a different application

The SendKeys.Send and SendKeys.SendWait methods send keystrokes to the active application, which is usually the application you're sending keystrokes from. To send keystrokes to another application, you first need to activate it. Because there's no managed method to activate another application, you must use native Windows methods to focus the other application. The following code example uses platform invoke to call the FindWindow and SetForegroundWindow methods to activate the Calculator application window, and then calls Send to issue a series of calculations to the Calculator application.

The following code example uses Send to simulate pressing keys into the Windows 10 Calculator application. It first searches for an application window with title of Calculator and then activates it. Once activated, the keystrokes are sent to calculate 10 plus 10.

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

private void button1_Click(object sender, EventArgs e)
{
    IntPtr calcWindow = FindWindow(null, "Calculator");

    if (SetForegroundWindow(calcWindow))
        SendKeys.Send("10{+}10=");
}
<Runtime.InteropServices.DllImport("USER32.DLL", CharSet:=Runtime.InteropServices.CharSet.Unicode)>
Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr : End Function

<Runtime.InteropServices.DllImport("USER32.DLL")>
Public Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean : End Function

Private Sub Button1_Click(sender As Object, e As EventArgs)
    Dim hCalcWindow As IntPtr = FindWindow(Nothing, "Calculator")

    If SetForegroundWindow(hCalcWindow) Then
        SendKeys.Send("10{+}10=")
    End If
End Sub

Use OnEventName methods

The easiest way to simulate keyboard events is to call a method on the object that raises the event. Most events have a corresponding method that invokes them, named in the pattern of On followed by EventName, such as OnKeyPress. This option is only possible within custom controls or forms, because these methods are protected and can't be accessed from outside the context of the control or form.

These protected methods are available to simulate keyboard events.

  • OnKeyDown
  • OnKeyPress
  • OnKeyUp

For more information about these events, see Using keyboard events (Windows Forms .NET).

See also