Polytrack Controller Mapper

Maps controller inputs (Triggers for W/S, Stick for WASD) to keyboard keys for Polytrack on iPad.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Polytrack Controller Mapper
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Maps controller inputs (Triggers for W/S, Stick for WASD) to keyboard keys for Polytrack on iPad.
// @author       You
// @match        https://polytrack.com/*
// @match        https://*.polytrack.com/*
// @match        https://eelekweb.github.io/polytrack/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=polytrack.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    console.log("Polytrack Controller Loaded");

    // Key state tracker to prevent spamming the game with events
    const keys = {
        'w': false,
        'a': false,
        's': false,
        'd': false
    };

    // Helper to send KeyDown
    function triggerDown(key) {
        if (keys[key]) return; // Already pressed
        keys[key] = true;
        let event = new KeyboardEvent('keydown', {
            key: key,
            code: 'Key' + key.toUpperCase(),
            keyCode: key.toUpperCase().charCodeAt(0),
            which: key.toUpperCase().charCodeAt(0),
            bubbles: true,
            cancelable: true,
            view: window
        });
        window.dispatchEvent(event);
        document.dispatchEvent(event);
        // Sometimes games listen to the canvas specifically
        let canvas = document.querySelector('canvas');
        if (canvas) canvas.dispatchEvent(event);
    }

    // Helper to send KeyUp
    function triggerUp(key) {
        if (!keys[key]) return; // Already released
        keys[key] = false;
        let event = new KeyboardEvent('keyup', {
            key: key,
            code: 'Key' + key.toUpperCase(),
            keyCode: key.toUpperCase().charCodeAt(0),
            which: key.toUpperCase().charCodeAt(0),
            bubbles: true,
            cancelable: true,
            view: window
        });
        window.dispatchEvent(event);
        document.dispatchEvent(event);
        let canvas = document.querySelector('canvas');
        if (canvas) canvas.dispatchEvent(event);
    }

    // The Main Loop checks the controller 60 times a second
    function gameLoop() {
        const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
        if (!gamepads) {
            requestAnimationFrame(gameLoop);
            return;
        }

        // We grab the first controller (Index 0)
        const gp = gamepads[0];

        if (gp) {
            // --- INPUT MAPPING ---

            // 1. Get Values
            // Left Stick: usually axis 0 (left/right) and 1 (up/down)
            let stickX = gp.axes[0]; 
            let stickY = gp.axes[1]; 
            
            // Triggers: typically buttons 6 (LT) and 7 (RT) on iOS/Xbox/PS
            // If these don't work, try changing 6/7 to 4/5.
            let lt = gp.buttons[6] ? gp.buttons[6].pressed : false;
            let rt = gp.buttons[7] ? gp.buttons[7].pressed : false;

            // Deadzone (ignore small stick drifts)
            let dz = 0.2;

            // 2. Logic: "RT for W" OR "Stick Up for W"
            if (rt || stickY < -dz) {
                triggerDown('w');
            } else {
                triggerUp('w');
            }

            // 3. Logic: "LT for S" OR "Stick Down for S"
            if (lt || stickY > dz) {
                triggerDown('s');
            } else {
                triggerUp('s');
            }

            // 4. Logic: Stick Left for A
            if (stickX < -dz) {
                triggerDown('a');
            } else {
                triggerUp('a');
            }

            // 5. Logic: Stick Right for D
            if (stickX > dz) {
                triggerDown('d');
            } else {
                triggerUp('d');
            }
        }
        
        // Keep the loop running
        requestAnimationFrame(gameLoop);
    }

    // Start the loop
    gameLoop();

})();