Speed Stars Controller Config

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

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

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.

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

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

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

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