Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

MouseEvent: button, coordinates, and modifier keys

MouseEvent fields expose the button associated with an event, viewport-relative pointer coordinates, and the state of Ctrl, Meta, Shift, and Alt. Distinguish button from buttons and viewport coordinates from document coordinates.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

event.button; event.clientX; event.clientY; event.ctrlKey; event.metaKey; event.shiftKey; event.altKey

MouseEvent is a browser Web API interface, not part of ECMAScript itself. Its instance properties are read-only. In an event handler, read them from the received object, such as event.clientX; MouseEvent.clientX is documentation notation, not access to a static property.

MouseEvent.button identifies the button pressed or released: 0 — main, 1 — auxiliary, 2 — secondary, 3 — usually Back, 4 — usually Forward. The main button is not necessarily physically leftmost because users can change the configuration. 0 can also mean an uninitialized state. Do not use button to determine held buttons during mousemove; use the buttons bitmask for that.

MouseEvent.clientX and MouseEvent.clientY provide horizontal and vertical coordinates in pixels relative to the viewport, not the element or the whole document. The viewport's left and top edges correspond to 0, even after the document scrolls. Do not assume coordinates must be integers.

MouseEvent.ctrlKey, MouseEvent.metaKey, MouseEvent.shiftKey, and MouseEvent.altKey are booleans describing the state of Ctrl, Meta, Shift, and Alt, respectively, when the event occurs. Meta is Command on macOS and the Windows key on Windows keyboards. The operating system may intercept Meta. On macOS, Ctrl-click opens a context menu, so ctrlKey is not detectable on that click event.

These fields and the constructor are widely supported in browsers; Node 24 being present in a repository does not establish browser API support. The example creates a synthetic mousedown with fixed data: it neither moves the pointer nor dispatches the event to an element.

Examples

const event = new MouseEvent("mousedown", {
  button: 0,
  clientX: 120,
  clientY: 80,
  ctrlKey: true,
  metaKey: false,
  shiftKey: true,
  altKey: false
});

console.log(JSON.stringify({
  button: event.button,
  x: event.clientX,
  y: event.clientY,
  ctrl: event.ctrlKey,
  meta: event.metaKey,
  shift: event.shiftKey,
  alt: event.altKey
}));

Result

{"button":0,"x":120,"y":80,"ctrl":true,"meta":false,"shift":true,"alt":false}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources