Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

KeyEventArgs Class

Provides data for the UIElement.KeyUp and UIElement.KeyDown routed events, as well as related attached and Preview events.

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
public class KeyEventArgs : KeyboardEventArgs

The KeyEventArgs type exposes the following members.

  NameDescription
Public methodKeyEventArgsInitializes a new instance of the KeyEventArgs class.
Top
  NameDescription
Public propertyDeadCharProcessedKeyGets the key that is part of dead key composition to create a single combined character.
Public propertyDeviceGets the input device that initiated this event. (Inherited from InputEventArgs.)
Public propertyHandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. (Inherited from RoutedEventArgs.)
Public propertyImeProcessedKeyGets the keyboard key referenced by the event, if the key will be processed by an Input Method Editor (IME).
Public propertyInputSourceGets the input source that provided this input.
Public propertyIsDownGets a value that indicates whether the key referenced by the event is in the down state.
Public propertyIsRepeatGets a value that indicates whether the keyboard key referenced by the event is a repeated key.
Public propertyIsToggledGets a value that indicates whether the key referenced by the event is in the toggled state.
Public propertyIsUpGets a value that indicates whether the key referenced by the event is in the up state.
Public propertyKeyGets the keyboard key associated with the event.
Public propertyKeyboardDeviceGets the keyboard device associated with the input event. (Inherited from KeyboardEventArgs.)
Public propertyKeyStatesGets the state of the keyboard key associated with this event.
Public propertyOriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. (Inherited from RoutedEventArgs.)
Public propertyRoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. (Inherited from RoutedEventArgs.)
Public propertySourceGets or sets a reference to the object that raised the event. (Inherited from RoutedEventArgs.)
Public propertySystemKeyGets the keyboard key referenced by the event, if the key will be processed by the system.
Public propertyTimestampGets the time when this event occurred. (Inherited from InputEventArgs.)
Top
  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodInvokeEventHandlerInvokes event handlers in a type-specific way, which can increase event system efficiency. (Overrides KeyboardEventArgs.InvokeEventHandler(Delegate, Object).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnSetSourceWhen overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. (Inherited from RoutedEventArgs.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

This event data class is used with the following attached events:

This event data class 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.

A key can be in both the up and toggled states or the down and toggled states. For this reason, determining whether a key is up or down is not as simple as checking the KeyStates value as a numeric value. Instead, you should check the value by treating it as a flag enumeration. Use an AND comparison of the first bit. Alternatively, use the helper properties IsUp, IsDown, and IsToggled to determine whether a given key is up, down, or toggled.

This example shows how to detect when the Enter key is pressed on the keyboard.

This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file.

When the user presses the Enter key in the TextBox, the input in the text box appears in another area of the user interface (UI).

The following XAML creates the user interface, which consists of a StackPanel, a TextBlock, and a TextBox.

<StackPanel>
  <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

The following code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a message is displayed in the TextBlock.

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.