Skip to content

WPF: Add support for back and forward mouse buttons #2133

Closed

Description

@merceyz

The WPF browser currently ignores XButton1 and XButton2.
These are used to navigate forward and backwards in Chrome, and should probably do so in CefSharp as well

// Cef currently only supports Left, Middle and Right button presses.
if (e.ChangedButton > MouseButton.Right)
{
return;
}

Activity

  1. added
    wpf
    on Aug 31, 2017
  2. added this to the milestone on Aug 31, 2017
  3. modified the milestones: , 62.0.0 on Dec 13, 2017
  4. frederikstonge commented on Jun 5, 2018

    @frederikstonge

    Could it be possible to disable this feature? Add a boolean to disable these buttons or make the method overriable.

  5. fvanheeswijk commented on Jul 25, 2018

    @fvanheeswijk

    It would be appreciated if this feature could be enabled/disabled at runtime, as most of the times this behaviour is wanted but sometimes it is not.

  6. amaitland commented on Jul 25, 2018

    @amaitland
    Member

    You can already override the behavior, just inherit from ChromiumWebBrowser, override the two methods for mouse up/down and mark them as handled.

  7. fvanheeswijk commented on Jul 26, 2018

    @fvanheeswijk

    @amaitland That should work indeed, thanks :) Though that does mean that I need to duplicate browser.GetHost().SendMouseClickEvent((int)point.X, (int)point.Y, (MouseButtonType)e.ChangedButton, mouseUp, e.ClickCount, modifiers);...

    In short, if you change some things in private void OnMouseButton(MouseButtonEventArgs e) in the future, then I'll be missing those changes, I'm not sure if that is the best way to go, but for now it solves the issue.

  8. amaitland commented on Jul 26, 2018

    @amaitland
    Member

    How about setting the eventargs as handled and call the base method? Shouldn't need to duplicate anything

  9. fvanheeswijk commented on Jul 26, 2018

    @fvanheeswijk

    That means though that private void OnMouseButton(MouseButtonEventArgs e) will not execute anything anymore, right? As the guard is !e.Handled && browser != null. And I just notice that in protected override void OnMouseDown(MouseButtonEventArgs e) I need to duplicate the Focus() call too.

    To clarify: I still want (most) of the original behavior of OnMouseUp and OnMouseDown, I just don't want the back/forward navigation using mouse buttons.

  10. amaitland commented on Jul 26, 2018

    @amaitland
    Member

    To clarify: I still want (most) of the original behavior of OnMouseUp and OnMouseDown, I just don't want the back/forward navigation using mouse buttons.

    That was perfectly clear.

    And I just notice that in protected override void OnMouseDown(MouseButtonEventArgs e) I need to duplicate the Focus() call too.

    For what reason?

    CefSharp is build with GitLink, you can step into the source https://github.com/GitTools/GitLink

  11. fvanheeswijk commented on Jul 26, 2018

    @fvanheeswijk

    I'll add the code of what I have thought of, hopefully you can see where I went wrong with this. I don't intend to use this in production as it is way too hacky. (I also haven't tested this code yet)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xaml;
    using System.Windows.Controls;
    using System.Windows.Input;
    
    using CefSharp;
    using CefSharp.Wpf;
    using CefSharp.Wpf.Internals;
    
    namespace Redacted
    {
        public class CustomChromiumWebBrowser : ChromiumWebBrowser
        {
            public bool IgnoreForwardBackMouseNavigation { get; private set; }
    
            protected override void OnMouseDown(MouseButtonEventArgs e)
            {
                if (IgnoreForwardBackMouseNavigation)
                {
                    Focus();
                    OnMouseButton(e);
    
                    CallGrandBaseMethod("OnMouseDown", e);
                }
                else
                {
                    base.OnMouseDown(e);
                }
            }
    
            protected override void OnMouseUp(MouseButtonEventArgs e)
            {
                if (IgnoreForwardBackMouseNavigation)
                {
                    OnMouseButton(e);
    
                    CallGrandBaseMethod("OnMouseUp", e);
                }
                else
                {
                    base.OnMouseUp(e);
                }
            }
    
            private void OnMouseButton(MouseButtonEventArgs e)
            {
                // implementation copied from https://github.com/cefsharp/CefSharp/blob/cefsharp/63/CefSharp.Wpf/ChromiumWebBrowser.cs#L2123
                // we do not implement the back/forward mouse button functionality (via XButton1/XButton2)
    
                var browser = GetBrowser();
    
                if (!e.Handled && browser != null)
                {
                    var modifiers = e.GetModifiers();
                    var mouseUp = (e.ButtonState == MouseButtonState.Released);
                    var point = e.GetPosition(this);
    
                    browser.GetHost().SendMouseClickEvent((int)point.X, (int)point.Y, (MouseButtonType)e.ChangedButton, mouseUp, e.ClickCount, modifiers);
    
                    e.Handled = true;
                }
            }
    
            private void CallGrandBaseMethod(string methodName, MouseButtonEventArgs e)
            {
                var pointer = typeof(ContentControl).GetMethod(methodName).MethodHandle.GetFunctionPointer();
                var grandBaseMethod = (Action<MouseButtonEventArgs>)Activator.CreateInstance(typeof(Action<MouseButtonEventArgs>, this, pointer);
                grandBaseMethod(e);
            }
        }
    }
    

    I could have gotten it to work in a reasonably nice way if base.base.method() was allowed, but I understand the reasons of why it is not allowed in C#. If private void OnMouseButton(MouseButtonEventArgs e) was protected then it would fix (most) of the issues too in this case.

  12. amaitland commented on Jul 26, 2018

    @amaitland
    Member

    You appear to be overthinking things, try.

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
    	if (e.ChangedButton == MouseButton.XButton1 || e.ChangedButton == MouseButton.XButton1)
    	{
    		e.Handled = true;
    	}	
    
    	base.OnMouseDown(e);
    }
  13. fvanheeswijk commented on Jul 26, 2018

    @fvanheeswijk

    That actually seems to be the solution I was looking for :) Thanks a lot, it is simple and effective.

  14. MelvilQ commented on May 25, 2022

    @MelvilQ

    This is what we had to do to prevent navigation:

    public class CustomChromiumWebBrowser : ChromiumWebBrowser
    {
    	protected override void OnMouseDown(MouseButtonEventArgs e)
    	{
    		if (e.ChangedButton == MouseButton.XButton1 || e.ChangedButton == MouseButton.XButton2)
    		{
    			e.Handled = true;
    		}
    
    		base.OnMouseDown(e);
    	}
    
    	protected override void OnMouseUp(MouseButtonEventArgs e)
    	{
    		if (e.ChangedButton == MouseButton.XButton1 || e.ChangedButton == MouseButton.XButton2)
    		{
    			e.Handled = true;
    		}
    
    		base.OnMouseUp(e);
    	}
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementwpf

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions