实现键盘快捷键

时间:2010-08-26 10:56:17

标签: c# wpf

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

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

} else if (e.Key == Key.Tab) {

} ...

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

有更好的实施吗?

2 个答案:

答案 0 :(得分:17)

您应该考虑实施<CommandBindings><InputBindings>

<Window.CommandBindings>
    <CommandBinding Command="Settings" CanExecute="SettingsCanExecute" Executed="SettingsExecuted" />
</Window.CommandBindings>

<Window.InputBindings>
    <KeyBinding Command="Settings" Key="S" Modifiers="Alt" />
</Window.InputBindings>

您的<Button>随后变为:

<Button Height="50" Width="50" Margin="50,5,0,0" Command="Settings" />

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

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

启用代码时有一个full tutorial

有关CommandBindingsInputBindings的更多信息,请访问MSDN。

答案 1 :(得分:2)

为其他人记录这个答案,因为有一种更简单的方法可以做到这一点很少被引用,并且根本不需要触及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.");
        }
    }
}