WPF中的多键手势

发布于 2024-08-20 02:53:30 字数 196 浏览 3 评论 0原文

我有一个名为 Comment SelectionRoatedUICommand。我需要为此命令添加一个输入手势,就像在 VIsual Studio 中一样,即。 (Ctrl+KCtrl+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).

Regards, Jawahar

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

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

发布评论

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

评论(4

半窗疏影 2024-08-27 02:53:31

我发现这篇博客文章我认为可能会有帮助

http://kent-boogaart.com/ blog/multikeygesture

基本上,WPF 没有内置支持,但子类化 InputGesture 或 KeyGesture 似乎是实现此目的的一种可能方法,无需太多麻烦。

I've found this blog post which I think could be of help

http://kent-boogaart.com/blog/multikeygesture

Basically, WPF has no built in support for it, but subclassing InputGesture or KeyGesture seems like a possible way to achieve this without too much hassle.

青柠芒果 2024-08-27 02:53:31

以下是我如何拼凑出一些实际有效的东西。我只是希望我能赞扬那些为我的启蒙之路铺平道路的人。

假设您的应用程序名为 Heckler。将应用程序的命名空间标记添加到 Window 对象:

<Window ...
    xmlns:w="clr-namespace:Heckler" 
    ...>

现在添加 CommandBindings 属性标记并启动 CommandBinding 对象的集合。这里我们添加自定义命令注释选择

<Window.CommandBindings>
    <CommandBinding
        Command="w:CustomCommands.CommentSelection"
        CanExecute="CommentSelectionCanExecute"
        Executed="CommentSelectionExecuted" />
</Window.CommandBindings>

MenuItem添加到主MenuMenuItem

    <Menu
        IsMainMenu="True">
        <MenuItem
            Header="_File">
            <MenuItem
                Command="w:CustomCommands.CommentSelection">
            </MenuItem>
        </MenuItem>
    </Menu>
    ...
</Window>

在< code>Window 代码隐藏,添加您的 CustomCommands 类和自定义命令:

public static class CustomCommands
{
    // Ctrl+Shift+C to avoid collision with Ctrl+C.
    public static readonly RoutedUICommand CommentSelection = 
        new RoutedUICommand("_Comment Selection", 
            "CommentSelection", typeof(MainWindow), 
            new InputGestureCollection() 
            { new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) });
}

现在连接您的事件处理程序:

private void CommentSelectionCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // Determines status of command.
    e.CanExecute = true;
}

private void CommentSelectionExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // TO-DO: Insert magic here.
}

您应该可以开始了。我希望这有帮助,我没有错过任何东西!

Here's how I cobbled together something that actually works. I just wish I could credit the person or persons who paved the way to my Path of Enlightenment.

Let's say your application is called Heckler. Add a namespace tag for your application to the Window object:

<Window ...
    xmlns:w="clr-namespace:Heckler" 
    ...>

Now add a CommandBindings property tag and start your collection of CommandBinding objects. Here we add custom command Comment Selection:

<Window.CommandBindings>
    <CommandBinding
        Command="w:CustomCommands.CommentSelection"
        CanExecute="CommentSelectionCanExecute"
        Executed="CommentSelectionExecuted" />
</Window.CommandBindings>

Add a MenuItem to a main Menu's MenuItem:

    <Menu
        IsMainMenu="True">
        <MenuItem
            Header="_File">
            <MenuItem
                Command="w:CustomCommands.CommentSelection">
            </MenuItem>
        </MenuItem>
    </Menu>
    ...
</Window>

In the Window code-behind, add your CustomCommands class and custom command:

public static class CustomCommands
{
    // Ctrl+Shift+C to avoid collision with Ctrl+C.
    public static readonly RoutedUICommand CommentSelection = 
        new RoutedUICommand("_Comment Selection", 
            "CommentSelection", typeof(MainWindow), 
            new InputGestureCollection() 
            { new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) });
}

Now wire up your event handlers:

private void CommentSelectionCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // Determines status of command.
    e.CanExecute = true;
}

private void CommentSelectionExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // TO-DO: Insert magic here.
}

You should be good to go. I hope this helps and I didn't miss anything!

柠檬色的秋千 2024-08-27 02:53:31
<KeyBinding Command="{Binding ExitCommand}"

                Key="{Binding ExitCommand.GestureKey}"

                Modifiers="{Binding ExitCommand.GestureModifier}"/>
get

    {

        if (exitCommand == null)

        {

            exitCommand = new DelegateCommand(Exit);

            exitCommand.GestureKey = Key.X;

            exitCommand.GestureModifier = ModifierKeys.Control;

            exitCommand.MouseGesture = MouseAction.LeftDoubleClick;

        }

        return exitCommand;

    }

}
 private void Exit()
{
    Application.Current.Shutdown();
}
<KeyBinding Command="{Binding ExitCommand}"

                Key="{Binding ExitCommand.GestureKey}"

                Modifiers="{Binding ExitCommand.GestureModifier}"/>
get

    {

        if (exitCommand == null)

        {

            exitCommand = new DelegateCommand(Exit);

            exitCommand.GestureKey = Key.X;

            exitCommand.GestureModifier = ModifierKeys.Control;

            exitCommand.MouseGesture = MouseAction.LeftDoubleClick;

        }

        return exitCommand;

    }

}
 private void Exit()
{
    Application.Current.Shutdown();
}
蓦然回首 2024-08-27 02:53:30

此代码是为“Ctrl+W、Ctrl+E”和/或“Ctrl+W、E”组合编写的,但是您可以针对任何组合键对其进行参数化:

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;
        }
    }
}

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