UI Button Element click event, distinguish between buttons


I’m detecting clicks on an ui button but I ran into an issue where it detects alla clicks, not only left mouse button. So even when I press the button I use to go ‘Back’ in my browser, this event fires if I’m hovering above the button.

any way to distinguish between them?


  • Björn

Hi @bjorn.syse! Maybe the page below can help you to solve the problems.



I see, so I gather it can be made to listen for regular input events like that. I’ll see if I can use that instead, thanks!

1 Like

The event object that is passed for Element/Button event callback has a property for the key that was pressed: ElementMouseEvent | PlayCanvas API Reference

1 Like

Perfect, thanks!

Follow up reflection: I’ve been using a “shapePicker”-script throughout my app to detect hits via raycast and a bunch of boundingsphere on mouse clicks.

It has worked fine, but just out of your experience - would there be any benefits of using this “UI button in world space” approach instead? I suppose on difference is that I have react to hover events and change the mouse pointer which I suppose I can’t do using my current setup because the raycasts aren’t happening until I actually do a mouse click.

The main difference would be using something that is built int (UI elements) vs maintaining your own picker with potentially custom behaviour.

The control of the shape of the hotspot could be a factor too in deciding between the two

Perhaps there is a performance difference too?

I suppose the shape you mention, the difference would be that with entity picking I could have a 3D shape where as with ui elements only a 2D shape allthough in 3D world space.

It’s not a massive one unless you have hundreds/thousands. Both do some raycasting of some sort. If think the sphere in shape picker is a bit faster as it doesn’t have to handle matrix math calculations.

Ok, good to know. I just figured, since the shapepicker on only raycast on mouse click events, where are in order to do detec hovering the other path must raycast much more often no?

Yes, as you would have to do raycasts on mouse move. UI elements does this too.