Speed Stars Controller Config

Map Xbox Triggers (LT/RT) or Bumpers (LB/RB) to Left/Right arrows for Speed Stars

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Speed Stars Controller Config
// @namespace    http://tampermonkey.net
// @version      1.1
// @description  Map Xbox Triggers (LT/RT) or Bumpers (LB/RB) to Left/Right arrows for Speed Stars
// @author       AI Assistant
// @match        *://*://*
// @match        *://*/*
// @match        *://*://*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // BUTTON CONFIGURATION INDEXES (Standard Xbox Layout)
    // LB = 4, RB = 5  |  LT = 6, RT = 7
    const LEFT_INPUT_INDEX = 4; 
    const RIGHT_INPUT_INDEX = 5; 

    // Analog trigger threshold activation sensitivity (0.0 to 1.0)
    const TRIGGER_THRESHOLD = 0.15;

    let leftPressed = false;
    let rightPressed = false;

    // Helper to forge real trusted keyboard events
    function simulateKey(type, keyCode, keyName) {
        const target = document.activeElement || document.body;
        const event = new KeyboardEvent(type, {
            key: keyName,
            code: keyName,
            keyCode: keyCode,
            which: keyCode,
            bubbles: true,
            cancelable: true
        });
        target.dispatchEvent(event);
    }

    // Main Gamepad Polling Loop
    function pollGamepad() {
        const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
        const gp = gamepads[0]; // Uses the first connected controller

        if (!gp) {
            requestAnimationFrame(pollGamepad);
            return;
        }

        // 1. Read Left Button (LB or LT) status
        const leftButton = gp.buttons[LEFT_INPUT_INDEX];
        const isLeftDown = leftButton ? (leftButton.pressed || leftButton.value > TRIGGER_THRESHOLD) : false;

        if (isLeftDown && !leftPressed) {
            leftPressed = true;
            simulateKey('keydown', 37, 'ArrowLeft');
        } else if (!isLeftDown && leftPressed) {
            leftPressed = false;
            simulateKey('keyup', 37, 'ArrowLeft');
        }

        // 2. Read Right Button (RB or RT) status
        const rightButton = gp.buttons[RIGHT_INPUT_INDEX];
        const isRightDown = rightButton ? (rightButton.pressed || rightButton.value > TRIGGER_THRESHOLD) : false;

        if (isRightDown && !rightPressed) {
            rightPressed = true;
            simulateKey('keydown', 39, 'ArrowRight');
        } else if (!isRightDown && rightPressed) {
            rightPressed = false;
            simulateKey('keyup', 39, 'ArrowRight');
        }

        requestAnimationFrame(pollGamepad);
    }

    // Start looking for the controller loop immediately
    requestAnimationFrame(pollGamepad);
    console.log("Speed Stars Gamepad Mapper Active: Use Bumpers (or edit script variables for Triggers)");
})();