doomtoo
Topic Author
Posts: 4
Joined: 11 Oct 2019, 18:17

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

21 Jul 2020, 23:37

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!!!!");
    }
    
 
User avatar
sfernandez
Site Admin
Posts: 2995
Joined: 22 Dec 2011, 19:20

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

27 Jul 2020, 20:10

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.

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 25 guests