4

I'm trying to add a keyboard accelerator to a CommandBar menu item in a UWP app. This works fine when the app starts up, but after I open the overflow menu for the first time, the accelerator stops working. This doesn't appear to happen with primary commands (outside the menu), only secondary commands inside the overflow menu. Also, the menu item still works fine when clicked.

XAML:

<CommandBar>
    <CommandBar.SecondaryCommands>
        <AppBarButton Label="Test" Click="AppBarButton_Click">
            <AppBarButton.KeyboardAccelerators>
                <KeyboardAccelerator Key="A" Modifiers="Control" />
            </AppBarButton.KeyboardAccelerators>
        </AppBarButton>
    </CommandBar.SecondaryCommands>
</CommandBar>

Code behind:

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("test clicked");
}

Am I missing something here or is this a bug in UWP?

Edit: I did some more testing and I'm fairly convinced that this is either a design flaw or a bug. If I make the button a primary command, the accelerator works while the button is visible, but if I resize the window so that it moves to the overflow menu, the accelerator stops working.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • @NicoZhu-MSFT I already read the MSDN documentation, and it says "If the event isn't handled, the XAML framework looks for other unscoped app accelerators outside of the bubbling path". My example isn't using a scoped accelerator, shouldn't it be always active? I'm not looking for a system-global hotkey, just within my app's window. – casablanca Dec 12 '18 at 08:17
  • Yep, you are right, I will report this issue to related team. – Nico Zhu Dec 12 '18 at 08:43
  • 1
    Looks like this issue was reported earlier this year in [another SO question](https://stackoverflow.com/questions/51456230/cannot-get-keyboard-accelerators-to-work-at-all). @NicoZhu-MSFT is there a public issue tracker for UWP that we can follow to get updates? – casablanca Dec 16 '18 at 19:29
  • 1
    Hi, I also work on the MSFT UWP support team. This is a bug and we will get a bug filed, however there is no public tracking. You can also file the issue yourself using the Visual Studio Help > Send Feedback > Report a Problem menu which will generate a public issue you can track. And the issue does seem to be that the Keyboard Accelerator is only working when the control is visible. – jgoldberger - MSFT Dec 17 '18 at 22:23
  • HI again, actually filing this issue using the Feedback Hub app on Windows 10 is a better option for UWP issues. – jgoldberger - MSFT Dec 17 '18 at 22:40
  • Hi @jgoldberger-MSFT and case owner I have escalated this issue. Please pay attention to the follow-up update. – Nico Zhu Dec 18 '18 at 09:59
  • @casablanca, we have fixed this issue in latest insider preview. please try it. – Nico Zhu Dec 27 '18 at 07:51
  • @NicoZhu-MSFT Thanks for the update. Any chance this will be hotfixed to older releases, seeing that the API was introduced in FCU? If not, is there a shim that apps can use to work around this issue until the next release is widely adopted by users? – casablanca Dec 29 '18 at 03:28
  • 1
    Any update on when this bug fix is going to be released? – b.pell Mar 23 '19 at 21:19

1 Answers1

1

Since this bug has not been addressed, the workaround exists.

Create no-space buttons (they has to be visible) with same click event and same keyboard accelerator. You don't need to be afraid about double firing. It will fire just the first one.

<Grid Height="0" Width="0" Opacity="0">
 <Button IsTabStop="False"  Click="AppBarButton_Click">
     <Button.KeyboardAccelerators>
        <KeyboardAccelerator Key="A" Modifiers="Control" />
     </Button.KeyboardAccelerators>
 </Button>
</Grid>


<CommandBar>
    <CommandBar.SecondaryCommands>
        <AppBarButton Label="Test" Click="AppBarButton_Click">
            <AppBarButton.KeyboardAccelerators>
                <KeyboardAccelerator Key="A" Modifiers="Control" />
            </AppBarButton.KeyboardAccelerators>
        </AppBarButton>
    </CommandBar.SecondaryCommands>
</CommandBar>
Alamakanambra
  • 5,845
  • 3
  • 36
  • 43