MouseEvent.buttons - Web APIs 编辑

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 MouseEvent.button property. The MouseEvent.buttons property indicates the state of buttons pressed during any kind of mouse event, while the 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).

  • 0 : No button or un-initialized
  • 1 : Primary button (usually the left button)
  • 2 : Secondary button (usually the right button)
  • 4 : Auxiliary button (usually the mouse wheel button or middle button)
  • 8 : 4th button (typically the "Browser Back" button)
  • 16 : 5th button (typically the "Browser Forward" button)

Example

This example logs the buttons property when you trigger a mousedown event.

HTML

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

JavaScript

let log = document.createTextNode('?');   // let log = new Text('?');

function logButtons(e) {
  log.data = `${e.buttons} (${e.type})`;  // log.nodeValue= `${e.buttons} (${e.type})`;
}

document.addEventListener('mouseup', logButtons);
document.addEventListener('mousedown', logButtons);
// document.addEventListener('mousemove', logButtons);

document.querySelector('#log').appendChild(log)

Result

Specifications

SpecificationStatusComment
UI Events
The definition of 'MouseEvent.buttons' in that specification.
Working DraftCurrent working draft
Document Object Model (DOM) Level 3 Events Specification
The definition of 'MouseEvent.buttons' in that specification.
ObsoleteInitial definition

Browser compatibility

BCD tables only load in the browser

Firefox notes

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

  • Utilities allow customization of button actions. Therefore, primary might not be the left button on the device, secondary might not be the right button, and so on. Moreover, the middle (wheel) button, 4th button, and 5th button might not be assigned a value, even when they are pressed.
  • Single-button devices may emulate additional buttons with combinations of button and keyboard presses.
  • Touch devices may emulate buttons with configurable gestures (e.g., one-finger touch for primary, two-finger touch for secondary, etc.).
  • On Linux (GTK), the 4th button and the 5th button are not supported. In addition, a mouseup event always includes the releasing button information in the buttons value.
  • On Mac OS X 10.5, the buttons attribute always returns 0 because there is no platform API for implementing this feature.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:50 次

字数:6034

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文