Specifies the possible key values on a keyboard.
<TypeConverterAttribute(GetType(KeyConverter))> _ Public Enumeration Key
Dim instance As Key
[TypeConverterAttribute(typeof(KeyConverter))] public enum Key
[TypeConverterAttribute(typeof(KeyConverter))] public enum class Key
public enum Key
<object property="enumerationMemberName" .../>
Information pertaining to key input can be obtained in several different ways in WPF. Key-related events, such as KeyDown and KeyUp, provide key state information through the KeyEventArgs object that is passed to the event handler. Key state information can also be obtained through the static methods on the Keyboard class, such as IsKeyUp and GetKeyStates. The Keyboard class reports the current state of the keyboard.
This example shows how to detect when the Enter key is pressed on the keyboard.
This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file. For the complete samples, see Keyboard Key Sample.
When the user presses the Enter key in the TextBox, the input in the text box appears in another area of the user interface (UI).
The following XAML creates the user interface, which consists of a StackPanel, a TextBlock, and a TextBox.
<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>
The following code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a message is displayed in the 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; } }
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003