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. WPF は、添付イベントをルーティングイベントとして実装します。WPF implements attached events as routed events. 添付イベントは、基本的には、イベントを定義していないオブジェクトで処理できるイベントを参照する XAML 言語の概念です。 WPF は、イベントがルートを走査できるようにすることによっても拡張されます。Attached events are fundamentally a XAML language concept for referencing events that can be handled on objects that do not define that event, which WPF expands upon by also enabling the event to traverse a route. アタッチされたイベントには、コードで直接処理構文がありません。コードでルーティングイベントのハンドラーをアタッチするには、指定された Add * Handler メソッドを使用します。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.