Making a hold button

Hi everyone, I’m looking to make a function that trigger an action if the button is holding down. I’m making the eventlistener like this:

rightButton.addEventListener(‘mousedown’, function(event)
{
setTimeout(function()
{
}, 500);
});

But it came out error message Uncaught TypeError: rightButton.addEventListener is not a function
at HTMLImageElement.. The rightbutton is a button from Babylon GUI. How can I make a hold button event trigger ?

Try this instead:

(rightButton as HTMLElement).addEventListener

It is related to the type definition in typescript. HTMLImageElement type doesn’t have addEventListener function exposed. So cast it to the parent type HTMLElement.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement

How to make a hold event trigger if I want to use button from babylon GUI instead of HTML?

you could probably attach an event onPointerDown and start a timer . Kill the timer in the pointer up function so it would only trigger if you press long enough.

A playground would be great.

1 Like