I have been working on developing a virtual controller in the form of a Gamepad class and registering it. Currently, my implementation is essentially a duplicate of the existing Gamepad class:
class CustomController {
readonly axes: ReadonlyArray<number>;
readonly buttons: ReadonlyArray<GamepadButton>;
readonly connected: boolean;
readonly hapticActuators: ReadonlyArray<GamepadHapticActuator>;
readonly id: string;
readonly index: number;
readonly mapping: GamepadMappingType;
readonly timestamp: DOMHighResTimeStamp;
constructor() {
this.axes = [];
this.buttons = [];
this.connected = true;
this.hapticActuators = [];
this.id = "custom-id";
this.index = 200;
this.mapping = "standard";
this.timestamp = Math.floor(Date.now() / 1000);
}
}
To implement it, I used the following code:
const event = new GamepadEvent("gamepadconnected", {
gamepad: new CustomController() as Gamepad
})
window.dispatchEvent(event);
However, an error message pops up stating:
Failed to construct 'GamepadEvent': Failed to read the 'gamepad' property from 'GamepadEventInit': Failed to convert value to 'Gamepad'.
TypeError: Failed to construct 'GamepadEvent': Failed to read the 'gamepad' property from 'GamepadEventInit': Failed to convert value to 'Gamepad'.
Seeking solutions: How can I successfully create and register my own custom Gamepad class with a GamepadEvent
?