Skip to content

PUIGamepadController

Elijah Cobb edited this page May 15, 2021 · 1 revision

This controller allows you to attach event handlers to specific button presses or actions taken by a user using a Gamepad. This will support ANY major gamepad. This API gives you access to all buttons, joysticks, and triggers. As you can see below, you can very quickly and easily use a gamepad with a PUI app.

import {PUIGamepadButton, PUIGamepadController} from "@pstdl/ui;

const gamepadController = new PUIGamepadController();

gamepadController.on("leftJoystickMove", (x, y) => {
	console.log(`Left joystick moved to: (${x}, ${y}).`)
});

gamepadController.on("rightJoystickMove", (x, y) => {
	console.log(`Right joystick moved to: (${x}, ${y}).`)
});

gamepadController.on("leftTrigger", v => {
	console.log(`Left trigger is depressed ${v}.`);
});

gamepadController.on("rightTrigger", v => {
	console.log(`Right trigger is depressed ${v}.`);
});

gamepadController.on("buttonDown", button => {
	if (button === PUIGamepadButton.A) console.log("A is down!");
	else console.log("Another button is down.")
});

gamepadController.on("buttonUp", button => {
	if (button === PUIGamepadButton.START) console.log("START button was let go.");
	else console.log("Another button was let go.")
});

gamepadController.on("buttonChange", (button, pressed) => {
	console.log(`Button '${button}' is ${pressed ? "pressed" : "not-pressed"}.`);
});

Clone this wiki locally