Nucleus
Input

Pen and touch input

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

View as Markdown

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

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 typeNew

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

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

PlatformFingerPenPressure
WindowsPointerType.TouchPointerType.Stylus / EraserReported when the digitizer supports it
Linux (Wayland and X11)PointerType.TouchPointerType.Stylus / EraserAlways 1f for a finger, not reported for a pen
macOS 27 and laterPointerType.TouchPointerType.Stylus / EraserAlways 1f for a finger, not reported for a pen
Earlier macOSNo touchscreenPointerType.Stylus / EraserNot 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

Was this page helpful?
Edit on GitHub

Last updated on