<Grid Button.Click="Grid_Click" KeyDown="Grid_KeyDown">
<StackPanel>
<Button Name="btn1" Width="20" Height="30" Margin="10" Click="btn1_Click">OK</Button>
<TextBox TextInput="TextBox_TextInput"></TextBox>
</StackPanel>
</Grid>
private void Grid_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine($"{e.Key.ToString()}");
}
private void TextBox_TextInput(object sender, TextCompositionEventArgs e)
{
Console.WriteLine($"{e.Text}");
}
KeyboardDevice属性提供的方法: