This documentation is archived and is not being maintained.

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

'Declaration
See Keyboard.AddKeyDownHandler, Keyboard.RemoveKeyDownHandler
'Usage
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 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

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.

Show: