体感与触控板
体感和触控板数据通过 WebHID 获取:体感支持 DualSense 和 Switch Pro,触控板支持 DualSense。使用前先检查 p1.getCapabilities().hasGyro 和 hasTouchpad,并保留摇杆或键盘的替代操作。
import { createMotionTracker } from '@gamepadly/sdk';
const motion = createMotionTracker();
// 每帧const m = motion.update(p1.input.getState(), dt, now);ship.roll = m.rollNorm; // ±45° 时为 -1..1if (m.justFlicked) cast(m.flickDirection); // 'up' | 'down' | 'left' | 'right'if (m.shaking) rattle(m.shakeIntensity);
recenterButton.onclick = () => motion.recenter(); // 当前姿态成为中立位置MotionState 包含以弧度表示的 pitch、roll 和 yaw,归一化的 pitchNorm / rollNorm,angularSpeed,shakeIntensity / shaking,以及只持续一帧的 justFlicked 和对应的 flickDirection。选项可以调整滤波(gyroWeight)、满倾斜角度(fullTiltRad),以及甩动和摇晃的阈值。
import { createTouchpadGestures } from '@gamepadly/sdk';
const touch = createTouchpadGestures();
// 每帧const t = touch.update(p1.input.getState(), dt, now);if (t.active) cursor.move(t.dx, t.dy); // x / y 范围 0..1if (t.justTapped) select();if (t.justSwiped) page(t.swipeDirection);if (t.pinchActive) zoom *= 1 + t.pinchDelta;状态包括手指数量和位置、每帧和累计移动量、速度、轻点(justTapped、justDoubleTapped、justTwoFingerTapped)、滑动、双指缩放(pinchScale、pinchDelta)、第二根手指,以及触控板的物理按压(clicked、justClicked)。
p1.lightbar.setColor(0, 122, 255);p1.lightbar.flash(255, 59, 48, 300); // 闪烁后恢复为常亮颜色