C# WPF编程-事件-键盘事件

时间:2024-03-24 07:48:13

在这里插入图片描述

<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属性提供的方法:
在这里插入图片描述

相关文章