Mouse Multi-Button Events: Mapping Left, Right, Middle & Auxiliary Buttons in Web Apps
The W3C Pointer Events standard assigns dedicated integer indexes to e.button: 0 = Primary (Left), 1 = Auxiliary (Middle Wheel Click), 2 = Secondary (Right), 3 = Back (Thumb 1), and 4 = Forward (Thumb 2). For simultaneous multi-button presses, the e.buttons property provides a binary bitmask (1 | 2 | 4 | 8 | 16). Auxiliary button clicks must be captured using the auxclick event listener with e.preventDefault() to override browser history traversal.
Step 1: The W3C Button Specification: button vs buttons Bitmask
Mouse and Pointer events provide two separate properties for identifying pressed buttons. e.button identifies the exact button that changed state (pressed or released) for that specific event. In contrast, e.buttons is a bitfield representing all buttons currently held down simultaneously.
/* W3C PointerEvent Mapping */
Button Index (e.button) Bitmask Flag (e.buttons)
0: Primary (Left click) 1 (1 << 0)
1: Auxiliary (Middle click) 4 (1 << 2) <- Note: bitmask 4, not 2!
2: Secondary (Right click) 2 (1 << 1)
3: Fourth (Thumb Back) 8 (1 << 3)
4: Fifth (Thumb Forward) 16 (1 << 4)
Step 2: Capturing Non-Primary Clicks with the auxclick Event
Historically, the standard click event only fired for primary (left) button presses. The W3C introduced auxclick to handle non-primary mouse clicks (middle click and side thumb buttons) consistently without triggering default browser actions like autoscroll or page navigation.
const element = document.querySelector('.interactive-canvas');
// Standard left click
element.addEventListener('click', (e) => {
console.log('Primary Left Click:', e.clientX, e.clientY);
});
// Non-primary clicks (middle click, side buttons)
element.addEventListener('auxclick', (e) => {
// Prevent browser autoscroll or history back/forward
e.preventDefault();
if (e.button === 1) {
console.log('Middle Mouse Button (Wheel Click) Triggered');
} else if (e.button === 3) {
console.log('Side Thumb Button 1 (Back) Intercepted');
} else if (e.button === 4) {
console.log('Side Thumb Button 2 (Forward) Intercepted');
}
});
Step 3: Intercepting Side Thumb Buttons Without Page Navigation Jumps
By default, Windows and macOS map Mouse Buttons 3 and 4 to "History Back" and "History Forward". When users press these buttons inside a web application, the browser may unexpectedly navigate away from the page. Intercepting both `pointerdown` and `auxclick` with `e.preventDefault()` halts this default behavior.
// Intercept side buttons globally to prevent navigation loss
window.addEventListener('pointerdown', (e) => {
if (e.button === 3 || e.button === 4) {
e.preventDefault();
e.stopPropagation();
handleCustomAppAction(e.button === 3 ? 'undo' : 'redo');
}
}, { passive: false });
window.addEventListener('mouseup', (e) => {
if (e.button === 3 || e.button === 4) {
e.preventDefault();
}
});
Step 4: Tracking Simultaneous Multi-Button Combos (Chording)
Gaming and precision CAD applications frequently require button chording: holding the right mouse button to aim or pan while clicking the left button to select or shoot. Inspect the binary bitmask `e.buttons` using bitwise AND operators.
canvas.addEventListener('pointermove', (e) => {
const isLeftHeld = (e.buttons & 1) !== 0;
const isRightHeld = (e.buttons & 2) !== 0;
const isMiddleHeld = (e.buttons & 4) !== 0;
if (isLeftHeld && isRightHeld) {
// Both Left + Right held simultaneously (Chording)
executeDualAxisOrbit(e.movementX, e.movementY);
} else if (isRightHeld) {
executeCameraRotate(e.movementX, e.movementY);
} else if (isMiddleHeld) {
executeCanvasPan(e.movementX, e.movementY);
}
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why does my middle-click not trigger a standard click event?
The W3C specification dictates that `click` fires exclusively for primary (left) button activations. For middle-click (button 1) and side buttons (buttons 3 and 4), you must attach an `auxclick` event listener.
Can JavaScript prevent the browser from going back when pressing mouse button 3 (thumb back)?
Yes, in most desktop browsers calling `e.preventDefault()` inside both a `pointerdown` and `auxclick` listener will successfully stop the browser from traversing backward in history.
What is the difference between e.button and e.buttons in mouse events?
`e.button` indicates which specific button caused the current event (pressed or released), ranging from 0 to 4. `e.buttons` is a cumulative binary bitmask (1, 2, 4, 8, 16) showing which buttons are currently depressed right now.
Can web applications detect more than 5 mouse buttons (e.g. MMO mice with 12 buttons)?
Native browser APIs only standardize buttons 0 through 4. MMO mice with 12 side buttons typically use companion software (Razer Synapse, Logitech G HUB) that binds those extra buttons to keyboard keys (like NumPad 1-9) or macros, which browsers then detect as standard `KeyboardEvent` signals.
What is the best event to use for modern mouse input: MouseEvent or PointerEvent?
`PointerEvent` is the modern standard. It extends `MouseEvent` while unifying input handling across physical mice, touchscreens, and digital styluses with additional pressure, tilt, and multi-button bitmask support.