Example: Buttons and Joysticks

Connect a gamepad to the computer, and click the buttons or move the different joysticks/axes. They will highlight as they are pressed.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 SELECT START

This is a 17-button standard gamepad layout as defined on the W3C Gamepad API definition. It may not match the gamepad that is connected, but it can be used to see how the buttons/joysticks in that gamepad match the buttons in the standard gamepad.

How it works

Event handlers can be associated to the buttons using the .on() method:

gamepad.on(DIRECTION_NAME+AXE_ID, CALLBACK);

Only one action is allowed by button/joystick/axe. If you use several .on() with the same direction, the latest call to it will be the one that is applied when the joystick is pressed.

gameControl.on('connect', function(gamepad) {
  gamepad.on('select', function() {
    // do something
  });
});

Some buttons/directions have aliases, so it is easier to associate events to them. For example, if we only use the name of the direction ("up", "dowm", "right", or "left"), the event handler will be associated to that direction in the first joystick/axe:

gamepad.on('up0', function() {
  // do something
});

gamepad.on('up', function() {
  // do something
});

Using aliases doesn't mean that you will be able to use more than one action per direction, as they are the same.

The available direction aliases are:

The available button aliases are: