다음을 통해 공유


방법: Enter 키를 누르는 시점 감지

이 예제에서는 키보드의 Enter 키를 누르는 시점을 감지하는 방법을 보여 줍니다.

이 예제는 Extensible Application Markup Language (XAML) 파일과 코드 숨김 파일로 구성됩니다.

예제

사용자가 TextBox에서 Enter 키를 누르면 텍스트 상자의 입력이 user interface (UI)의 다른 영역에 나타납니다.

다음 XAML에서는 StackPanel, TextBlockTextBox로 구성된 사용자 인터페이스를 만듭니다.

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

다음 코드 숨김에서는 KeyDown 이벤트 처리기를 만듭니다. 사용자가 누른 키가 Enter 키이면 TextBlock에 메시지가 표시됩니다.

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
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}

참고 항목

개념

입력 개요

라우트된 이벤트 개요