find if camera has attached or detached keyboard control to canvas in Babylonjs

648 Views Asked by At

I have a program in BabylonJS that has multiple cameras (free camera, scene, follow, and sometimes an arc camera). I set the active camera(s) and attach control (keyboard based movement) using the following:

scene.activeCameras[0] = camera;
camera.attachControl(canvas);

There are times I bring up html forms in my program and allow the user to type in the form (or temporarily turn the controls over to custom keydown / keyup controls) so I use the following to detach the control (so that the typed keys are not intercepted by the canvas scene):

for (var i = 0;i < scene.activeCameras.length;i++) {
    scene.activeCameras[i].detachControl(canvas);
}

Then I attach again when done. This works great, but there are times when I need to test if the control is attached or not.

Currently, I set an outside variable (but I have to place it in many locations in my code) but I was wondering if there is a function in BabylonJS that tells you the attached control camera name or boolean true / false if camera control is currently attached?

2

There are 2 best solutions below

0
On BEST ANSWER

you can test scene.activeCameras[i].inputs.attachedElement

0
On

Using #DavidCatuhe 's answer I was able to write this function others might find useful. Thanks!

function iscamaraattached() {
    var attached = false;
    if (scene.activeCameras != null) {
        for (var i=0;i < scene.activeCameras.length;i++) {
            if (scene.activeCameras[i].inputs.attachedElement != null) {
console.log("i=" + i + " - " + scene.activeCameras[i].inputs.attachedElement.id);
                attached = true;
            }
        }
    }
    return attached;
}