public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Key> keys = new List<Key> { Key.B, Key.A, Key.F1 };
foreach (var item in keys)
{
KeyBinding kbd = new KeyBinding() { Key = item };
kbd.Command = Uitl.ShortKeyCommand;
kbd.CommandParameter = item;
this.InputBindings.Add(kbd);
}
var cb = new CommandBinding(Uitl.ShortKeyCommand, ButtonPrev_OnClick);
CommandBindings.Add(cb);
}
private void ButtonPrev_OnClick(object sender, ExecutedRoutedEventArgs e)
{
var param = (Key)e.Parameter;
MessageBox.Show($"你按下了{param}!");
//new Window1().ShowDialog();
}
}
static class Uitl
{
public static RoutedCommand ShortKeyCommand { get; } = new RoutedCommand(nameof(ShortKeyCommand), typeof(Uitl));
}