2

I've also posted this question to Microsoft's Documentation here.

I am trying to implement both Access Keys and Keyboard Accelerators in a UWP app, which, as I understand it, is "good coding practice". Here is a snippet from MainPage.xaml where I try to implement the "open" shortcut key of "Ctrl+O"

<MenuFlyoutItem Text="Open" AccessKey="O" Click="menuFileOpen">
   <MenuFlyoutItem.KeyboardAccelerators>
        <KeyboardAccelerator Modifiers="Control" Key="O" Invoked="menuFileOpen"/>
   </MenuFlyoutItem.KeyboardAccelerators> 
</MenuFlyoutItem>

Inside MainPage.xaml.cs is the following snippet of code:

    /// <summary>
    /// Menu option to select a file
    /// </summary>
    private void menuFileOpen(object sender, RoutedEventArgs e)
    {
        this.OpenFile();
    }

    /// <summary>
    /// Keyboard Accelerator to select a file
    /// </summary>
    private void menuFileOpen(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
    {
        this.OpenFile();
        args.Handled = true; // because the docs say to do this
    }

    /// <summary>
    /// Opens the dialog box to select a file.
    /// </summary>
    private void OpenFile()
    {
        // TODO: write the code to open the file
        throw new NotImplementedException();
    }

For debugging purposes, I haven't actually implemented the code to open the file, because of the issue I'm having. All that should happen is, when I select Open from the menu, I should get "NotImplementedException" in the debugger, and the program should stop.

In short, selecting the flyout menu item with a mouse throws the exception. Using access keys also throws the exception. Pressing the Keyboard Accelerator, however, does NOTHING! It doesn't enter the "Invoked" method (the second method signature above), so I can't even place a break point to see if the method gets a hit at all...which I'm pretty certain it doesn't!

What I've tried:

  • Renaming the methods to give them unique names
  • Adding the "IsEnabled" property, setting it to true, on the KeyboardAccelerator tag in the XAML file.

Any assistance is very much appreciated.

  • I've also tried removing the "Invoked" method, and hoping it would enter the "Click" method, which is implied with other posts as well as in other places in the documentation, but that also didn't work. – Stanley Jointer II Jul 21 '18 at 12:50
  • I've also tried implementing **ProcessKeyboardAccelerators** on the **MenuFlyoutItem** tag, and adding a method that also throws **NotImplementedException**, but that also did not work. – Stanley Jointer II Jul 21 '18 at 18:47
  • I've also tried adding the **ProcessKeyboardAccelerators** event to the **Page** element. That *DID call* the method and throw the **NotImplementedException**, but the documentation linked on this page doesn't imply that I need to do that. I'll go with this for now, but I'd like the correct solution, rather than this workaround. It seems to me that this workaround isn't correct either, since I will need to have the if-else if structure for all accelerators used in my app. – Stanley Jointer II Jul 21 '18 at 18:55
  • Please check your Visual Studio `Exception Setting` if `Break when this exception type is user-unhandled` option uncheck? – Nico Zhu Jul 24 '18 at 08:49
  • @Nico Zhu: I'm running in the debugger. The exception gets hit when I use Access Keys, or a mouse click. It never gets hit when I try to use the keyboard accelerator that is assigned. – Stanley Jointer II Jul 24 '18 at 13:36
  • 1
    I looked closer at the exposed properties for the "args" parameter in the Keyboard Accelerator version of the method, and, after a couple of minutes of manipulating the accelerator and trying a few different things, I finally made the keyboard accelerator work and call the correct Keyboard Accelerator method, and not the accelerator method associated with the page itself...but the answer is USELESS! In order to get the Keyboard accelerator to work, the submenu with the MenuFlyoutItem needed to be opened, so I could view the actual selection! THAT CANNOT BE CORRECT! – Stanley Jointer II Jul 26 '18 at 01:35
  • @StanleyJointerII I've been [facing a similar issue](https://stackoverflow.com/questions/53735503/keyboard-accelerator-stops-working-in-uwp-app) and it appears that accelerators only work when the element is visible, which is rather useless. I did get a comment from a Microsoft person saying they would report this issue for me. – casablanca Dec 16 '18 at 19:25
  • @casablanca I received an email from Microsoft's support team about a week ago stating that they were able to reproduce the issue and that they are working on it. – Stanley Jointer II Dec 27 '18 at 14:11
  • @StanleyJointerII Hi, I have a related issue, don't want to create a question yet because should be a simple one...I added to a button but is not triggered when I'm inside some control like textbox and passwordbox, do you know why? they shouldn't handle any key enter event. thanks – PandaSharp Mar 18 '19 at 07:11
  • 2
    @EmilianoMagliocca Microsoft said it was a known issue and they are working on it. shortly after I posted the question. – Stanley Jointer II Mar 18 '19 at 15:35
  • 1
    @StanleyJointerII I think I found your issue, looks like we need to wait next month release, hopefully will be fixed https://github.com/MicrosoftDocs/windows-uwp/issues/809 – PandaSharp Mar 19 '19 at 07:48
  • @StanleyJointerII just tried with the latest SDK, still doesn't work, I also commented on your github issue... is your problem fixed? – PandaSharp May 22 '19 at 06:49
  • 1
    I agree that it doesn't work. It doesn't seem to be repaired in VS 2019 either, but I only base that on the fact that it's not in the repair list. Similarly, I didn't see an update status saying that the issue was going to be closed, as well. The only thing I can think of is that it's not a top priority. UWP is designed for cross-platform type development, which means a keyboard accelerator is pointless. Not desirable, since part of the cross-platform I want to do is for Windows 10, but we will need to be patient. – Stanley Jointer II May 24 '19 at 17:04

0 Answers0