window.addEventListener("gamepadconnected", gamepadHandler);Desktop gamepad controls / notebook
Desktop gamepad controls
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
let controller = {};
let buttonsPressed = [];
function gamepadHandler(e) {
controller = e.gamepad;
output.textContent = `Gamepad: ${controller.id}`;
}function gamepadUpdateHandler() {
buttonsPressed = [];
if (controller.buttons) {
for (const [i, button] of controller.buttons.entries()) {
if (button.pressed) {
buttonsPressed.push(i);
}
}
}
}function gamepadButtonPressedHandler(button) {
return buttonsPressed.includes(button);
}function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// …
gamepadUpdateHandler();
if (gamepadButtonPressedHandler(0)) {
playerY -= 5;
} else if (gamepadButtonPressedHandler(1)) {
playerY += 5;
}
if (gamepadButtonPressedHandler(2)) {
playerX -= 5;
} else if (gamepadButtonPressedHandler(3)) {
playerX += 5;
}
if (gamepadButtonPressedHandler(11)) {
alert("BOOM!");
}
// …
ctx.drawImage(img, playerX, playerY);
requestAnimationFrame(draw);
}const GamepadAPI = {
active: false,
controller: {},
connect(event) {},
disconnect(event) {},
update() {},
buttons: {
layout: [],
cache: [],
status: [],
pressed(button, state) {},
},
axes: {
status: [],
},
};window.addEventListener("gamepadconnected", GamepadAPI.connect);
window.addEventListener("gamepaddisconnected", GamepadAPI.disconnect);const GamepadAPI = {
// …
connect(event) {
GamepadAPI.controller = event.gamepad;
GamepadAPI.active = true;
},
// …
};const GamepadAPI = {
// …
disconnect(event) {
delete GamepadAPI.controller;
GamepadAPI.active = false;
},
};const GamepadAPI = {
// …
update() {
GamepadAPI.buttons.cache = [];
for (let k = 0; k < GamepadAPI.buttons.status.length; k++) {
GamepadAPI.buttons.cache[k] = GamepadAPI.buttons.status[k];
}
GamepadAPI.buttons.status = [];
const c = GamepadAPI.controller || {};
const pressed = [];
if (c.buttons) {
for (let b = 0; b < c.buttons.length; b++) {
if (c.buttons[b].pressed) {
pressed.push(GamepadAPI.buttons.layout[b]);
}
}
}
const axes = [];
if (c.axes) {
for (const ax of c.axes) {
axes.push(ax.toFixed(2));
}
}
GamepadAPI.axes.status = axes;
GamepadAPI.buttons.status = pressed;
return pressed;
},
// …
};const GamepadAPI = {
// …
buttons: {
// …
pressed(button, hold) {
let newPress = false;
if (GamepadAPI.buttons.status.includes(button)) {
newPress = true;
}
if (!hold && GamepadAPI.buttons.cache.includes(button)) {
newPress = false;
}
return newPress;
},
// …
},
// …
};function create() {
// …
const message = "Gamepad connected! Press Y for controls";
const textGamepad = this.add.text(0, 0, message);
textGamepad.visible = false;
}function update() {
// …
if (GamepadAPI.active) {
this.textGamepad.visible = true;
GamepadAPI.update();
if (GamepadAPI.buttons.pressed("Start")) {
// start the game
}
if (GamepadAPI.buttons.pressed("X")) {
// turn on/off the sounds
}
this.screenGamepadHelp.visible = GamepadAPI.buttons.pressed("Y", "hold");
}
}function create() {
// …
if (this.game.device.desktop) {
if (GamepadAPI.active) {
moveText = "DPad or left Stick\nto move";
shootText = "A to shoot,\nY for controls";
} else {
moveText = "Arrow keys\nor WASD to move";
shootText = "X or Space\nto shoot";
}
} else {
moveText = "Tap and hold to move";
shootText = "Tap to shoot";
}
}if (GamepadAPI.buttons.pressed("DPad-Up", "hold")) {
// move player up
} else if (GamepadAPI.buttons.pressed("DPad-Down", "hold")) {
// move player down
}
if (GamepadAPI.buttons.pressed("DPad-Left", "hold")) {
// move player left
}
if (GamepadAPI.buttons.pressed("DPad-Right", "hold")) {
// move player right
}
if (GamepadAPI.axes.status) {
if (GamepadAPI.axes.status[0] > 0.5) {
// move player up
} else if (GamepadAPI.axes.status[0] < -0.5) {
// move player down
}
if (GamepadAPI.axes.status[1] > 0.5) {
// move player left
} else if (GamepadAPI.axes.status[1] < -0.5) {
// move player right
}
}if (GamepadAPI.buttons.pressed("A", "hold")) {
this.spawnBullet();
}this.screenGamepadHelp.visible = GamepadAPI.buttons.pressed("Y", "hold");if (gamepadAPI.buttonPressed("B")) {
this.managePause();
}if (GamepadAPI.buttons.pressed("Start")) {
this.managePause();
}
if (GamepadAPI.buttons.pressed("Back")) {
this.stateBack();
}if (GamepadAPI.buttons.pressed("Start")) {
this.stateRestart();
}
if (GamepadAPI.buttons.pressed("Back")) {
this.stateBack();
}