Skip to content

Instantly share code, notes, and snippets.

@Plnda
Last active September 15, 2020 12:26
Show Gist options
  • Save Plnda/44a1fc04d957b033c60c6c77c0599ff0 to your computer and use it in GitHub Desktop.
Save Plnda/44a1fc04d957b033c60c6c77c0599ff0 to your computer and use it in GitHub Desktop.
var emulatedGamepad = {
id: "Emulated iOS Controller",
index: 0,
connected: true,
timestamp: 0,
mapping: "standard",
axes: [0, 0, 0, 0],
buttons: new Array(17).fill().map(m => ({pressed: false, touched: false, value: 0}))
}
navigator.getGamepads = function() {
window.webkit.messageHandlers.controller.postMessage({}).then((controllerData) => {
try {
var data = JSON.parse(controllerData);
for(let i = 0; i < data.buttons.length; i++) {
emulatedGamepad.buttons[i].pressed = data.buttons[i].pressed;
emulatedGamepad.buttons[i].value = data.buttons[i].value;
}
for(let i = 0; i < data.axes.length; i++) {
emulatedGamepad.axes[i] = data.axes[i]
}
} catch(e) {
}
});
return [emulatedGamepad, null, null, null];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment