WPF中的多键手势 [英] multi key gesture in wpf
本文介绍了WPF中的多键手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为Comment Selection
的RoutedUICommand
.我需要为此命令添加输入手势,就像在VIsual Studio中一样. ( Ctrl + K , Ctrl + C ).
我怎样才能做到这一点?请帮我. (请记住VS功能).
I have a RoutedUICommand
called Comment Selection
. I need to add an input gesture for this command as it is in VIsual Studio, ie. (Ctrl+K, Ctrl+C).
How can I do this? Plz help me. (Keep VS functionality in mind).
问候,贾瓦哈尔
推荐答案
此代码是针对"Ctrl + W,Ctrl + E"和/或"Ctrl + W,E"组合制作的,但是您可以对任何参数进行参数化组合键:
This code is made for "Ctrl+W, Ctrl+E" and/or "Ctrl+W, E" combinations, however you can parametrize it for any key combinations:
XAML:
<MenuItem Header="Header" InputGestureText="Ctrl+W, E" Command="ShowCommand"/>
C#:
public static readonly RoutedUICommand ShowCommand = new RoutedUICommand(
"Show command text",
"Show command desc",
typeof(ThisWindow),
new InputGestureCollection(new[] { new ShowCommandGesture (Key.E) }));
public class ShowCommandGesture : InputGesture
{
private readonly Key _key;
private bool _gotFirstGesture;
private readonly InputGesture _ctrlWGesture = new KeyGesture(Key.W, ModifierKeys.Control);
public ShowCommandGesture(Key key)
{
_key = key;
}
public override bool Matches(object obj, InputEventArgs inputEventArgs)
{
KeyEventArgs keyArgs = inputEventArgs as KeyEventArgs;
if (keyArgs == null || keyArgs.IsRepeat)
return false;
if (_gotFirstGesture)
{
_gotFirstGesture = false;
if (keyArgs.Key == _key)
{
inputEventArgs.Handled = true;
}
return keyArgs.Key == _key;
}
else
{
_gotFirstGesture = _ctrlWGesture.Matches(null, inputEventArgs);
if (_gotFirstGesture)
{
inputEventArgs.Handled = true;
}
return false;
}
}
}
这篇关于WPF中的多键手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文