Skip to content

Instantly share code, notes, and snippets.

@eric-schleicher
Created May 16, 2017 00:07
Show Gist options
  • Save eric-schleicher/7dd2081debe9ebce0178caa6492ece48 to your computer and use it in GitHub Desktop.
Save eric-schleicher/7dd2081debe9ebce0178caa6492ece48 to your computer and use it in GitHub Desktop.
example script showing gamepad enumeration with and without vr displays
<html>
<head>
<meta charset="UTF-8">
<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
<title>Test Gamepads</title>
</head>
<body>
<script>
$(function(){
//bind functions to keep track to gamepads
window.gamepadTest = {
connectedGamePadCount: 0,
startupEvents :[],
showStartupEvents: function(){
window.gamepadTest.startupEvents.forEach(function(item){
console.log ("[" + item.timestamp + "]", item.name );
})
console.log("length of navigator.getGamepads():", navigator.getGamepads().length );
}
};
window.addEventListener('gamepadconnected', function(event){
this.gamepadTest.startupEvents.push({"name":"gamepad::gamepad connected (" +event.gamepad.id + ")", "timestamp": performance.now()})
this.gamepadTest.connectedGamePadCount++ || 0
})
window.addEventListener('gamepaddisconnected', function(event){
this.gamepadTest.startupEvents.push({"name":"gamepad::gamepad disconnected (" +event.gamepad.id ? event.gamepad.id : UNKNOWN + ")", "timestamp": performance.now()})
this.gamepadTest.connectedGamePadCount-- || 0
})
window.gamepadTest.startupEvents.push({"name": "lifecycle::script started", "timestamp":performance.now()})
console.log("current number of gamepad controllers: " + window.gamepadTest.connectedGamePadCount);
console.log("about to enumerate VR displays")
navigator.getVRDisplays()
.then(function(result){
window.gamepadTest.startupEvents.push(
{
"name":"vr::got displays (" + result.length + ")",
"timestamp":performance.now()
});
if (result.length === 0) console.log("no VR displays Found")
})
.catch(function(err){
debugger
console.log("`getVRDisplays` Failure");
window.gamepadTest.startupEvents.push({"name":"lifecycle::error","timestamp": performance.now()})
throw err;
})
}());
console.log("waiting 5 seconds to show startup summary");
setTimeout(window.gamepadTest.showStartupEvents,5000)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment