secret
Last active

MozJoy* listeners

  • Download Gist
joystick.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 
This gist predates the current GamepadAPI, preserved it for posterity.
 
https://wiki.mozilla.org/GamepadAPI
 
 
MozJoyAxisMove
MozJoyButtonUp
MozJoyButtonDown
*/
 
var joyEvents = {
 
MozJoyAxisMove: {
props: [ "joystickID", "axis", "value" ],
listen: 1
},
MozJoyButtonUp: {
props: [ "joystickID", "button" ],
listen: 1
},
MozJoyButtonDown: {
props: [ "joystickID", "button" ],
listen: 1
}
};
 
// For each type of joystick event, add an event listener to log events
Object.keys( joyEvents ).forEach(function( type, idx ) {
 
var joyEvent = joyEvents[ type ];
 
if ( joyEvent.listen ) {
 
window.addEventListener( type, function( event ) {
 
console.log(
type,
joyEvent.props.map(function( val ) {
return val + ": " + event[ val ];
}).join(", ")
);
 
}, false);
}
 
if ( idx === Object.keys( joyEvents ).length - 1 ) {
console.log( "MozJoy* Events Ready" );
}
}, joyEvents );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.