# Motion and touchpad

> Use DualSense and Switch Pro gyro and accelerometer for tilt, flick, and shake, and DualSense touchpad gestures.

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

Motion and touchpad data arrive over [WebHID](https://gamepadly.com/sdk/start/tiers#l1-webhid-one-click-desktop-chrome--edge):
DualSense and Switch Pro for motion, DualSense for the touchpad. Check `p1.getCapabilities().hasGyro` and
`hasTouchpad` before relying on them, and keep a stick or keyboard alternative.

## Motion

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

const motion = createMotionTracker();

// each frame
const m = motion.update(p1.input.getState(), dt, now);
ship.roll = m.rollNorm; // -1..1 at ±45°
if (m.justFlicked) cast(m.flickDirection); // 'up' | 'down' | 'left' | 'right'
if (m.shaking) rattle(m.shakeIntensity);

recenterButton.onclick = () => motion.recenter(); // current pose becomes neutral
```

`MotionState` includes `pitch`, `roll`, and `yaw` in radians, normalized `pitchNorm` / `rollNorm`, `angularSpeed`,
`shakeIntensity` / `shaking`, and the single-frame `justFlicked` with its `flickDirection`. Options tune the
filter (`gyroWeight`), full-tilt angle (`fullTiltRad`), and flick and shake thresholds.

## Touchpad

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

const touch = createTouchpadGestures();

// each frame
const t = touch.update(p1.input.getState(), dt, now);
if (t.active) cursor.move(t.dx, t.dy); // x / y are 0..1
if (t.justTapped) select();
if (t.justSwiped) page(t.swipeDirection);
if (t.pinchActive) zoom *= 1 + t.pinchDelta;
```

The state includes finger count and positions, per-frame and total movement, velocity, taps (`justTapped`,
`justDoubleTapped`, `justTwoFingerTapped`), swipes, pinch (`pinchScale`, `pinchDelta`), the second finger, and the
physical touchpad click (`clicked`, `justClicked`).

## Lightbar

```ts
p1.lightbar.setColor(0, 122, 255);
p1.lightbar.flash(255, 59, 48, 300); // flashes, then returns to the steady color
```
