Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
UIElement Class
UIElement Events
 KeyDown Event

  Switch on low bandwidth view
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
UIElement..::.KeyDown Event

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

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public Event KeyDown As KeyEventHandler
Visual Basic (Usage)
Dim instance As UIElement
Dim handler As KeyEventHandler

AddHandler instance.KeyDown, handler
C#
public event KeyEventHandler KeyDown
XAML Attribute Usage
<uiElement KeyDown="eventhandler"/>

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 event is raised by the object that has focus while the key is pressed, and then bubbles to each successive parent object. The bubbling metaphor indicates that the event starts at the source 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.

NoteNote:

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 tied. 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.

KeyUp and OnKeyUp

Controls that inherit KeyDown can provide special handling for the event, 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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker