Expand Minimize

Control.OnKeyDown method

Applies to Windows and Windows Phone

Called before the KeyDown event occurs.

Syntax

Protected Overridable Sub OnKeyDown(
  e As KeyRoutedEventArgs 
)

Parameters

e

Type: KeyRoutedEventArgs

The data for the event.

Remarks

OnKeyDown represents a prewired event handler for the UIElement.KeyDown event. Practical controls that derive from Control can override the OnKeyDown method and use this to provide control-specific handling and behavior for that input event. The most common scenario is to use the event handler to mark the event as Handled in the event data. The control code has first chance to handle this event, before any event handlers that are wired on a control instance are invoked. When the event data is marked Handled, then other handlers like those on the control instance won't be called. Also, the event won't bubble. For more info, see the "On* event handler overrides" section in Control.

As it's implemented directly on Control, OnKeyDown has an empty implementation. But each ancestor in a control's hierarchy may have provided an implementation. You won't be able to see this implementation because it's internal native code. In some cases a control will already have existing On* overrides that mark the event Handled. For key events, controls are usually only handling for certain keys, by checking values in KeyRoutedEventArgs. For example, ButtonBase detects the Space key as a way to fire Click. Control code or your code probably shouldn't be suppressing all key events, because it's a common pattern to let key events bubble to the root visual so that they can be shortcuts or accelerators for app interaction. For more info see Responding to keyboard interactions.

Once you've provided an initial On* override for a control, then any controls that you further derive from your own control class would also inherit the On* overrides you define. Any instances you use have that behavior too.

Note  App code can still handle events that may have been marked Handled by a control's On* method logic, but they need to use the handledEventsToo parameter for the UIElement.AddHandler method. For more info, see UIElement.AddHandler or Events and routed events overview.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.winmd

See also

Control
UIElement.KeyDown
KeyRoutedEventArgs
Responding to keyboard interactions
Events and routed events overview

 

 

Show:
© 2014 Microsoft