Making a button repeat its action as long as it's held down

I want a toolbar button that acts as a fast forward button, calling a function every X milliseconds as long as the button is pressed. I tried the obvious way: set a “playbackSpeed” variable on mouse down, zero it on mouse up. But seems buttons eat their MouseDownEvents so this failed:

The button:

var buttonNext = root.Q<ToolbarButton>("button-next");

Mouse up event is fired:

buttonNext.RegisterCallback<MouseUpEvent>((evt) =>
{
    Debug.Log("I'm called");
});

Mouse down, never:

buttonNext.RegisterCallback<MouseDownEvent>((evt) =>
{
    Debug.Log("I'm never called");
});

Then I got wind of something called “Clickable”, which has a delay and interval on its constructor. It’s part or something called “Manipulators”, that isn’t anywhere in the documentation (as usual). After fumbling through the reference source code, I still can’t figure out how to get them to work (if they even do):

I tried this:

buttonNext.clickable = new Clickable(
    () => {
        Debug.Log("I'm never called either");
    }, 0, 20);

And this:

var clickableMess = new Clickable(
    () => {
        Debug.Log("I too, will never be called");
    },
    0, 20);
clickableMess.activators.Add(new ManipulatorActivationFilter() { button = MouseButton.LeftMouse, clickCount = 1 });
buttonNext.AddManipulator(clickableMess);

Is this even possible at all, or should I begin rolling my own buttons from scratch too?

Hi KokkuHub,

Could you give more context as to where that button is being used? Its events might be consumed by another element upwards with a RegisterCallback(…, TrickleDown.TrickleDown) for example?

I’ve tested the following, and it seems to work fine on my side:

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

public class TestRepeatButtonWindow : EditorWindow
{
    [MenuItem("Window/TestRepeatButtonWindow")]
    static void Init()
    {
        GetWindow(typeof(TestRepeatButtonWindow)).Show();
    }

    void OnEnable()
    {
        var button = new ToolbarButton();
        button.clickable = new Clickable(() => Debug.Log("Clicked"), 0, 20);
        rootVisualElement.Add(button);
    }
}

I’m on the most recent UIToolkit package and Unity 2020.2 alpha (not published versions though, but I could retest it on a published version if you can’t get my code to work).

Doesn’t work for me, I tried adding the button both to the root element and to the toolbar, in neither the message is printed. I’m on 2019.2.21 using the core UIElements, so that might be why.