Skip to content

Instantly share code, notes, and snippets.

@cedced19
Last active August 29, 2015 14:20
Show Gist options
  • Save cedced19/d146c75db76046534028 to your computer and use it in GitHub Desktop.
Save cedced19/d146c75db76046534028 to your computer and use it in GitHub Desktop.
Catch event on a gamepad
var gamepad = require('gamepad'),
colors = require('colors'),
app = new hapi.Server();
gamepad.init();
// List the state of all currently attached devices
for (var i = 0, l = gamepad.numDevices(); i < l; i++) {
console.log(i, gamepad.deviceAtIndex());
}
// Create a game loop and poll for events
setInterval(gamepad.processEvents, 16);
// Scan for new gamepads as a slower rate
setInterval(gamepad.detectDevices, 500);
// Listen for move events on all gamepads
gamepad.on('move', function (id, axis, value) {
if (axis == 5 && value == 0) {
console.log(colors.yellow('Left') + ' of the little controller');
return;
} else if (axis == 5 && value == 1) {
console.log(colors.yellow('Right') + ' of the little controller');
return;
} else if (axis == 6 && value == 0) {
console.log(colors.yellow('Up') + ' of the little controller');
return;
} else if (axis == 6 && value == 1) {
console.log(colors.yellow('Down') + ' of the little controller');
return;
} else if (axis == 3 && value < 0 && value >= -1) {
if (value == -1) {
console.log(colors.yellow('Up stict') + ' of the master controller');
return;
}
console.log(colors.yellow('Up') + ' of the master controller');
return;
} else if (axis == 3 && value > 0 && value <= 1) {
if (value == 1) {
console.log(colors.yellow('Down stict') + ' of the master controller');
return;
}
console.log(colors.yellow('Down') + ' of the master controller');
return;
} else if (axis == 4 && value < 0 && value >= -1) {
if (value == -1) {
console.log(colors.yellow('Left stict') + ' of the master controller');
return;
}
console.log(colors.yellow('Left') + ' of the master controller');
return;
} else if (axis == 4 && value > 0 && value <= 1) {
if (value == 1) {
console.log(colors.yellow('Right stict') + ' of the master controller');
return;
}
console.log(colors.yellow('Right') + ' of the master controller');
return;
}
});
gamepad.on('up', function (id, num) {
num++;
console.log('Number ' + colors.yellow(num) + ' is up');
});
gamepad.on('down', function (id, num) {
num++;
console.log('Number ' + colors.yellow(num) + ' is down');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment