locked
Back Button not being handled? RRS feed

  • Question

  • If you create default a Metro Grid application, run it, click on one of the items, then you go to a different page.  At this point you would expect that you can press the back button on your keyboard (or device) to go back.  Unfortunately nothing happens when you press the back button on your keyboard. Why is this not handled by default, and what needs to be done to handle it in a generic way?

    ...Stefan

    Saturday, May 5, 2012 5:49 AM

Answers

All replies

  • Hi,

    Yes, the back button on keyboard is not associated to the back button on the UI. Actually the back button on the UI is a normal button. It is the template creates a button, attach its Click event handler to the GoBack method defined in the LayoutAwarePage (also generated by the project template, not a built-in feature), and so when we click this button, we will go back to the previous page. If you want to add keyboard support based on the template, I would like to suggest you to modify the LayoutAwarePage.

    In constructor, add:

                Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;

    And in the event handler, invoke the generated GoBack method:

            void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
            {
                if (args.VirtualKey == Windows.System.VirtualKey.Back)
                {
                    this.GoBack(sender, new RoutedEventArgs());
                }
            }

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Monday, May 7, 2012 8:40 AM
    Moderator
  • Since you are attaching to a static event, remember to unhook from this even in the OnNavigatingFrom method, or you'll have this page leaking memory, as well as reacting to back presses when it's not loaded any longer.
    Monday, May 7, 2012 2:50 PM
  • @Ming,

    Thanks, I'm just not clear why the frame itself doesn't handle these things.  From an end user perspective is truly awful to run a Metro application that doesn't support the back button.  I'm hopeful that some of the new tablets will have a back button, rather than just the start button. The back button on my phone gets used a lot more than the start button does.  Not every application developer is going to realise that they need to  write code to handle the back key.

    Anyway, thanks for the suggestion, but unfortunately that function never gets called when you press the back key. it does get called if other keys are pressed.

    ...Stefan

    Monday, May 7, 2012 11:39 PM
  • Hi,

      >> but unfortunately that function never gets called when you press the back key. it does get called if other keys are pressed.

    It would be better if you can provide more details on what’s not working. You can also download the SimpleBackButton demo from https://skydrive.live.com/#cid=4722D155FB172DBB&id=4722D155FB172DBB%21631. It follows the same steps as pointed out in my previous reply. If you hit the back key on the keyboard, the app will navigate back.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Tuesday, May 8, 2012 7:23 AM
    Moderator
  • Hi Ming,

    Thanks for the sample.  Unfortunately the example uses the backspace key, not the Back button.  The backspace key is not something I would expect to be supported by default, because different applications can do different things with that particular key. However I would expect the back and forward buttons to work automatically with the frame. On my keyboard there are back and forward buttons.  Unfortunately in the current build there appears to be no virtual key for those keys, thus they can't be captured. This is a huge problem and it is absolutely CRUCIAL this is fixed before release.  As I say, I think the obvious solution is that it is handled automatically by the frame, but at the very least there needs to be virtual keys for the forward and back buttons.

    ...Stefan

    Tuesday, May 8, 2012 6:50 PM
  • Hi,

      >> On my keyboard there are back and forward buttons. 

    Are you using a special keyboard or do you mean the left and right key? From my experience, a standard keyboard doesn’t have the back/forward buttons that you mentioned.

      >> I think the obvious solution is that it is handled automatically by the frame, but at the very least there needs to be virtual keys for the forward and back buttons.

    If it is a hardware specific feature, we cannot expect a general project template to covert it.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Wednesday, May 9, 2012 9:06 AM
    Moderator
  • I'm using a Microsoft keyboard that has back and forward buttons on it. 

    As I say the point is one of accessibility, it's important for people who struggle to use the mouse to be able use keyboard commands like this. It should be a default part of the framework (not part of a template), just as it is in WPF.

    And again, we need to be able to get notification when those buttons are pressed.
    Wednesday, May 9, 2012 9:52 AM
  • As ming pointed out, since it is not a standard keyboard, it is up to the hardware to support the features you are talking about. Usually hardware driver sends a standard key code to the OS, and the OS translates the key to a virtual key that is understandable by most application frameworks. For example, you may try XButton1 and XButton2. Those two virtual keys are usually used by hardware extensions. Some hardware buttons may also send combination of keys. For example, Ctrl + C. To find the exact key code, you may need to consult the hardware menu.

    Thursday, May 10, 2012 3:15 AM
  • @Name-Dis,

    Thanks for your suggestion, unfortunately the key never gets through to the KeyDown handler because there is no virtual key declared for it. In WPF there was a series of BrowserKeys the relevant ones here are Key.BrowserBack and Key.BrowserForward.  The first problem here is that there is no way to capture these keys.

    Then the second problem is the fact that by default they are not captured.  In WPF doing something like this was easy, looked like this:
    <Window.InputBindings>
            <KeyBinding Command="NavigationCommands.BrowseBack" Key="BrowserBack"/>
            <KeyBinding Command="NavigationCommands.BrowseForward" Key="BrowserForward"/>
    </Window.InputBindings>

    And the infrastructure did all the work.  Unfortunately there is no commanding infrastructure whatsoever in WinRT and it's a weakness like that that causes the sort of problems.  But as Ming indicated it could be worked around by creating a KeyDown handler, except for the fact that the key is not passed on, which seriously worries me in that I now unsure how many other keys I'm not going to be able to handle in the future because WinRT hasn't declared them?

    ...Stefan

    Thursday, May 10, 2012 3:52 AM
  • Hi Stefan,

    I have notified the product group of your request.  Thanks for all your feedback everyone!

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 10, 2012 11:55 AM
    Moderator
  • unfortunately the key never gets through to the KeyDown handler because there is no virtual key declared for it.

    Hi Stefan,

    You must be doing something wrong in your code. I just confirmed that the browser back and browser forward buttons both get delivered to CoreWindow.KeyDown and Dispatcher.AcceleratorKeyActivated in my test app. The browser buttons aren't listed in Windows.System.VirtualKey, but you can find the values for VK_BROWSER_BACK (0xA6) and VK_BROWSER_FORWARD (0xA7) in winuser.h

    const int VK_BROWSER_BACK = 0xA6;
    const int VK_BROWSER_FORWARD = 0xA7;
    Window.Current.CoreWindow.KeyUp += (sender, args) =>
    {
        string s = "KeyUp: " + args.VirtualKey.ToString();
        if ((int) args.VirtualKey == VK_BROWSER_BACK) s += " VK_BROWSER_BACK";
        if ((int) args.VirtualKey == VK_BROWSER_FORWARD) s += " VK_BROWSER_BACK";
        Debug.WriteLine(s);
    };
    Window.Current.CoreWindow.KeyDown += (sender, args) =>
    {
        string s = "KeyDown: " + args.VirtualKey.ToString();
        if ((int)args.VirtualKey == VK_BROWSER_BACK) s += " VK_BROWSER_BACK";
        if ((int)args.VirtualKey == VK_BROWSER_FORWARD) s += " VK_BROWSER_BACK";
        Debug.WriteLine(s);
    };
    Dispatcher.AcceleratorKeyActivated += (sender, args) =>
    {
        string s = "AcceleratorKeyActivated: " + args.VirtualKey.ToString();
        if ((int)args.VirtualKey == VK_BROWSER_BACK) s += " VK_BROWSER_BACK";
        if ((int)args.VirtualKey == VK_BROWSER_FORWARD) s += " VK_BROWSER_BACK";
        Debug.WriteLine(s);
    };

    If you need more help with capturing these keys then please start a separate thread specifically on that topic.

    --Rob


    Thursday, May 10, 2012 5:56 PM
    Moderator
  • This is fixed in RP. Thanks.
    Wednesday, June 6, 2012 2:54 AM