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.20250623125623
// @namespace https://gf.qytechs.cn/users/your-id
// @description 为http://www.heyzxz.me/pcol/添加W/A/S/D/空格虚拟按键,解决iOS平板无键盘操作问题
// ==/UserScript==

(function() {
    'use strict';

    // 按键配置(移除空格键,仅保留W/A/S/D)
    const keysConfig = [
        { key: 'w', text: 'W', top: 30, left: 50 },
        { key: 'a', text: 'A', top: 120, left: 30 },
        { key: 's', text: 'S', top: 120, left: 50 },
        { key: 'd', text: 'D', top: 120, left: 70 }
    ];

    // 创建虚拟按键的函数
    function createVirtualKey(keyConfig) {
        const btn = document.createElement('button');
        btn.id = `pcol-assist-${keyConfig.key}-btn`;
        btn.textContent = keyConfig.text;
        btn.style.cssText = `
            position: fixed;
            width: 60px;
            height: 60px;
            font-size: 28px;
            background-color: rgba(255, 165, 0, 0.9);
            color: white;
            border: 2px solid white;
            border-radius: 50%;
            cursor: pointer;
            z-index: 99999;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
            display: flex;
            align-items: center;
            justify-content: center;
        `;

        // 自适应屏幕布局
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;
        btn.style.top = `${keyConfig.top * (screenHeight / 300)}px`;
        btn.style.left = `${keyConfig.left * (screenWidth / 100)}%`;

        // 模拟按键事件
        function triggerKeyEvent(type) {
            const key = keyConfig.key;
            const event = new KeyboardEvent(type, {
                key, code: `Key${key.toUpperCase()}`,
                keyCode: key === 'w' ? 87 : key === 'a' ? 65 : key === 's' ? 83 : 68,
                bubbles: true,
                cancelable: true
            });
            const gameCanvas = document.querySelector('canvas');
            gameCanvas?.dispatchEvent(event) || document.dispatchEvent(event);
        }

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

        document.body.appendChild(btn);
    }

    // 延迟500ms创建按键
    setTimeout(() => {
        keysConfig.forEach(createVirtualKey);
        console.log('【PCOL-ASSIST】W/A/S/D按键已加载,空格键已移除');
    }, 500);
})();

QingJ © 2025

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