UIElement.KeyDown Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs when a keyboard key is pressed while the UIElement has focus.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)


Public Event KeyDown As KeyEventHandler
public event KeyEventHandler KeyDown
<uiElement KeyDown="eventhandler"/>


Use a handler based on KeyEventHandler to handle this event. To determine the specific key that was pressed, check Key or PlatformKeyCode. To determine if any modifier keys are pressed, check Keyboard.Modifiers. For more information on how to handle key events, and whether to use Key or PlatformKeyCode, see Keyboard Support.

The KeyDown event is a bubbling event. This means that if multiple KeyDown event handlers are registered for a sequence of objects connected by parent-child relationships in the object tree, the event is received by each object in that relationship. The bubbling metaphor indicates that the event starts at the object that has focus while the key is pressed, and works its way up the object tree. For a bubbling event, the sender available to the event handler identifies the object where the event is handled, not necessarily the object that actually received the input condition that initiated the event. To get the object that initiated the event, use the OriginalSource value of the event's RoutedEventArgs event data.


Handling keyboard events might vary between browsers. When you create an application that uses keyboard input, make sure to test the application in your target browsers.

Element focus and the ability to initiate a keyboard event are related concepts. In order to generate the KeyDown event, the UIElement must have focus.

On the TextBox class, there is a TextChanged event that has potential interactions with KeyDown, as well as special handling for keys such as ENTER. For details, see TextBox.

In most cases, if a key remains held down, Silverlight repeatedly raises the KeyDown event at a system-determined interval. However, some browser hosts do not provide auto-repeat key-down information to their plug-in integration layer, and therefore Silverlight cannot auto-repeat KeyDown for these cases.

KeyUp and OnKeyUp

Controls that inherit KeyDown can provide handling for the event that acts as handler for all instances, by overriding the OnKeyDown method. This might include marking the Handled value of the event as true, which has the effect of suppressing further handling of the KeyDown event for some or all keys on any instance of the control (and potentially any subclass of the control). For instance, ButtonBase implements OnKeyDown in such a way that the SPACE key invokes a button click, but also sets Handled, which prevents SPACE from being reported as a KeyDown event that could be handled by a button instance. For more information, see OnKeyDown.

You can also register handlers such that they are invoked even for already-handled routed events. For more information, see AddHandler.

Version Information


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0


For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.