Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

PointerEvent: pointer identity and primary status

`pointerId` links events from the same active pointer; `isPrimary` tells you whether that pointer is primary for its input type.

Version: Current web platform; runtime support varies

Syntax

// Pointer Events Web API; no ECMAScript 2024 requirement
event.pointerId  // number, read-only
event.isPrimary  // boolean, read-only

PointerEvent.pointerId/isPrimary refers to two separate read-only instance properties. pointerId is a number that lets you match events from the same active pointer and distinguish it from other active pointers, such as a second touch contact. It is not a permanent device ID: don't infer the device type from the number or assume that it will persist across interactions or never be reused. In browser-generated events, -1 indicates no pointing device, for example in a keyboard-triggered click.

isPrimary is a boolean. true identifies the primary pointer for that pointerType, not the primary button. The mouse pointer is primary; for touch and pen, the first pointer of that type to become active when no other pointer of that type is active is primary. A mouse and a touch contact can therefore both have isPrimary === true. When the primary touch ends, a remaining active contact does not automatically become primary.

To track multiple gestures, store state in a Map keyed by pointerId on pointerdown, look it up on pointermove, and remove it on pointerup or pointercancel. If you need just one pointer across all input types, remember the selected ID; filtering on isPrimary alone is not enough. These properties are part of a Web API, not ECMAScript; check support in your target runtime.

Examples

<!doctype html>
<meta charset="utf-8">
<title>PointerEvent example</title>
<pre id="output"></pre>
<script>
  const events = [
    new PointerEvent("pointerdown", { pointerType: "touch", pointerId: 7, isPrimary: true }),
    new PointerEvent("pointerdown", { pointerType: "touch", pointerId: 8, isPrimary: false }),
    new PointerEvent("pointermove", { pointerType: "touch", pointerId: 7, isPrimary: true }),
    new PointerEvent("pointermove", { pointerType: "mouse", pointerId: 12, isPrimary: true }),
  ];

  const lines = [];
  for (const event of events) {
    lines.push(`${event.type} ${event.pointerType} ${event.pointerId} ${event.isPrimary}`);
  }
  document.getElementById("output").textContent = lines.join("\n");
</script>

Result

pointerdown touch 7 true
pointerdown touch 8 false
pointermove touch 7 true
pointermove mouse 12 true

Save the example as an HTML file and open it in a browser with `PointerEvent` support. The output appears on the page. ID `7` links two touch events, `8` identifies a different contact, and both the mouse and the first touch have `true`. The code reads explicitly supplied values from synthetic events. It neither dispatches the events nor tests how the browser assigns values for real input.

Related lessons

  • JavaScript · Lesson 35 · Intermediate · Version: 3

Sources