Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active April 18, 2026 00:05
Show Gist options
  • Select an option

  • Save lewdev/8ce2ca0f98484f6d6d6d463c0b571a1c to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/8ce2ca0f98484f6d6d6d463c0b571a1c to your computer and use it in GitHub Desktop.
🖱️ Mouse Up & Down for Left, Middle, Right, & other clicks
<script>
const showCoord = (eventName, pos, e) => {
o.innerHTML = `${eventName} "${pos}" at x: ${e.x}, y: ${e.y}`;
e.preventDefault()
};
const onMouseEvent = eventName => e => [
e => showCoord(eventName, "Left", e),
e => showCoord(eventName, "Middle", e),
e => showCoord(eventName, "Right", e),
e => showCoord(eventName, "Back", e),
e => showCoord(eventName, "Forward", e),
][e.button](e);
addEventListener('mousedown', onMouseEvent("Down"));
addEventListener('mouseup', onMouseEvent("Up"));
addEventListener('mousemove', onMouseEvent("Move"));
addEventListener('mouseenter', onMouseEvent("Enter"));
addEventListener('mouseout', onMouseEvent("Out"));
addEventListener('dblclick', onMouseEvent("Double Click")); //only works with Left click
//prevent context menu from appearing
addEventListener('contextmenu', e => e?.cancelable && e.preventDefault());
</script>
Move or click with Left, Middle, or Right mouse buttons on the screen to see which event was triggered and the x/y coordinate it happened at.
<p id=o></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment