Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
KeyEventArgs Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
Public Class KeyEventArgs _
    Inherits KeyboardEventArgs
Visual Basic (Usage)
Dim instance As KeyEventArgs
C#
public class KeyEventArgs : KeyboardEventArgs
Visual C++
public ref class KeyEventArgs : public KeyboardEventArgs
JScript
public class KeyEventArgs extends KeyboardEventArgs
XAML
You cannot directly create an instance of this class in XAML.

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. For the complete samples, see Keyboard Key Sample.

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.

Visual Basic
Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs)
    If (e.Key = Key.Return) Then
        textBlock1.Text = "You Entered: " + textBox1.Text
    End If
End Sub

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Control + Alt + Shift      Den71   |   Edit   |   Show History
To get control of a combination, for example CTRL+L use this code:

private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.L && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
// Do something
}
}
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker