Pointer input across devices
Quick answer
PointerEvent combines mouse, pen and touch interaction in one interface. pointerType identifies the device category when known, and pointerId distinguishes active pointers. Use pointerdown, pointermove, pointerup and pointercancel for gestures. Pointer cancellation must release temporary gesture state; use native click for ordinary activation so keyboard users are included.
Browser examples
Run each snippet in a fresh browser document. These are static code blocks; ByteMind browser execution is not verified. Storage examples require a test origin.
const e=new PointerEvent("pointerdown",{pointerType:"pen",pointerId:8});console.log(e.pointerType,e.pointerId);Expected console output:
pen 8These are the values explicitly assigned to the synthetic event.
const el=document.createElement("div"),active=new Set();el.addEventListener("pointerdown",e=>active.add(e.pointerId));el.addEventListener("pointercancel",e=>active.delete(e.pointerId));el.dispatchEvent(new PointerEvent("pointerdown",{pointerId:3}));el.dispatchEvent(new PointerEvent("pointercancel",{pointerId:3}));console.log(active.size);Expected console output:
0Cancellation removes the active identifier.
Common mistake
Do not rely on pointerdown alone to perform an ordinary button action. Synthetic pointer events do not prove physical touch behavior or pointer capture.
Practice
Build a small gesture tracker that records pointer IDs on down and removes them on up or cancel.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pointer input across devices — Knowledge Check
Apply the concept and predict the browser example behavior.
Which property helps distinguish simultaneous active pointers?
What should a gesture handler do on pointercancel?
Which event is usually best for ordinary native button activation?
What is the exact console output in Pointer input across devices example 1?
const e=new PointerEvent("pointerdown",{pointerType:"pen",pointerId:8});console.log(e.pointerType,e.pointerId);What is the exact console output in Pointer input across devices example 2?
const el=document.createElement("div"),active=new Set();el.addEventListener("pointerdown",e=>active.add(e.pointerId));el.addEventListener("pointercancel",e=>active.delete(e.pointerId));el.dispatchEvent(new PointerEvent("pointerdown",{pointerId:3}));el.dispatchEvent(new PointerEvent("pointercancel",{pointerId:3}));console.log(active.size);