gpt4 book ai didi

javascript - 是否可以获得鼠标按钮 4、5 等?

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:09 26 4
gpt4 key购买 nike

简单地说,有没有什么方法可以在 JavaScript 中检测额外的鼠标按钮按下?它没有记录在其余的鼠标输入中,所以我猜它不在标准实现中。

有什么方法可以启用额外的鼠标按钮吗?

最佳答案

是的,你可以这样做,检查 MouseEvent.button ,请参见下面的示例,该示例将检测 3 和 4 个按钮的点击。

Some pointing devices provide or simulate more buttons. To represent such buttons, the value must be doubled for each successive button (in the binary series 8, 16, 32, ... ) as mentioned in https://www.w3.org/TR/DOM-Level-3-Events/#events-mouseevents

var whichButton = function (e) {
// Handle different event models
var e = e || window.event;
var btnCode;

if ('object' === typeof e) {
btnCode = e.button;

switch (btnCode) {
case 3:
console.log('Browser Back button clicked.');
break;

case 4:
console.log('Browser Forward button clicked.');
break;

default:
console.log('Unexpected code: ' + btnCode);
}
}
}
<button onmouseup="whichButton(event);" oncontextmenu="event.preventDefault();">Click with mouse...</button>

关于javascript - 是否可以获得鼠标按钮 4、5 等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com