# 体感与触控板

> 用 DualSense 和 Switch Pro 的陀螺仪与加速度计实现倾斜、甩动和摇晃，以及 DualSense 触控板手势。

Source: https://gamepadly.com/sdk/zh/guides/motion-and-touchpad

体感和触控板数据通过 [WebHID](https://gamepadly.com/sdk/zh/start/tiers#l1webhid点一次授权桌面版-chrome--edge) 获取：体感支持 DualSense 和 Switch Pro，触控板支持 DualSense。使用前先检查 `p1.getCapabilities().hasGyro` 和 `hasTouchpad`，并保留摇杆或键盘的替代操作。

## 体感

```ts
import { createMotionTracker } from '@gamepadly/sdk';

const motion = createMotionTracker();

// 每帧
const m = motion.update(p1.input.getState(), dt, now);
ship.roll = m.rollNorm; // ±45° 时为 -1..1
if (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`），以及甩动和摇晃的阈值。

## 触控板

```ts
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..1
if (t.justTapped) select();
if (t.justSwiped) page(t.swipeDirection);
if (t.pinchActive) zoom *= 1 + t.pinchDelta;
```

状态包括手指数量和位置、每帧和累计移动量、速度、轻点（`justTapped`、`justDoubleTapped`、`justTwoFingerTapped`）、滑动、双指缩放（`pinchScale`、`pinchDelta`）、第二根手指，以及触控板的物理按压（`clicked`、`justClicked`）。

## 灯条

```ts
p1.lightbar.setColor(0, 122, 255);
p1.lightbar.flash(255, 59, 48, 300); // 闪烁后恢复为常亮颜色
```
