# Pen and touch input

Read touchscreen and pen contacts, with pressure, from Compose pointer events.

Touchscreen contacts reach Compose as pointers of type `PointerType.Touch`, and a tablet pen as
`PointerType.Stylus`, or `PointerType.Eraser` for its eraser end. You read them with the same
`pointerInput` modifier as mouse input, and you add nothing beyond `nucleus-application`.

## Draw with pressure

```kotlin
Modifier.pointerInput(Unit) {
    awaitPointerEventScope {
        while (true) {
            val change = awaitPointerEvent().changes.first()
            if (change.type == PointerType.Touch || change.type == PointerType.Stylus) {
                addPoint(change.position, change.pressure)
                change.consume()
            }
        }
    }
}
```

Gesture detectors, `consume()` and hit testing treat these contacts like any other Compose
pointer.

## Read the pointer type 

`PointerInputChange.type` names the tool behind each event, so one handler can draw with the pen
tip and erase with the other end:

```kotlin
Modifier.pointerInput(Unit) {
    awaitPointerEventScope {
        while (true) {
            val change = awaitPointerEvent().changes.first()
            if (!change.pressed) continue
            when (change.type) {
                PointerType.Stylus -> addPoint(change.position, change.pressure)
                PointerType.Eraser -> erase(change.position)
                else -> Unit
            }
        }
    }
}
```

A pen that touches the surface presses the primary button. A pen hovering in range arrives as
`Move` events with no button pressed. Compose sends `Enter` and `Exit` for the mouse only, so a
pen hover never produces them.

## Platform support

| Platform | Finger | Pen | Pressure |
| --- | --- | --- | --- |
| Windows | `PointerType.Touch` | `PointerType.Stylus` / `Eraser` | Reported when the digitizer supports it |
| Linux (Wayland and X11) | `PointerType.Touch` | `PointerType.Stylus` / `Eraser` | Always `1f` for a finger, not reported for a pen |
| macOS 27 and later | `PointerType.Touch` | `PointerType.Stylus` / `Eraser` | Always `1f` for a finger, not reported for a pen |
| Earlier macOS | No touchscreen | `PointerType.Stylus` / `Eraser` | Not reported |

- Treat `pressure == 1f` as "average or unknown". Most consumer touchscreens report `1f`.
- `PointerInputChange` carries no tilt or barrel rotation.
- A finger dragged on the title bar moves the window on Windows, Linux and macOS. On Linux, a finger on a
  window edge resizes it.
- On Linux, the gaps between `uptimeMillis` values follow the times the screen sampled the
  contact, not the times the events reached the app, so velocity and fling stay correct after a
  slow frame.

## Touch on macOS

On macOS 27 and later, the MacBook Pro with PROLED XDR Display and an iPad used as a Sidecar
display send fingers as `PointerType.Touch` pointers. Multi-touch, finger drags and Compose's own
pinch and rotation (`detectTransformGestures`) work as on Windows. You need no code and no
`Info.plist` key.

AppKit gives touches only to gesture recognizers. Nucleus attaches one to each window, and it
turns off AppKit's mouse emulation for that window: without it, a tap becomes a click, a pan a
scroll, a long press a right click, and a pinch a magnify and rotate. To keep the emulation in
every window, start the JVM with `-Dnucleus.tao.directTouch=false`.

- Touch and mouse never mix. A finger that lands ends a trackpad pan or rotation in progress.
  While a finger is down, a cursor move is ignored, and a click or scroll cancels the touch.
- A cancelled contact releases without a click, here and on Windows.
- A touch outside a native popup dismisses it.
- Fingers on two windows are tracked at the same time.
- Native popup panels and `NativeView` children keep the mouse emulation.
- Covered by tests with injected touch events, not yet on hardware: a finger that leaves the
  window during a drag, whether a touch activates a background window, and the native title bar
  strip with the traffic lights.

## What's next

- [Trackpad gestures](/docs/input/trackpad-gestures): pans, pinch-to-zoom and rotation.
- [Linux and Wayland](/docs/windows/linux-wayland): compositor behavior on Linux.
- [Accessibility](/docs/input/accessibility): expose your UI to screen readers.
