实现键盘快捷键

我目前使用onKeyDown事件和if/else语句来创建键盘快捷键:

 if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift && e.Key == Key.Tab) { } else if (e.Key == Key.Tab) { } ... 

但是,如果我有更多的键盘快捷键,这会变得混乱。

有更好的实施吗?

您应该看一下实现

       

你的然后变成:

  

SettingsCanExecute方法确定何时启用该按钮,并在按下按钮或敲击组合键时调用SettingsExecuted方法。

然后,您不需要KeyDown处理程序。

有关Switch On The Code的完整教程 。

有关CommandBindings和InputBindings的更多信息可以在MSDN上找到。

为其他人记录这个答案,因为有一个很简单的方法来做这个很少被引用,并且根本不需要触及XAML。

要链接键盘快捷键,请在Window构造函数中将新的KeyBinding添加到InputBindings集合。 作为命令,传入实现ICommand的任意命令类。 对于execute方法,只需实现您需要的任何逻辑。 在下面的示例中,我的WindowCommand类接受一个委托,它将在每次调用时执行。 当我构造新的WindowCommand以传入我的绑定时,我只是在我的初始化器中指出我希望WindowCommand执行的方法。

您可以使用此模式来提供自己的快捷键盘快捷键。

 public YourWindow() //inside any WPF Window constructor { ... //add this one statement to bind a new keyboard command shortcut InputBindings.Add(new KeyBinding( //add a new key-binding, and pass in your command object instance which contains the Execute method which WPF will execute new WindowCommand(this) { ExecuteDelegate = TogglePause //REPLACE TogglePause with your method delegate }, new KeyGesture(Key.P, ModifierKeys.Control))); ... } 

创建一个简单的WindowCommand类,它接受一个执行委托来触发它上面的任何方法集。

 public class WindowCommand : ICommand { private MainWindow _window; //Set this delegate when you initialize a new object. This is the method the command will execute. You can also change this delegate type if you need to. public Action ExecuteDelegate { get; set; } //You don't have to add a parameter that takes a constructor. I've just added one in case I need access to the window directly. public WindowCommand(MainWindow window) { _window = window; } //always called before executing the command, mine just always returns true public bool CanExecute(object parameter) { return true; //mine always returns true, yours can use a new CanExecute delegate, or add custom logic to this method instead. } public event EventHandler CanExecuteChanged; //i'm not using this, but it's required by the interface //the important method that executes the actual command logic public void Execute(object parameter) { if (ExecuteDelegate != null) //let's make sure the delegate was set { ExecuteDelegate(); } else { throw new InvalidOperationException("ExecuteDelegate has not been set. There is no method to execute for this command."); } } }