How do I achieve a Mouse Up & Key Down combination?

Hi everyone.

I am hoping that this is simple. In my playground I have green spheres that attach to the roof space, when you click the mouse. I would really like this action to happen when a key (say Ctrl) and the mouse is clicked. At present the spheres can be attached accidently if the mouse hovers over the roof space and the camera is rotated with the left mouse button.

So, having a combination of mouse click and key press should eliminate this accident.

Any help would be appreciated.

Cheers.

You can also use right click:

var left, right;
left = 1;
right = 2;
scene.onPointerDown = (e) => {
if(e.button === right){ 
// your code   
}
}

Example - https://www.babylonjs-playground.com/index.html#TAZ2CB#20

1 Like

Hi Iabris, thanks for your answer.

Sorry…I must be doing something wrong, it’s not working.

I had tried this code earlier, before entering my query into the forum, and could not get it to work. But I gave it another shot with the code you provided.

Ideally, I think I would still like a key and mouse input, as I may need more functionality that the mouse alone cannot do. But having a right click function would be really handy.

Cheers.

You must define the right variable: var right = 2;

https://playground.babylonjs.com/#IM9BMN#6

Regarding the ctrl key, you can use the scene.onKeyboardObservable to keep track of the state of this key:

https://playground.babylonjs.com/#IM9BMN#7

3 Likes

Hi Evgeni_Popov, thanks for your help.

I just this second entered the forum to say that I had solved my issue…I put it down to some bad coding I had in other parts of my project. So, maybe my code wasn’t bad after all (I’m sure it was).

And the scene.onKeyboardObservable looks interesting.

Thanks again.

And thanks again to Iabris, who steered me in the right direction.

Cheers.

1 Like