# Confusion about how to implement ICameraInput in TypeScript

**URL:** <https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833>\
**Category:** Questions\
**Created:** [October 11, 2020, 7:00pm UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833 "2020-10-11T19:00:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![owen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/owen/32/9325_2.png) [@owen](https://forum.babylonjs.com/u/owen)\
**Post date:** [October 11, 2020, 7:00pm UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/1 "2020-10-11T19:00:40Z")

</div>

I’m following the guides on [Customize Camera Inputs - Babylon.js Documentation](https://doc.babylonjs.com/how_to/customizing_camera_inputs)  
I like the camera movement in this playground example:

https://www.babylonjs-playground.com/#6PA720

And I am trying to port the example from javascript to typescript.

The docs say `Using TypeScript, you could implement the interface ICameraInput` so presumably I implement that with a class like so:

`export class FreeCameraKeyboardWalkInput implements BABYLON.ICameraInput<BABYLON.UniversalCamera> `

and then just port the required functions from the playground into the class.

Where I get confused is that the example functions in the playground reference properties like this.\_onKeyDown and this.\_onKeyUp which are not defined in the interface. I’m not sure where my class is supposed to get those.

Here is an example:

In the playground, one of the functions to implement is ‘detachControl’:

```
//Add detachment controls
        FreeCameraKeyboardWalkInput.prototype.detachControl = function (element) {
            if (this._onKeyDown) {
                element.removeEventListener("keydown", this._onKeyDown);
                element.removeEventListener("keyup", this._onKeyUp);
                BABYLON.Tools.UnregisterTopRootEvents([
                    { name: "blur", handler: this._onLostFocus }
                ]);
                this._keys = [];
                this._onKeyDown = null;
                this._onKeyUp = null;
            }
        };

```

In my class that would be a instance method (see further below):

```
export class FreeCameraKeyboardWalkInput implements BABYLON.ICameraInput<BABYLON.UniversalCamera>{
  
  camera: UniversalCamera
  _keys: number[]
  keysUp: number[]
  keysDown: number[]
  keysLeft: number[]
  keysRight: number[]

  constructor() {
    this._keys = [];
    this.keysUp = [38];
    this.keysDown = [40];
    this.keysLeft = [37];
    this.keysRight = [39];
  }
 .....
 
   detachControl(element: HTMLElement) {     
      if (this._onKeyDown) {
        element.removeEventListener("keydown", this._onKeyDown);
        element.removeEventListener("keyup", this._onKeyUp);
        BABYLON.Tools.UnregisterTopRootEvents(window, [
          { name: "blur", handler: this._onLostFocus }
        ]);
        this._keys = [];
        this._onKeyDown = null;
        this._onKeyUp = null;
      }
    }

```

Error: Property ‘\_onKeyDown’ does not exist on type ‘FreeCameraKeyboardWalkInput’

I did a search of the code base for \_onKeyDown and it can be found in ’ src/Inputs/scene.inputManager.ts’

So then I thought maybe I need to extend my class from an inputManager to access those properties?

I tried inheriting from a inputs manager like this, as a guess:  
`export class FreeCameraKeyboardWalkInput extends BABYLON.CameraInputsManager<BABYLON.UniversalCamera> implements BABYLON.ICameraInput<BABYLON.UniversalCamera>` but intellisense still doesn’t know how this.\_onKeyDown is defined.

No sure at this point… anyway I think the documentation could benefit from at least one TypeScript example. 🙂

Thanks for any advice on how to proceed!

---

<div class="post-metadata">

**Author:** ![owen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/owen/32/9325_2.png) [@owen](https://forum.babylonjs.com/u/owen)\
**Post date:** [October 11, 2020, 10:03pm UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/2 "2020-10-11T22:03:19Z")

</div>

Nevermind. I searched the codebase for ICameraInput and found some typescript examples that implemented it. It’s done in a very different way, but using those examples I was able to port the playground example into a class. Here it is if anyone else is interested:

```
import * as BABYLON from 'babylonjs'

export class FreeCameraKeyboardWalkInput implements BABYLON.ICameraInput<BABYLON.UniversalCamera>{
  public camera: BABYLON.UniversalCamera
  public keysUp = [38]
  public keysDown = [40]
  public keysLeft = [37]
  public keysRight = [39]

  private _keys = new Array<number>();
  private _onCanvasBlurObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.Engine>>;
  private _onKeyboardObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.KeyboardInfo>>;
  private _engine: BABYLON.Engine;
  private _scene: BABYLON.Scene;

  attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
    if (this._onCanvasBlurObserver) {
      return;
    }

    this._scene = this.camera.getScene();
    this._engine = this._scene.getEngine();

    this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
      this._keys = [];
    });

    this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
      let evt = info.event;
      if (!evt.metaKey) {
        if (info.type === BABYLON.KeyboardEventTypes.KEYDOWN) {
          if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
            this.keysDown.indexOf(evt.keyCode) !== -1 ||
            this.keysLeft.indexOf(evt.keyCode) !== -1 ||
            this.keysRight.indexOf(evt.keyCode) !== -1) {
            var index = this._keys.indexOf(evt.keyCode);
            if (index === -1) {
              this._keys.push(evt.keyCode);
            }
            if (!noPreventDefault) {
              evt.preventDefault();
            }
          }

        } else {
          if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
            this.keysDown.indexOf(evt.keyCode) !== -1 ||
            this.keysLeft.indexOf(evt.keyCode) !== -1 ||
            this.keysRight.indexOf(evt.keyCode) !== -1) {
            var index = this._keys.indexOf(evt.keyCode);

            if (index >= 0) {
              this._keys.splice(index, 1);
            }
            if (!noPreventDefault) {
              evt.preventDefault();
            }
          }
        }
      }
    });

  }

  detachControl(element: BABYLON.Nullable<HTMLElement>): void {
    if (this._scene) {
      if (this._onKeyboardObserver) {
        this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
      }

      if (this._onCanvasBlurObserver) {
        this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
      }
      this._onKeyboardObserver = null;
      this._onCanvasBlurObserver = null;
    }
    this._keys = [];
  }

  public checkInputs(): void {
    if (this._onKeyboardObserver) {
      var camera = this.camera;
      // Keyboard
      for (var index = 0; index < this._keys.length; index++) {
        var keyCode = this._keys[index];
        var speed = camera._computeLocalCameraSpeed() / 2;
        if (this.keysLeft.indexOf(keyCode) !== -1) {
          camera.rotation.y -= speed;
          camera._localDirection.copyFromFloats(0, 0, 0);
        } else if (this.keysUp.indexOf(keyCode) !== -1) {
          camera._localDirection.copyFromFloats(0, 0, speed);
        } else if (this.keysRight.indexOf(keyCode) !== -1) {
          camera.rotation.y += speed;
          camera._localDirection.copyFromFloats(0, 0, 0);
        } else if (this.keysDown.indexOf(keyCode) !== -1) {
          camera._localDirection.copyFromFloats(0, 0, -speed);
        }

        if (camera.getScene().useRightHandedSystem) {
          camera._localDirection.z *= -1;
        }

        camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
        BABYLON.Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
        camera.cameraDirection.addInPlace(camera._transformedDirection);
      }
    }
  }

  public getClassName(): string {
    return 'FreeCameraKeyboardWalkInput'
  }

  public _onLostFocus(): void {
    this._keys = [];
  }

  public getSimpleName(): string {
    return 'keyboard'
  }
}
```

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 12, 2020, 9:08am UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/3 "2020-10-12T09:08:45Z")

</div>

> [@owen](#):
>
> Where I get confused is that the example functions in the playground reference properties like this.\_onKeyDown and this.\_onKeyUp which are not defined in the interface.

They are defined in the example:

```javascript
FreeCameraKeyboardRotateInput.prototype.attachControl = function(
  element,
  noPreventDefault
) {
  var _this = this;
  if (!this._onKeyDown) {
    element.tabIndex = 1;
    this._onKeyDown = function(evt) {
        ...
    };
    this._onKeyUp = function(evt) {
        ...
    };

```

---

<div class="post-metadata">

**Author:** ![owen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/owen/32/9325_2.png) [@owen](https://forum.babylonjs.com/u/owen)\
**Post date:** [October 12, 2020, 11:10pm UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/4 "2020-10-12T23:10:15Z")

</div>

Yes… , but this is rather incongruous for the given advice for typescript users, ‘implement the interface ICameraInput’, of which this.\_onKeyDown and such are not members of that interface.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 12, 2020, 11:27pm UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/5 "2020-10-12T23:27:24Z")

</div>

Those are simply private properties used to implement the interface.

The only specific thing to javascript is that you don’t need to declare this property beforehand and you can simply write `this._onKeyDown=...` to create and assign something to the property, whereas in Typescript you will have to declare the property (`private _onKeyDown: (evt: any) => void;`) first before being able to assign something to it.

---

<div class="post-metadata">

**Author:** ![vsnikhilvs](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vsnikhilvs/32/22847_2.png) [@vsnikhilvs](https://forum.babylonjs.com/u/vsnikhilvs)\
**Post date:** [March 15, 2022, 4:16am UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/6 "2022-03-15T04:16:39Z")

</div>

Thank you Owen. Lost almost 2 days for the right implementation of types for this functionality. I also tried implementing BABYLON.Camera, but in vain. It was a pool of errors.  
The ‘class’ you created gave a better idea on how to implement it in ts. I think the doc does not says much. Only a rough idea. But your answer helped so much.

---

<div class="post-metadata">

**Author:** ![owen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/owen/32/9325_2.png) [@owen](https://forum.babylonjs.com/u/owen)\
**Post date:** [March 15, 2022, 8:14am UTC](https://forum.babylonjs.com/t/confusion-about-how-to-implement-icamerainput-in-typescript/14833/7 "2022-03-15T08:14:35Z")

</div>

Glad it helped!
