--- title: MouseEvent.buttons slug: Web/API/MouseEvent/buttons translation_of: Web/API/MouseEvent/buttons ---
{{APIRef("DOM Events")}}

The MouseEvent.buttons read-only property indicates which buttons are pressed on the mouse (or other input device) when a mouse event is triggered.

Each button that can be pressed is represented by a given number (see below). If more than one button is pressed, the button values are added together to produce a new number. For example, if the secondary (2) and auxilary (4) buttons are pressed simultaneously, the value is 6 (i.e., 2 + 4).

Note: Do not confuse this property with the {{domxref("MouseEvent.button")}} property. The {{domxref("MouseEvent.buttons")}} property indicates the state of buttons pressed during any kind of mouse event, while the {{domxref("MouseEvent.button")}} property only guarantees the correct value for mouse events caused by pressing or releasing one or multiple buttons.

Syntax

var buttonsPressed = instanceOfMouseEvent.buttons

Return value

A number representing one or more buttons. For more than one button pressed simultaneously, the values are combined (e.g., 3 is primary + secondary).

Example

This example logs the buttons property when you trigger a {{Event("mousedown")}} event.

HTML

<p>Click anywhere with one or more mouse buttons.</p>
<p id="log">buttons: </p>

JavaScript

let button = document.querySelector('#button');
let log = document.createTextNode('?');
document.addEventListener('mousedown', logButtons);

function logButtons(e) {
  log.data = `${e.buttons} (mousedown)`;
}
document.addEventListener('mouseup', unpress);
function unpress(e) {
  log.data = `${e.buttons} (mouseup)`
}
document.querySelector('#log').appendChild(log)

Result

{{EmbedLiveSample("Example")}}

Specifications

Specification Status Comment
{{SpecName('DOM3 Events','#widl-MouseEvent-buttons','MouseEvent.buttons')}} {{Spec2('DOM3 Events')}} Initial definition

Browser compatibility

{{Compat("api.MouseEvent.buttons")}}

Firefox notes

Firefox supports the buttons attribute on Windows, Linux (GTK), and macOS with the following restrictions:

See also