var thumbStickThreshHold=.75,buttonStates=[{padIndex:0,key:"GamepadA",keyCode:195},{padIndex:1,key:"GamepadB",keyCode:196},{padIndex:2,key:"GamepadX",keyCode:197},{padIndex:3,key:"GamepadY",keyCode:198},{padIndex:4,key:"GamepadLeftShoulder",keyCode:200},{padIndex:5,key:"GamepadRightShoulder",keyCode:199},{padIndex:6,key:"GamepadLeftTrigger",keyCode:201},{padIndex:7,key:"GamepadRightTrigger",keyCode:202},{padIndex:8,key:"GamepadView",keyCode:208},{padIndex:9,key:"GamepadMenu",keyCode:207},{padIndex:10,key:"GamepadLeftThumbstickButton",keyCode:209},{padIndex:11,key:"GamepadRightThumbstickButton",keyCode:210},{padIndex:12,key:"GamepadDPadUp",keyCode:203},{padIndex:13,key:"GamepadDPadDown",keyCode:204},{padIndex:14,key:"GamepadDPadLeft",keyCode:205},{padIndex:15,key:"GamepadDPadRight",keyCode:206}],axisStates=[{axis:0,key:"GamepadLeftThumbstickLeft",keyCode:214},{axis:0,key:"GamepadLeftThumbstickRight",keyCode:213},{axis:1,key:"GamepadLeftThumbstickUp",keyCode:211},{axis:1,key:"GamepadLeftThumbstickDown",keyCode:212},{axis:2,key:"GamepadRightThumbstickLeft",keyCode:218},{axis:2,key:"GamepadRightThumbstickRight",keyCode:217},{axis:3,key:"GamepadRightThumbstickUp",keyCode:215},{axis:3,key:"GamepadRightThumbstickDown",keyCode:216}],allControls=[],isLooping=!1,repeatIntervalId=null;function allowInput(){var doc=document;return"hidden"!==doc.visibilityState&&!!doc.hasFocus()}function stopRepeatInterval(){repeatIntervalId&&(clearInterval(repeatIntervalId),repeatIntervalId=null)}function startRepeatInterval(){stopRepeatInterval(),repeatIntervalId=window.setInterval(onRepeatInterval,100)}function onRepeatInterval(){var downButton=allControls.find(function(e){return e.pressed});downButton&&1<allControls.filter(function(e){return e.pressed}).length&&(downButton=null),allowInput()?downButton?raiseEvent("keydown",downButton.key,downButton.keyCode,!0):stopRepeatInterval():(stopRepeatInterval(),clearAll())}function raiseEvent(name,key,keyCode,repeat){var event,element;!allowInput()&&"keyup"!==name||((event=document.createEvent("Event")).initEvent(name,!0,!0),event.key=key,event.keyCode=keyCode,null!=repeat&&(event.repeat=repeat),element=document.activeElement||document.body,setTimeout(function(){return element.dispatchEvent(event)}))}function getGamepads(){try{return navigator.getGamepads()||[]}catch(err){return console.error("Error getting gamepads: ",err),[]}}function fireKeyUpEvents(){for(var i=0;i<allControls.length;i++){var button=allControls[i];button.pressed&&!button.newPressedState&&(button.pressed=!1,raiseEvent("keyup",button.key,button.keyCode,!1))}}function fireKeyDownEvents(){for(var i=0;i<allControls.length;i++){var button=allControls[i];!button.pressed&&button.newPressedState&&(button.pressed=!0,raiseEvent("keydown",button.key,button.keyCode,!1))}}function processStateChanges(){allControls.some(function(e){return e.pressed||e.newPressedState})&&!allowInput()?clearAll():((allControls.find(function(e){return!e.pressed&&e.newPressedState})?(fireKeyUpEvents(),fireKeyDownEvents(),startRepeatInterval):fireKeyUpEvents)(),allControls.some(function(e){return e.pressed!==e.newPressedState})&&console.error("gamepadtokey.processStateChanges: Logic error!"))}function clearAll(){allControls.forEach(function(e){return e.newPressedState=!1}),fireKeyUpEvents()}function runInputLoop(){for(var gamepads=getGamepads(),gamepad=null,i=0,length=gamepads.length;i<length;i++)if(gamepads[i]){gamepad=gamepads[i];break}if(gamepad){if(!(gamepad.timestamp&&gamepad._lastTimestamp===gamepad.timestamp||!gamepad.timestamp&&gamepad._lastTimestamp&&Date.now()-gamepad._lastTimestamp<100)){gamepad._lastTimestamp=gamepad.timestamp||Date.now();for(var buttons=gamepad.buttons,j=0;j<buttons.length&&j<buttonStates.length;j++)buttonStates[j].newPressedState=buttons[j].pressed;for(var axes=gamepad.axes,_j=0;_j<axes.length&&_j<4;_j++)axisStates[2*_j].newPressedState=axes[_j]<-thumbStickThreshHold,axisStates[2*_j+1].newPressedState=axes[_j]>thumbStickThreshHold;allControls.some(function(e){return e.pressed!==e.newPressedState})&&processStateChanges()}requestAnimationFrame(runInputLoop)}else clearAll(),console.log("exiting gamepad input loop"),isLooping=!1}buttonStates.forEach(function(e){return allControls.push(e)}),axisStates.forEach(function(e){return allControls.push(e)}),allControls.forEach(function(e){return e.pressed=!1}),allControls.forEach(function(e){return e.newPressedState=!1}),allControls.forEach(function(e,index){return e.index=index}),navigator.getGamepads||(navigator.webkitGetGamepads?navigator.getGamepads=navigator.webkitGetGamepads:navigator.getGamepads=function(){return null}),window.addEventListener("gamepadconnected",function(e){var e=e.gamepad;e&&(e=e.index,console.log("gamepadconnected: "+e),isLooping||(isLooping=!0,runInputLoop()))}),window.addEventListener("gamepaddisconnected",function(e){clearAll();var e=e.gamepad;e&&(e=e.index,console.log("gamepaddisconnected: "+e))});