WPF-Base/WPF路由事件-5-处理按键事件/MainWindow.xaml.cs

33 lines
792 B
C#

using System.Windows;
using System.Windows.Input;
namespace WPF路由事件_5_处理按键事件;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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);
}
}