KeyEventHandler Delegate


Represents the method that will handle the KeyUp and KeyDown routed events, as well as related attached and Preview events.

public delegate void KeyEventHandler(System::Object ^ sender, KeyEventArgs ^ e);
public delegate void KeyEventHandler(object sender, KeyEventArgs e);
type KeyEventHandler = delegate of obj * KeyEventArgs -> unit
Public Delegate Sub KeyEventHandler(sender As Object, e As KeyEventArgs)



The object where the event handler is attached.


The event data.


The following example checks whether the key associated with the event is the Return key.

private void OnKeyDownHandler(object sender, KeyEventArgs e)
    if (e.Key == Key.Return)
        textBlock1.Text = "You Entered: " + textBox1.Text;
Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs)
    If (e.Key = Key.Return) Then
        textBlock1.Text = "You Entered: " + textBox1.Text
    End If
End Sub


This delegate is used with the following attached events:

This delegate is also used with the following routed events on base elements. These routed events forward the previously listed attached events to make them more accessible to the general element model in WPF.

The attached events and the base element routed events share their event data, and the bubbling and tunneling versions of the routed events also share event data. This can affect the handled characteristics of the event as it travels the event route. For details, see Input Overview.

Extension Methods


Gets an object that represents the method represented by the specified delegate.

Applies to

See also