question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MenuItem HotKey only works after the menu item has been displayed at least once.

See original GitHub issue

It looks like this is the same issue which was fixed in #290. If the HotKey property of a MenuItem is set, it will not work until the user has opened the menu which contains that MenuItem.

To reproduce the bug simply add the following changes to the Menu sample in the ControlCatalog. In MenuItemViewModel.cs

    public class MenuItemViewModel
    {
        public string Header { get; set; }
        public ICommand Command { get; set; }
        public object CommandParameter { get; set; }
        public IList<MenuItemViewModel> Items { get; set; }
        // Add the Gesture property.
        public KeyGesture Gesture { get; set; }
    }

In MenuPage.xaml:

                <TextBlock Classes="h3" Margin="4 8">Dyanamically generated</TextBlock>
                <Menu Items="{Binding MenuItems}">
                    <Menu.Styles>
                        <Style Selector="MenuItem">
                            <Setter Property="Header" Value="{Binding Header}"/>
                            <Setter Property="Items" Value="{Binding Items}"/>
                            <Setter Property="Command" Value="{Binding Command}"/>
                            <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/>
                            <!-- Add a binding for the HotKey property -->
                            <Setter Property="HotKey" Value="{Binding Gesture}"/>
                        </Style>
                    </Menu.Styles>
                </Menu>

In MenuPageViewModel.cs

                    Items = new[]
                    {
                        new MenuItemViewModel { Header = "_Open...", Command = OpenCommand },
                        // Assign a gesture to the Save command.
                        new MenuItemViewModel
                        {
                            Header = "Save",
                            Command = SaveCommand,
                            Gesture = new Avalonia.Input.KeyGesture(Avalonia.Input.Key.S, Avalonia.Input.InputModifiers.Control)
                        },
                        new MenuItemViewModel { Header = "-" },
                        // The rest of the items....

With these changes, run the ControlCatalog.Desktop project, and go to the Menu page. Press Ctrl+S and notice that nothing will be printed in the output. Open the menu which contains the Save command, and press Ctrl+S again. This time it will print Save in the output.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
Raymonfcommented, Nov 24, 2022

A workaround is to use KeyBindings on the window, if anyone finds this thread in the future:

<Window.KeyBindings>
    <KeyBinding Gesture="Ctrl+O" Command="{Binding OpenCommand}" />
</Window.KeyBindings>
1reaction
robloocommented, Sep 14, 2019

Instead of InputBindings in might be good to look at the ‘updated’ concept of KeyboardAccelerator from UWP.

https://docs.microsoft.com/en-us/windows/uwp/design/input/keyboard-accelerators https://docs.microsoft.com/en-us/windows/uwp/design/input/keyboard-interactions

Oh, and then there is gestures for special touch input 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

My key shortcut isn't working
I am tryiong to add a shortcut to my menu Item but It doesn't seams to work. here is my menu item: <MenuItem...
Read more >
MenuItem scripting | Apple Developer Forums
I've been trying to create an applescript to go through every menuitem and list out the keyboard shortcuts. I've created a script to...
Read more >
Stop MenuItem from consuming keyboard input event
One big problem for me however is, that the menu item consumes the keyboard input ... most use key combinations (CTRL + key)...
Read more >
Adding keyboard shortcuts to menu items created by script. ...
I wanted to create a custom menu by script and assign (on the numeric keyboard) shortcuts to the menu items so that I...
Read more >
Unity - Scripting API: MenuItem
The MenuItem attribute turns any static function into a menu command. Only static functions can use the MenuItem attribute. To create a hotkey, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found