Speed Stars Controller Config

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 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)");
})();