Jaa


UIElement.KeyDown Event

Definition

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

public:
 virtual event KeyEventHandler ^ KeyDown;
// Register
event_token KeyDown(KeyEventHandler const& handler) const;

// Revoke with event_token
void KeyDown(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::KeyDown_revoker KeyDown(auto_revoke_t, KeyEventHandler const& handler) const;
public event KeyEventHandler KeyDown;
function onKeyDown(eventArgs) { /* Your code */ }
uIElement.addEventListener("keydown", onKeyDown);
uIElement.removeEventListener("keydown", onKeyDown);
- or -
uIElement.onkeydown = onKeyDown;
Public Custom Event KeyDown As KeyEventHandler 
<uiElement KeyDown="eventhandler"/>

Event Type

Remarks

Controls in your UI generate keyboard events only when they have input focus. By default, the first focusable element in the visual tree is given focus by the system. An individual control gains focus when the user clicks or taps directly on that control in the layout, or uses the Tab key to step into a tab sequence within the content area. You can also focus controls programmatically by calling Control.Focus.

KeyDown is a routed event. For more info on the routed event concept, see Events and routed events overview.

KeyDown uses KeyRoutedEventArgs event data. The most relevant properties of KeyRoutedEventArgs for most handler scenarios are Key and possibly KeyStatus. For more info on handling keyboard events, including example code for defining a KeyEventHandler method, see Keyboard interactions.

One scenario for handling keyboard events is to support access keys or accelerator keys for an app, or a region or control within an app. For more info about this scenario, see Keyboard accessibility.

Specific Windows Runtime controls may have class-based handling for the KeyDown input event. If so, the control probably has an override for the method OnKeyDown. Typically these class handlers are intended to process a subset of key presses that enable a keyboard-based user interaction with that control, and often this interaction supports a keyboard accessibility feature. If a key press is handled by class-based handling, then the key press is considered to be already handled, and the KeyDown event is not raised for handling by any user code handlers on that control for that key specifically. Usually this is just for a few dedicated keys. For example, ButtonBase has class handling so that the Space key and Enter key are handled as keyboard equivalents for invoking the Button. That provides a built-in keyboard equivalent for tapping the button or clicking on it with a mouse, and enables the accessibility scenario of using the UI and the button using a keyboard only. But any other keys other than Space or Enter will still fire a KeyDown and KeyUp event for a Button. For more info on how class-based handling for events works, see Events and routed events overview.

KeyDown supports the ability to attach event handlers to the route that will be invoked even if the event data for the event is marked Handled. See AddHandler.

Applies to

See also