Page 1 of 1

Unity: Some button mouse events fire, others do not?

Posted: 21 Jul 2020, 23:37
by doomtoo
In Unity, some button events appear to fire(click, doubleclick, enter), but others do not(mousedown, mouseup).
Any reason why?

void Start()
    {
        FrameworkElement root = noesis_view.Content;
        Button btn = (Button)root.FindName("Button_Main");

        //does not fire
        btn.MouseLeftButtonUp += Btn_MouseLeftButtonUp;
        btn.MouseUp += Btn_MouseUp;
        btn.MouseDown += Btn_MouseDown;

        //does fire
        btn.Click += Btn_Click;
        btn.MouseEnter += Btn_MouseEnter;
        btn.MouseDoubleClick += Btn_MouseDoubleClick;
    }
        private void Btn_MouseDown(object sender, MouseButtonEventArgs args)
    {
        Debug.Log("Btn_MouseDown!!!!");
    }

    private void Btn_Click(object sender, RoutedEventArgs args)
    {
        Debug.Log("Btn_Click!!!!");
    }
    

Re: Unity: Some button mouse events fire, others do not?

Posted: 27 Jul 2020, 20:10
by sfernandez
It is because Button class handles some of them to implement the Click logic.

A Button with ClickMode=Release will notify you about the following events:
PreviewMouseLeftButtonDown
PreviewMouseDown
PreviewMouseLeftButtonUp
PreviewMouseUp
Click
Once MouseLeftButtonDown is routed through the Button it marks e.Handled as true and you won't be able receive that event or MouseDown event. The same happens with MouseUp/MouseLeftButtonUp events.