Keyboard key + click listener

Hello everyone,
Can I somehow make listener on e.q. ctrl + left mouse button? I made this playground and I can change the target of the ArcRotateCamera by double-clicking on objects. But I’d love to make when the user holds ctrl and then do DoubleClick.
Also, I wanted to camera radius to stay the same after the switch. Do I make it the right way?

https://www.babylonjs-playground.com/#BBMK96#2

I guess I could implement actionManager when anything is doubleclicked, not on every single mesh.

Hi Symlis,

Looks like you’ve pretty much got it already! Does this bridge the final few missing features?

https://www.babylonjs-playground.com/#BBMK96#3

I changed a few things, most of which was just removing code duplication to make it easier to change all the behaviors at once. The “only do this when control is pressed” behavior is implemented as a simple early-out informed by input events received from the canvas (you might prefer to get them from the window or something, I just picked the canvas as a sort of input source default); implementation from lines 42 through 52 and from 57 through 60. The other thing I changed, which is a little bit trickier, is I changed the inline lambdas from anonymous functions—function (...) { ... }—to arrow functions—(...) => { ... }. I did this because the early-out depends on reading the changing value of the isCtrlPressed variable; this syntax requires the use of arrow functions because they have no separate this, which allows them to read a changing value that the anonymous function syntax would have instead taken a copy of.

1 Like

Hello @syntheticmagus,
Thank you very much for your answer, it helped me a lot :smiley: There was one bug with your playground. keyup event listener didn’t work so I changed the if’s statement to
e.code === "ControlLeft"
and it started changing isCtrlPressed variable to false on keyup.

https://www.babylonjs-playground.com/#BBMK96#4

1 Like