PCOL-ASSIST

为http://www.heyzxz.me/pcol/添加W/A/S/D/空格虚拟按键,解决iOS平板无键盘操作问题

目前為 2025-06-23 提交的版本,檢視 最新版本

// ==UserScript==
// @name         PCOL-ASSIST
// @author       葉月Hikaru
// @match        http://www.heyzxz.me/pcol/*
// @version 0.0.1.20250623133304
// @namespace https://gf.qytechs.cn/users/your-id
// @description 为http://www.heyzxz.me/pcol/添加W/A/S/D/空格虚拟按键,解决iOS平板无键盘操作问题
// ==/UserScript==

(function() {
    'use strict';

    // 按键配置(ctrl键替换cmd,+/-文本互换)
    const keysConfig = [
        // W键左右侧滚轮键(文本互换)
        { key: '+', text: '-', top: 30, left: 40 },    // 原+键显示-
        { key: 'w', text: 'W', top: 30, left: 50 },    
        { key: '-', text: '+', top: 30, left: 60 },    // 原-键显示+
        // 顶部功能键
        { key: 'space', text: 'SPACE', top: 30, left: 70 }, 
        { key: 'x', text: 'X', top: 30, left: 80 },    
        { key: 'c', text: 'C', top: 30, left: 90 },    
        // C键正下方P键
        { key: 'p', text: 'P', top: 120, left: 90 },   
        // 中下部按键
        { key: 'a', text: 'A', top: 120, left: 30 },    
        { key: 's', text: 'S', top: 120, left: 50 },    
        { key: 'd', text: 'D', top: 120, left: 70 },    
        // A键左侧ctrl键(替换原cmd键)
        { key: 'Control', text: 'ctrl', top: 120, left: 20 } // ctrl键显示ctrl
    ];

    // 创建按键函数
    function createVirtualKey(keyConfig) {
        const btn = document.createElement('button');
        btn.id = `pcol-assist-${keyConfig.key}-btn`;
        btn.textContent = keyConfig.text;
        btn.style.cssText = `
            position: fixed;
            top: ${keyConfig.top}px;
            left: ${keyConfig.left}%;
            transform: ${keyConfig.left === 50 ? 'translateX(-50%)' : 'translateX(0)'};
            width: 60px;
            height: 60px;
            font-size: ${keyConfig.text === 'SPACE' ? '16px' : '28px'};
            background-color: rgba(255, 165, 0, 0.9);
            color: white;
            border: 2px solid white;
            border-radius: 50%;
            cursor: pointer;
            z-index: 9999;
        `;

        // 模拟按键事件
        function triggerKeyEvent(type) {
            if (keyConfig.key === '+' || keyConfig.key === '-') {
                // 滚轮事件(文本互换但功能不变,+仍为上滚,-仍为下滚)
                const wheelEvent = new WheelEvent('wheel', {
                    deltaY: keyConfig.key === '+' ? -300 : 300,
                    bubbles: true,
                    cancelable: true
                });
                const gameCanvas = document.querySelector('canvas');
                gameCanvas?.dispatchEvent(wheelEvent) || document.dispatchEvent(wheelEvent);
            } else {
                // 键盘事件(ctrl键对应Control键)
                const key = keyConfig.key === 'space' ? ' ' : keyConfig.key;
                const event = new KeyboardEvent(type, {
                    key: key === ' ' ? 'Spacebar' : key,
                    code: key === ' ' ? 'Space' : 
                          key === 'Control' ? 'ControlLeft' : 
                          `Key${key.toUpperCase()}`,
                    keyCode: key === 'w' ? 87 : 
                           key === 'a' ? 65 : 
                           key === 's' ? 83 : 
                           key === 'd' ? 68 : 
                           key === 'x' ? 88 : 
                           key === 'c' ? 67 : 
                           key === 'p' ? 80 : 
                           key === 'Control' ? 17 : 32, // ctrl键keyCode:17
                    bubbles: true,
                    cancelable: true
                });
                const gameCanvas = document.querySelector('canvas');
                gameCanvas?.dispatchEvent(event) || document.dispatchEvent(event);
            }
        }

        // 绑定事件
        ['touchstart', 'mousedown'].forEach(e => btn.addEventListener(e, e => {
            e.preventDefault();
            triggerKeyEvent('keydown');
        }));
        ['touchend', 'mouseup'].forEach(e => btn.addEventListener(e, e => {
            e.preventDefault();
            triggerKeyEvent('keyup');
        }));

        document.body.appendChild(btn);
    }

    // 延迟初始化
    setTimeout(() => {
        keysConfig.forEach(createVirtualKey);
        console.log('【PCOL-ASSIST】按键已更新:ctrl键+/-文本互换');
    }, 500);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址