Search Unity

RegisterCallback<MouseDownEvent> registers only right mouse button?

Discussion in 'UI Toolkit' started by Oskar_Kasprzak, Aug 13, 2020.

  1. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Hello there.

    I have simple code like this:

    Code (CSharp):
    1. public void Awake()
    2. {
    3.     UIDocument uid = GetComponent<UIDocument>();
    4.     var rootVE = uid.rootVisualElement;
    5.     Button playButton = rootVE.Q<Button>("play");
    6.     playButton.RegisterCallback<MouseDownEvent>((e) => Debug.Log("test)"));
    7. }
    And when I press left mouse button on playButton nothing happens, it works when I press right mouse button. Now if I change MouseDownEvent to MouseUpEvent it works fine with both LMB and RMB... but why it doesn't work with MouseDownEvent?

    Best regards.
     
  2. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    What is happening is the Button class "eats" the MouseDownEvent and doesn't pass it on.

    Some people are suggesting using playButton.clickable.clicked, but to be honest, I tried it and it doesn't work - it gets eaten as well. PointerDownEvent gets eaten too.

    I can think of two ways:

    1) Derive a class from Button and pass on MouseDownEvent instead of eating it.

    2) Use a Label or VisualElement or some other type that works with MouseDownEvent. You might need to re-implement the button-clicking logic if you still need ClickEvent.

    This question seems to be coming up a lot in the forums and as far as I can tell, there's no easy way to force Button to stop eating MouseDownEvent. Button hungry. Button want food. Button angry. All your event are belong to Button.
     
    solarwindstorm and R4vakk like this.
  3. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Instead of playButton.clickable.clicked I just used
    Code (csharp):
    1.  
    2. playButton.clicked += PlayButtonClicked;
    3.  
    Hope this helps you
     
  4. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Ah, I see - button.clicked is the same as button.RegisterCallback<ClickEvent>(), which tells you that the user pressed the mouse over the button, and then sometime later, released the mouse over the same button. I thought you were asking about MouseDownEvent because you wanted to know when the mouse was pressed (which is useful for things like drag and drop).

    If you want to get the actual MouseDownEvent, you can use a VisualElement instead of a button and it works pretty well. Just like with a button, visual elements can respond to a normal click:

    Code (CSharp):
    1. btn.RegisterCallback<ClickEvent>(evt => OnItemClicked(evt, type));
    but with VisualElement you can also get the mouse down since the VisualElement doesn't eat it:

    Code (CSharp):
    1. btn.RegisterCallback<PointerDownEvent>((evt) => m_mouseDown = true);
    Button Nazi say: "No event for YOU".
     
    Last edited: Oct 2, 2020
  5. muthu0914_unity

    muthu0914_unity

    Joined:
    Oct 1, 2020
    Posts:
    5
    Could you please say how to achieve this two ways of getting MouseDownEvent?
    i couldn't get it..
    Edit:
    I get it, need to change element as VisualElement to get the MouseDownEvent. Works fine thanks..!!!
     
    Last edited: Oct 1, 2020
    lclemens likes this.
  6. shmafoo

    shmafoo

    Joined:
    Nov 22, 2016
    Posts:
    24
    Just because I ran into this exact same issue right now: Clearing the activator list of the Clickable property fixes the issue with the left mouse button event getting consumed.

    e.g.:
    Code (CSharp):
    1. var button = root.Q<Button>("SomeButton");
    2. button.clickable.activators.Clear();
    3. // Now, if you register a callback for MouseDownEvent, it also works with the left mouse button:
    4. button.RegisterCallback<MouseDownEvent>(e => SomeCallback(e));
    I don't know why they decided to make it that difficult as there seems to just be no way do this in any other way. At least, I was unable to find any way nor an explanation on why it's designed this way or how it would work "the correct way".

    Hope this helps some people out there.
     
    PM79, Skollid, Alex-Chouls and 2 others like this.
  7. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Works great, thank you for sharing :)
     
    shmafoo likes this.
  8. PM79

    PM79

    Joined:
    Mar 24, 2020
    Posts:
    1

    Works like a Charm. Thanks for sharing.