是否可以在 WPF 中为一系列按键定义键绑定,例如 Visual Studio 中的快捷方式,例如Ctrl+ R、Ctrl+A在当前解决方案中运行所有测试
据我所见,我只能使用元素绑定单个组合键,例如Ctrl+ 。S我可以使用此绑定序列还是必须手动处理按键才能执行此操作?
是否可以在 WPF 中为一系列按键定义键绑定,例如 Visual Studio 中的快捷方式,例如Ctrl+ R、Ctrl+A在当前解决方案中运行所有测试
据我所见,我只能使用元素绑定单个组合键,例如Ctrl+ 。S我可以使用此绑定序列还是必须手动处理按键才能执行此操作?
InputGesture
您需要通过覆盖该Matches
方法来创建自己的 。
像这样的东西:
public class MultiInputGesture : InputGesture
{
public MultiInputGesture()
{
Gestures = new InputGestureCollection();
}
public InputGestureCollection Gestures { get; private set; }
private int _currentMatchIndex = 0;
public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
{
if (_currentMatchIndex < Gestures.Count)
{
if (Gestures[_currentMatchIndex].Matches(targetElement, inputEventArgs))
{
_currentMatchIndex++;
return (_currentMatchIndex == Gestures.Count);
}
}
_currentMatchIndex = 0;
return false;
}
}
它可能需要更多一点,比如忽略某些事件(例如KeyUp
事件之间的KeyDown
事件不应该重置_currentMatchIndex
),但你明白了......
@ThomasLevesque 的答案大部分是正确的,但不涉及重复键。(请注意,按住 Ctrl 键会导致生成键重复事件。)如果用户在序列中停顿,超时也很有用。这是我正在使用的:
public class MultiKeyInputGesture : InputGesture {
private const int MAX_PAUSE_MILLIS = 1500;
private InputGestureCollection mGestures = new InputGestureCollection();
private DateTime mLastWhen = DateTime.Now;
private int mCheckIdx;
public MultiKeyInputGesture(KeyGesture[] keys) {
Debug.Assert(keys.Length > 0);
// Grab a copy of the array contents.
foreach (KeyGesture kg in keys) {
mGestures.Add(kg);
}
}
public override bool Matches(object targetElement, InputEventArgs inputEventArgs) {
if (!(inputEventArgs is KeyEventArgs)) {
// does this actually happen?
return false;
}
DateTime now = DateTime.Now;
if ((now - mLastWhen).TotalMilliseconds > MAX_PAUSE_MILLIS) {
mCheckIdx = 0;
}
mLastWhen = now;
if (((KeyEventArgs)inputEventArgs).IsRepeat) {
// ignore key-repeat noise (especially from modifiers)
return false;
}
if (!mGestures[mCheckIdx].Matches(null, inputEventArgs)) {
mCheckIdx = 0;
return false;
}
mCheckIdx++;
if (mCheckIdx == mGestures.Count) {
mCheckIdx = 0;
inputEventArgs.Handled = true;
return true;
}
return false;
}
}
我通过RoutedUICommand
在 XAML 中定义来使用它:
<Window.Resources>
<RoutedUICommand x:Key="MyCommand" Text="My Command"/>
</Window.Resources>
这是从<Window.CommandBindings>
和<MenuItem>
照常引用的。然后,在窗口构造函数中,我这样做:
RoutedUICommand ruic = (RoutedUICommand)FindResource("MyCommand");
ruic.InputGestures.Add(
new MultiKeyInputGesture(new KeyGesture[] {
new KeyGesture(Key.H, ModifierKeys.Control, "Ctrl+H"),
new KeyGesture(Key.C, ModifierKeys.Control, "Ctrl+C")
}) );
我发现这个论坛帖子很有帮助。
You will need to add an explicit InputGestureText
to any MenuItem
, unless you want to try the DisplayString
hack in the linked forum post.
NOTE: the key gesture handler "eats" the key that completes the gesture. If you have more than one handler, and the user tries to use two multi-key sequences in a row (e.g. Ctrl+H, Ctrl+C followed immediately by Ctrl+H, Ctrl+D), the second handler won't reset when Ctrl+C is hit. Instead, it'll reset when the second Ctrl+H arrives, and will miss the combo. The actual behavior is dependent upon the order in which the handlers are called. I'm currently handling this by defining a static event that fires when a match is found, and subscribing all instances to it.
Update: one other thing to be aware of: the order of items in <Window.CommandBindings>
matters. If you have a Copy handler that fires on Ctrl+C, it must appear in the list after the multi-key gesture for Ctrl+H, Ctrl+C.
<KeyBinding x:Name="mykeybinding" Gesture="CTRL+P" Key="E"
Command="mycommand"/>
这似乎对我有用,我必须按 ctrl+P+E 才能执行“mycommand”
基于 http://msdn.microsoft.com/en-in/library/system.windows.input.keybinding.aspx