首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现键盘快捷键

实现键盘快捷键
EN

Stack Overflow用户
提问于 2010-08-26 18:56:17
回答 2查看 15.3K关注 0票数 6

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

代码语言:javascript
复制
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift && e.Key == Key.Tab) {

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

} ...

然而,如果我有相当多的键盘快捷键,这就会变得混乱。

有没有更好的实现?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-26 19:01:17

您应该考虑实现和

代码语言:javascript
复制
<Window.CommandBindings>
    <CommandBinding Command="Settings" CanExecute="SettingsCanExecute" Executed="SettingsExecuted" />
</Window.CommandBindings>

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

然后,您的<Button>将变为:

代码语言:javascript
复制
<Button Height="50" Width="50" Margin="50,5,0,0" Command="Settings" />

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

这样就不需要KeyDown处理程序了。

代码上有一个full tutorial on开关。

关于CommandBindingsInputBindings的更多信息可以在MSDN上找到。

票数 17
EN

Stack Overflow用户

发布于 2016-07-27 08:23:21

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

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

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

代码语言:javascript
复制
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类,它接受一个执行委托来触发在它上面设置的任何方法。

代码语言:javascript
复制
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.");
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3574405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档