MenuItem HotKey only works after the menu item has been displayed at least once.
See original GitHub issueIt 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:
- Created 4 years ago
- Comments:9 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A workaround is to use KeyBindings on the window, if anyone finds this thread in the future:
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 😃