Keyboard.KeyDown 附加事件
定义
当按下键盘上的某个键时发生。Occurs when a key on the keyboard is pressed.
see AddKeyDownHandler, and RemoveKeyDownHandler
see AddKeyDownHandler, and RemoveKeyDownHandler
see AddKeyDownHandler, and RemoveKeyDownHandler
示例
下面的示例创建 TextBox 用于附加事件的事件处理程序 KeyDown 。The following example creates TextBox that attaches an event handler for the KeyDown event. Return按下时,事件处理程序将 TextBox 在中的中显示文本 TextBlock 。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;
}
}
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
注解
这是一个附加事件。This is an attached event. WPFWPF 将附加事件作为路由事件实现。implements attached events as routed events. 附加事件本质上是一种 XAMLXAML 用于引用事件的语言概念,这些事件可在未定义该事件的对象上进行处理,后者 WPFWPF 通过同时启用事件来遍历路由。Attached events are fundamentally a XAMLXAML language concept for referencing events that can be handled on objects that do not define that event, which WPFWPF expands upon by also enabling the event to traverse a route. 附加事件在代码中没有直接处理语法;若要在代码中附加路由事件的处理程序,请使用指定的 Add * 处理程序方法。Attached events do not have a direct handling syntax in code; to attach handlers for a routed event in code, you use a designated Add*Handler method. 有关详细信息,请参阅 附加事件概述。For details, see Attached Events Overview.
路由事件信息Routed Event Information
标识符字段Identifier field | KeyDownEvent |
路由策略Routing strategy | 冒泡Bubbling |
委托Delegate | KeyEventHandler |
- 对应的隧道事件为 PreviewKeyDown 。The corresponding tunneling event is PreviewKeyDown.