public void Awake()
{
UIDocument uid = GetComponent<UIDocument>();
var rootVE = uid.rootVisualElement;
Button playButton = rootVE.Q<Button>("play");
playButton.RegisterCallback<MouseDownEvent>((e) => Debug.Log("test)"));
}
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?
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:
Derive a class from Button and pass on MouseDownEvent instead of eating it.
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.
Ah, I see - button.clicked is the same as button.RegisterCallback(), 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:
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…!!!
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.:
var button = root.Q<Button>("SomeButton");
button.clickable.activators.Clear();
// Now, if you register a callback for MouseDownEvent, it also works with the left mouse button:
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”.