Detect click vs. touch in JavaScript [duplicate]
Asked Answered
B

1

6

I know you can detect if the browser is running on a device with a touchscreen as per this answer: https://mcmap.net/q/42184/-what-39-s-the-best-way-to-detect-a-39-touch-screen-39-device-using-javascript

But, I know some devices both have a touchscreen and a mouse. Can I detect the difference between those types of touch? Essentially I want to distinguish between 3 different events: onClick (which fires on click or on touch), onTouch (which fires only on touch), onMouseClick (which fires only on click).

Bonefish answered 24/1, 2022 at 0:46 Comment(1)
Review pointerType on PointerEventMaxim
S
12

You can use the pointerType property on PointerEvent to detect the input type ("mouse", "touch", or "pen"):

element.addEventListener('pointerup', (event) => {
  if (event.pointerType === "mouse") {}
  if (event.pointerType === "touch") {}
  if (event.pointerType === "pen") {}
});

Note - I have used pointerup event to match the behavior of click, but you can also use pointerdown.

If you want specific events for each type of click, you can create custom events:

const mouse = new Event("mouseclick");
const touch = new Event("touch");
document.addEventListener("pointerup", ({ pointerType, target }) => {
  if (pointerType === "mouse") target.dispatchEvent(mouse);
  if (pointerType === "touch") target.dispatchEvent(touch);
});

const someElement = document.querySelector(".box");
someElement.addEventListener("mouseclick", () => {
  console.log("Clicked with mouse");
});
someElement.addEventListener("touch", () => {
  console.log("Touched with mobile device");
});
someElement.addEventListener("click", () => {
  console.log("Clicked by any device");
});
.box {
  position: absolute;
  inset: 2em;
  background: darkred;
  color: lightcoral;
  padding: 1em;
}
<div class="box">A box with custom events</div>

Tested for mobile on iOS 17 but this should work on any browser.

Note that if you are using React or another framework, there may be different ways to create custom events.

In React, for example, you may implement these events with a reusable function:

function pointerEvents(
  listeners: {
    onMouse?: (event: PointerEvent) => void;
    onTouch?: (event: PointerEvent) => void;
  }
) {
  return {
    onPointerUp(event) {
      const { pointerType } = event;
      if (pointerType === "mouse") {
        listeners.onMouseClick?.(event); 
      } else if (pointerType === "touch") {
          listeners.onTouch?.(event);
      }
    },
  };
}

function MyComponent() {
  return (
    <button
      className="something"
      {...pointerEvents({
        onMouse: (e) => { /* … */ },
        onTouch: (e) => { /* … */ },
      })}
    >
      Click me
    </button>
  );
}
Shea answered 24/1, 2022 at 1:10 Comment(1)
Is there something similar for click and dblclick events?Hobbledehoy

© 2022 - 2024 — McMap. All rights reserved.