简体   繁体   中英

How do I capture mouse “back” button and cause it to do something else?

I have a Windows 8.1 Universal App I am working on and all of the pages are "inside" a root Frame object.

The landing page is a Home page and the user can select from 5 different pages to go to. When the user goes to one of those pages, the page has a "home" button on it to take them back to the Home page.

I recently discovered that if a mouse has a back button on it, it will go back to the last page visited. This method of going back to the Home page bypasses some logic that is necessary for the app.

Is it possible to "disable" any action of those forward and back buttons on the mouse? If it is not possible, can I capture that event and redirect it to the method that has the necessary logic?

Thanks,
Zach

I think you are looking for the concept of navigation service.

And this MSDN Article should help you to manage your backward navigation.

VK_XBUTTON1 and VK_XBUTTON2. Those are the constants for the additional mouse buttons, that are often assigned to forward and backward navigation.

If you want to handle the X button messages immediately, they are posted to your application using WM_XBUTTONDOWN and WM_XBUTTONUP.

Check here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731.aspx

Edit: I just noticed that this question was about a "Universal app", this example is for WinForms but it may be helpful for finding the answer for Universal or UWP apps...

Assuming you already have two event handlers on buttons to navigate backwards ( void NavigateBack(object sender, EventArgs e) ) and forwards ( void NavigateForward(object sender, EventArgs e) )

First add this snippet to your Form code:

private void HandlePreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.XButton1:
            NavigateBack(sender, e); // call the back button event handler
            break;
        case Keys.XButton2:
            NavigateForward(sender, e); // call the forward button event handler
            break;
    }
}

private void HandleMouseDown(object sender, MouseEventArgs e)
{
    switch (e.Button)
    {
        case MouseButtons.XButton1:
            NavigateBack(sender, e); // call the back button event handler
            break;
        case MouseButtons.XButton2:
            NavigateForward(sender, e); // call the forward button event handler
            break;
    }
}

Then go to the designer view, go through all the major controls on your form and hookup the PreviewKeyDown and MouseDown events to the corresponding methods.

A better (future proof) way would be to write code to recursively hookup the events like this:

private void HookupNavigationButtons(Control ctrl)
{
    for (int t = ctrl.Controls.Count - 1; t >= 0; t--)
    {
        Control c = ctrl.Controls[t];
        c.PreviewKeyDown -= HandlePreviewKeyDown;
        c.PreviewKeyDown += HandlePreviewKeyDown;
        c.MouseDown -= HandleMouseDown;
        c.MouseDown += HandleMouseDown;
        HookupNavigationButtons(c);
    }
}

And call that method somewhere after InitializeComponent(); with HookupNavigationButtons(this);

If you only want the mouse events you can leave out the keyboard stuff, but there are a few keyboards out there that also have these navigation buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM