在 WPF 中使用多个修饰键创建 KeyBinding

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4050066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:15:33  来源:igfitidea点击:

Creating KeyBinding in WPF with more than one modifier key

wpf

提问by Jiew Meng

The way I created KeyBindingwas something like:

我创建的方式KeyBinding是这样的:

<KeyBinding Modifiers="Ctrl" Key="S" Command="{Binding SaveCommand}" />

But what if I needed two modifier keys? For example, Ctrl+ Shift.

但是如果我需要两个修饰键怎么办?例如,Ctrl+ Shift

回答by paxdiablo

The documentation states that you can just separate the modifiers with the +character:

文档指出,您可以将修饰符与+字符分开:

<KeyBinding Modifiers="Ctrl+Shift" Key="S" Command="{Binding SaveCommand}" />

See herefor the gory details, with the relevant bits extracted below in case the link ever disappears:

有关血腥的详细信息,请参阅此处,并在下面提取相关位,以防链接消失:



XAML

XAML

<object property="oneOrMoreModifierKeys"/>

XAML Values

XAML 值

oneOrMoreModifierKeys— One or more modifier keys, defined by the ModifierKeysenumeration, delimited with a +character.

oneOrMoreModifierKeys— 一个或多个修饰键,由ModifierKeys枚举定义,以+字符分隔。



You can also use a gesture on its own rather than a key/modifier combo:

您还可以单独使用手势而不是键/修饰符组合:

<KeyBinding Gesture="Ctrl+Shift+S" Command="{Binding SaveCommand}" />

as per that same documentation link:

根据相同的文档链接:

When defining a KeyBinding in XAML, there are two ways to specify the KeyGesture.

The first way to establish a KeyBinding in XAML is to define the Gesture attribute of the KeyBinding element, which enables a syntax to specify keys and modifiers as a single string, for example "CTRL+P".

The second way is to define the Key attribute and the Modifiers attributes of the KeyBinding element.

Both ways of setting the KeyGesture are equivalent and modify the same underlying object, but there will be a conflict if both are used. In the case when the Key, Modifiers, and the Gesture attributes are all set, the attribute which is defined last will be used for the KeyGesture.

在 XAML 中定义 KeyBinding 时,有两种方法可以指定 KeyGesture。

在 XAML 中建立 KeyBinding 的第一种方法是定义 KeyBinding 元素的 Gesture 属性,这使语法能够将键和修饰符指定为单个字符串,例如“CTRL+P”。

第二种方法是定义 KeyBinding 元素的 Key 属性和 Modifiers 属性。

两种设置KeyGesture的方式是等价的,修改的是同一个底层对象,但是如果同时使用就会有冲突。在 Key、Modifiers 和 Gesture 属性都设置的情况下,最后定义的属性将用于 KeyGesture。

回答by Aurelien Ribon

<KeyBinding Command="{Binding SaveCommand}"
            Gesture="Ctrl+Shift+S" />

See the MSDN documentation, KeyBinding Class.

请参阅 MSDN 文档KeyBinding Class

回答by KornMuffin

I know the question is for XAML, but here's a sample if you want to do it in code (multiple ModifierKeys can be specified via logical OR):

我知道问题是针对 XAML 的,但是如果您想在代码中执行此操作,这里有一个示例(可以通过逻辑 OR 指定多个 ModifierKeys):

new KeyBinding( SaveCommand, Key.S, ModifierKeys.Control | ModifierKeys.Shift )

回答by Terence

Here is my codeto implement multiple character shortcut keys, such as Alt+ P+ Ain WPF MVVM.

这里是我的代码来实现多个字符的快捷键,如Alt+ P+A在WPF MVVM。

Add this to your XAML(attached behavior for the KeyDown event):

将此添加到您的XAML(KeyDown 事件的附加行为):

cb:ShortCutBehavior.Command="{Binding Shortcuts.CmdKeyPressed}"

Add this to your view model:

将此添加到您的视图模型中:

ShortCuts Shortcuts = new ShortCuts( this );

//Add Plenty of shortcuts here until your heart is desired

Shortcuts.AddDoubleLetterShortCut( AddOrganization, Key.P, Key.A, ModifierKeys.Alt, true);
Shortcuts.AddSingleLetterShortCut( CmdAddNewAgreement, Key.A, ModifierKeys.Alt);

These are two examples of adding shortcuts. The first is a double letter shortcut: Alt+ P+ Awhich runs the method AddOrganization() and the second is a single letter shortcut: Alt+ Awhich executes the ICommand CmdAddNewAgreemnt.

这是添加快捷方式的两个示例。第一个是一个双字母快捷方式:Alt+ P+A它运行方法AddOrganization()和第二个是一个单字母简写:Alt+A它执行的ICommand CmdAddNewAgreemnt。

Both AddDoubleLetterShortCut and AddSingleLetterShortCut are overloaded to accept Actions or ICommands.

AddDoubleLetterShortCut 和 AddSingleLetterShortCut 都被重载以接受操作或 ICommand。

This is one of my first attempts at generisizing something, so you can take the idea and make it suitable for you.

这是我第一次尝试将某些东西通用化,因此您可以采用这个想法并使其适合您。

回答by Jitendra Pancholi

It might be too late but here is the simplest and shortest solution.

可能为时已晚,但这是最简单和最短的解决方案。

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S)
    {
         // Call your method here
    }
}

<Window x:Class="Test.MainWindow" KeyDown="Window_KeyDown" >