Si verifica quando viene premuto un tasto mentre l'elemento detiene lo stato attivo.
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS per XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Public Event KeyDown As KeyEventHandler
public event KeyEventHandler KeyDown
public: virtual event KeyEventHandler^ KeyDown { void add (KeyEventHandler^ value); void remove (KeyEventHandler^ value); }
abstract KeyDown : IEvent<KeyEventHandler, KeyEventArgs> override KeyDown : IEvent<KeyEventHandler, KeyEventArgs>
<object KeyDown="KeyEventHandler" .../>
Implementa
IInputElement.KeyDown|
Campo dell'identificatore |
|
|
Strategia di routing |
Bubbling |
|
Delegate |
-
L'evento di tunneling corrispondente è PreviewKeyDown.
-
Eseguire l'override di OnKeyDown per implementare la gestione della classe per questo evento nelle classi derivate.
La gestione dei tasti interagisce con altre funzionalità di piattaforma, ad esempio l'esecuzione di comandi e la composizione di testo. L'evento KeyDown è un evento di input di testo di basso livello che potrebbe non comportarsi come previsto in determinati controlli. Questo si verifica perché in alcuni controlli la composizione dei controlli o la gestione delle classi forniscono una versione di livello superiore della gestione dell'input di testo e degli eventi correlati.
Questo evento crea un alias per l'evento associato Keyboard.KeyDown per questa classe, di modo che KeyDown faccia parte dell'elenco dei membri della classe quando UIElement viene ereditato come elemento di base. I gestori eventi associati all'evento KeyDown sono associati all'evento Keyboard.KeyDown sottostante e ricevono la stessa istanza dei dati dell'evento.
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.
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; } }
.NET Framework
Supportato in: 4, 3.5, 3.0.NET Framework Client Profile
Supportato in: 4, 3.5 SP1Windows 7, Windows Vista SP1 o versione successiva, Windows XP SP3, Windows Server 2008 (componenti di base del server non supportati), Windows Server 2008 R2 (componenti di base del server supportati con SP1 o versione successiva), Windows Server 2003 SP2
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.