Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Keyboard.KeyDown Attached Event

Occurs when a key on the keyboard is pressed.

Namespace: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

See Keyboard.AddKeyDownHandler, Keyboard.RemoveKeyDownHandler
See Keyboard.AddKeyDownHandler, Keyboard.RemoveKeyDownHandler
See Keyboard.AddKeyDownHandler, Keyboard.RemoveKeyDownHandler
<object Keyboard.KeyDown="KeyEventHandler" .../>

Identifier field

KeyDownEvent

Routing strategy

Bubbling

Delegate

KeyEventHandler

The corresponding tunneling event is PreviewKeyDown.

Keyboard.KeyDown is an attached event. For more information, see Routed Events Overview.

The following example creates TextBox that attaches an event handler for the KeyDown event. When the Return is pressed, the event handler displays the text in the TextBox in a TextBlock.

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

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Community Additions

Show:
© 2014 Microsoft