Game keys manager for handling key bindings and tracking pressed keys.

Constructors

Properties

Methods

Constructors

Properties

addKeys: {
    (id: string, key: string, fn?: ((dt: number) => void)): void;
    (keysToAdd: KeyBinding | KeyBinding[]): void;
} = ...

Type declaration

    • (id, key, fn?): void
    • Adds or updates a key binding.

      Parameters

      • id: string

        The id of the key binding.

      • key: string

        The key associated with the binding.

      • Optionalfn: ((dt: number) => void)

        The function executed when the binding is pressed

          • (dt): void
          • Parameters

            • dt: number

            Returns void

      Returns void

      Use the other overload instead, as it is more flexible.

      addKey("Move Up", "w", () => player.velocity.y += player.acceleration.y);
      
    • (keysToAdd): void
    • Adds or updates multiple key bindings.

      Parameters

      Returns void

      const moveUpKeyBinding: KeyBinding = { name: "Move Up", key: "w", onDownContinuous: () => player.velocity.y += player.acceleration.y };
      // Use either of the following:
      addKey(moveUpKeyBinding);
      // or
      addKeys([
      moveUpKeyBinding,
      // Add more key bindings here...
      ]);

Use addKey instead.

binds: KeyBinding[]

The key bindings

Methods

  • Adds or updates a key binding.

    Parameters

    • id: string

      The id of the key binding.

    • key: string

      The key associated with the binding.

    • Optionalfn: ((dt: number) => void)

      The function executed when the binding is pressed

        • (dt): void
        • Parameters

          • dt: number

          Returns void

    Returns void

    Use the other overload instead, as it is more flexible.

    addKey("Move Up", "w", () => player.velocity.y += player.acceleration.y);
    
  • Adds or updates multiple key bindings.

    Parameters

    Returns void

    const moveUpKeyBinding: KeyBinding = { name: "Move Up", key: "w", onDownContinuous: () => player.velocity.y += player.acceleration.y };
    // Use either of the following:
    addKey(moveUpKeyBinding);
    // or
    addKeys([
    moveUpKeyBinding,
    // Add more key bindings here...
    ]);
  • Changes the framerate of the key manager.

    Parameters

    • fps: number

      The new framerate to use.

    Returns void