using System.Windows; using System.Windows.Input; namespace WPF路由事件_5_处理按键事件; /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void UIElement_OnPreviewKeyDown(object sender, KeyEventArgs e) { // 忽略键盘长按结果 if ((bool)CheckBox.IsChecked! && e.IsRepeat) return; ListBox.Items.Add($"Event:{e.RoutedEvent} Key: {e.Key}"); } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { ListBox.Items.Clear(); } private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e) { Console.WriteLine(e.Text); } }