0

I want to make some shortcuts for my Menu in WPF. I made a static class with RoutedCommands but I can't get it to work. It says that my markup is invalid, but I wrote everything fine. The error I get is: 'None+D6' key and modifier combination is not supported for KeyGesture. I haven't used D6 anywhere in my RoutedCommands.

Relevant XAML

Window x:Class="WorldResources.GlowingEarth"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:cmd="clr-namespace:WorldResources.View"/>
<Window.CommandBindings>
    <CommandBinding Command="cmd:RoutedCommands.NewMap" Executed="NewFile_Click" />
</Window.CommandBindings>
<DockPanel>
    <Menu DockPanel.Dock="Top">
        <Menu.ItemsPanel>
            <ItemsPanelTemplate>
                <DockPanel HorizontalAlignment="Stretch"></DockPanel>
            </ItemsPanelTemplate>
        </Menu.ItemsPanel>
        <MenuItem Header="File">
            <MenuItem Header="New">
                <MenuItem Header="_Map" Command="cmd:RoutedCommands.NewMap" />
        </MenuItem>
    </Menu>
</DockPanel>

And here is my RoutedCommands class

static class RoutedCommands
{
    public static RoutedUICommand NewMap = new RoutedUICommand(
        "New Map",
        "NewMap",
        typeof(RoutedCommands),
        new InputGestureCollection()
        {
            new KeyGesture(Key.M & Key.A, ModifierKeys.Control & ModifierKeys.Shift)
        }
        );
}

What could be the problem?

Riki
  • 342
  • 1
  • 19
  • 1
    Key.M & Key.A evaluates to a new key value – Sir Rufo May 30 '17 at 13:13
  • Are you trying to create a gesture invoked by pressing the M and A keys simultaneously, plus the two modifieers? Or what VS irritatingly calls a "chord", like Ctrl+M *followed by* Shift+M? If it's the former, that's not supported out of the box, but [according to an answer here it's doable](https://stackoverflow.com/questions/2181991/multi-key-gesture-in-wpf). The latter seems like it might involve roping all your key bindings into a bizarro state machine. I bet it'd be fun to write. – 15ee8f99-57ff-4f92-890c-b56153 May 30 '17 at 13:37

1 Answers1

1

Your KeyGesture is invalid. Try this if you want to handle CTRL+Shift + M and CTRL+Shift + A:

static class RoutedCommands
{
    public static RoutedUICommand NewMap = new RoutedUICommand(
        "New Map",
        "NewMap",
        typeof(RoutedCommands),
        new InputGestureCollection()
        {
            new KeyGesture(Key.M, ModifierKeys.Control | ModifierKeys.Shift),
            new KeyGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift)
        }
        );
}
mm8
  • 163,881
  • 10
  • 57
  • 88