创建一个键绑定,每次用任何数字按下控件时都会触发命令,然后将该数字作为参数传递?

发布于 2024-08-25 04:10:25 字数 51 浏览 6 评论 0原文

我需要为每个控件+数字组合创建热键,并且不希望创建十个命令。有什么办法可以做到这一点吗?

I need to create hot-keys for every control + number combination and would prefer not to have create ten commands. Is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-09-01 04:10:25

如果我理解你的问题,你有一个命令,比如 MyCommand,并且你想在用户按 CTRL+0 到 CTRL+9 时触发它,并为每个组合给命令一个不同的参数。

在这种情况下,只需在窗口中创建 10 个键绑定,全部绑定到 MyCommand,并给它们一个参数:

<Window.InputBindings>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+0" CommandParameter="0"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+1" CommandParameter="1"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+2" CommandParameter="2"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+3" CommandParameter="3"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+4" CommandParameter="4"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+5" CommandParameter="5"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+6" CommandParameter="6"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+7" CommandParameter="7"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+8" CommandParameter="8"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+9" CommandParameter="9"/>
</Window.InputBindings>

If I understand your question, you have a single command, say MyCommand, and you want to fire it if the user presses CTRL+0 through CTRL+9, and give the command a different parameter for each combination.

In that case, just create 10 key bindings in your window, all bound to MyCommand, and give them a parameter:

<Window.InputBindings>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+0" CommandParameter="0"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+1" CommandParameter="1"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+2" CommandParameter="2"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+3" CommandParameter="3"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+4" CommandParameter="4"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+5" CommandParameter="5"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+6" CommandParameter="6"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+7" CommandParameter="7"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+8" CommandParameter="8"/>
    <KeyBinding Command="MyCommand" Gesture="Ctrl+9" CommandParameter="9"/>
</Window.InputBindings>
我喜欢麦丽素 2024-09-01 04:10:25

是的,您可以创建一个自定义 KeyBinding 来执行此操作。代码看起来像这样:

[ContentProperty("Keys")]
public class MultiKeyBinding : InputBinding
{
  public ModifierKeys Modifiers;
  public List<Key> Keys = new List<Key>();

  private Gesture _gesture;

  public override InputGesture Gesture
  {
    get
    {
      if(_gesture==null) _gesture = new MultiKeyGesture { Parent = this };
      return _gesture;
    }
    set { throw new InvalidOperationException(); }
  }

  class MultiKeyGesture : InputGesture
  {
    MultiKeyBinding Parent;

    public override bool Matches(object target, InputEventArgs e)
    {
      bool match =
        e is KeyEventArgs &&
        Parent.Modifiers == Keyboard.Modifiers &&
        Parent.Keys.Contains( ((KeyEventArgs)e).Key );

      // Pass actual key as CommandParameter
      if(match) Parent.CommandParameter = ((KeyEventArgs)e).Key;

      return match;
    }
  }
}

它将像这样使用:

<local:MultiKeyBinding Command="..." Modifiers="Control">
  <Key>D0</Key>
  <Key>D1</Key>
  ...
</local:MultiKeyBinding>

希望这会有所帮助。

Yes, you can create a custom KeyBinding that does this. The code would look something like this:

[ContentProperty("Keys")]
public class MultiKeyBinding : InputBinding
{
  public ModifierKeys Modifiers;
  public List<Key> Keys = new List<Key>();

  private Gesture _gesture;

  public override InputGesture Gesture
  {
    get
    {
      if(_gesture==null) _gesture = new MultiKeyGesture { Parent = this };
      return _gesture;
    }
    set { throw new InvalidOperationException(); }
  }

  class MultiKeyGesture : InputGesture
  {
    MultiKeyBinding Parent;

    public override bool Matches(object target, InputEventArgs e)
    {
      bool match =
        e is KeyEventArgs &&
        Parent.Modifiers == Keyboard.Modifiers &&
        Parent.Keys.Contains( ((KeyEventArgs)e).Key );

      // Pass actual key as CommandParameter
      if(match) Parent.CommandParameter = ((KeyEventArgs)e).Key;

      return match;
    }
  }
}

It would be used like this:

<local:MultiKeyBinding Command="..." Modifiers="Control">
  <Key>D0</Key>
  <Key>D1</Key>
  ...
</local:MultiKeyBinding>

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文