Detect simultaneously pressed mousebuttons

Hi community,

I’m having problems implementing the steering of the player in my current project.
Left mouse button should trigger firing, right mouse button should trigger acceleration.
It already works to a certain degree, but I’m struggling to implement the behavior that when the right mouse button (acceleration) is held down it still is possible to trigger fire with left mouse button without stopping acceleration. Here is a playground which only implements non-simultaneously pressed buttons https://playground.babylonjs.com/#T4MJ6I#3. The left box indicates firing with CCW rotation, the right box indicates acceleration with CW rotation.

According to MouseEvent.buttons - Web APIs | MDN the event.buttons property indicates simultaneously pressed buttons which probably can be used to aceive what I’m looking for. But all attempts messing around with this property failed. Maybe someone already has done something similar in the past and could modify the playground.

Any help appreciated!

Hi holger,

Does this do what you’re looking for? The only changes I made were to remove the state reset on controlEnabled and switch the setting of your acceleration flags to depend only on the buttons variable, which uses bit flags to indicate which mouse buttons are currently pressed. I think it works pretty well, though if there were special cases you were guarding against with some of those conditions I removed, you should double-check that those cases aren’t broken now. :upside_down_face:

1 Like

That’s exactly how it should work :smiley: :smiley: Thank you very much for your effort, much appreciated!
The bit flags did the trick.

Hi community,
it seems something has changed regarding the detection of simultaneously pressed mousebuttons since 4.2.0. With latest 5.0 it doesn’t work anymore. Maybe someone can have a look, see this PG https://playground.babylonjs.com/#T4MJ6I#5

Any help apprechiated :slight_smile:

@PolygonalSun will hopefully be able to help!

Lemme take a look at this and see what’s going on

So here’s an update with this. Based on what I’m seeing, it looks like the current system is registering the simultaneous input but not properly triggering the proper observer. I have noticed that when you hold both left and right click down and then move the mouse, both squares will rotate. I’m going to continue to figure out why each isn’t firing properly and will update this thread when I having things figure out and have a fix in the works.

Thanks for investigating :+1:

Hi @PolygonalSun any news on this :slightly_smiling_face:

Hey @holger sorry about the delay in response. I’ve created an Issue for this: Simultaneous button presses not working properly · Issue #10087 · BabylonJS/Babylon.js (github.com) and I’m going to work to fix it this week.

1 Like

Ok looking into it, so far the previous version was working but not correctly as we were receiving a move event and not up/down for the second button.

Let me see what we can do.

Should be fixed by Fix Simultaneous button click by sebavan · Pull Request #10098 · BabylonJS/Babylon.js · GitHub

Thank you @sebavan and @PolygonalSun for investigating and fixing :slight_smile:

1 Like

Hi @PolygonalSun and @sebavan, It seems I neeed your help again :slight_smile: The PG metioned above https://playground.babylonjs.com/#T4MJ6I#5 stopped working again with latest 5.0. An error is raised when clicking left/right mousebuttons

babylon.js:16 Uncaught DOMException: Failed to execute 'setPointerCapture' on 'Element': InvalidStateError
    at HTMLCanvasElement._pointerDownEvent (https://preview.babylonjs.com/babylon.js:16:3669360)
_pointerDownEvent @ babylon.js:16

Any help appreciated :slight_smile:

Recently, I pushed some changes to set pointer capture for input but I’m not sure why it’s throwing an InvalidStateError. I’ll try to look at it this week and see what’s happening.

3 Likes

Would be great as it might be a regression and I am afraid of unforeseen impact

So here’s what I’ve found. In the current code, we set pointer capture when we get a pointer down event so that we can track input, if it leaves the window before the button that initiated the pointer down event is release. Pointer capture can’t be set when a pointer lock is set.

I am currently testing a fix where we check for pointer lock before using setPointerCapture and should have a PR up sometime today.

1 Like