Speed Stars Controller Config

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

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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)");
})();