Skip to content

Instantly share code, notes, and snippets.

@Krazete
Last active August 11, 2021 03:52
Show Gist options
  • Save Krazete/76c5096eb183723b0ff0d28f2a7151fb to your computer and use it in GitHub Desktop.
Save Krazete/76c5096eb183723b0ff0d28f2a7151fb to your computer and use it in GitHub Desktop.
Enable game controllers for GuraQuest.
// Instructions
// 1. Go to https://www.newgrounds.com/portal/view/809045 or https://lee-pic.itch.io/guraquest.
// 2. Click "Play Game" or "Run Game".
// 3. Open your browser's console.
// 4. Change JS context from "top" to "uploads.ungrounded.net" or "index.html" (Chrome)
// or from "Auto" to "game_drop" (Safari).
// 5. Copy and paste the code below into the console and press Enter.
var gamepadListener;
var game = document.getElementById("canvas");
function pressKey(keyCode, value) {
game.dispatchEvent(new KeyboardEvent(
value > 0.1 ? "keydown" : "keyup",
{keyCode: keyCode, which: keyCode, bubbles: true}
));
}
function gamepadListen() {
gamepadListener = requestAnimationFrame(gamepadListen);
var gamepad = navigator.getGamepads()[0]; // dualshock
if (gamepad) {
pressKey(32, gamepad.buttons[0].value); // cross
pressKey(88, gamepad.buttons[1].value); // circle
// pressKey(0, gamepad.buttons[2].value); // square
// pressKey(0, gamepad.buttons[3].value); // triangle
// pressKey(0, gamepad.buttons[4].value); // l1
// pressKey(0, gamepad.buttons[5].value); // r1
// pressKey(0, gamepad.buttons[6].value); // l2
// pressKey(0, gamepad.buttons[7].value); // r2
// pressKey(0, gamepad.buttons[8].value); // share
pressKey(13, gamepad.buttons[9].value); // options
// pressKey(0, gamepad.buttons[10].value); // l3
// pressKey(0, gamepad.buttons[11].value); // r3
// pressKey(38, gamepad.buttons[12].value); // up
// pressKey(40, gamepad.buttons[13].value); // down
pressKey(37, Math.max(gamepad.buttons[14].value, -gamepad.axes[0])); // left
pressKey(39, Math.max(gamepad.buttons[15].value, gamepad.axes[0])); // right
// pressKey(0, gamepad.buttons[16].value); // playstation
// pressKey(0, gamepad.buttons[17].value); // touchpad
if (gamepad.buttons[16].value) {
location.reload();
}
}
else {
cancelAnimationFrame(gamepadListener);
pressKey(32, 0);
pressKey(88, 0);
pressKey(13, 0);
pressKey(37, 0);
pressKey(39, 0);
}
}
if (navigator.getGamepads().length) {
gamepadListen();
}
window.addEventListener("gamepadconnected", gamepadListen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment