我想检测鼠标移动事件期间鼠标右键还是左键按下

发布于 2024-12-12 11:10:26 字数 1411 浏览 3 评论 0原文

我正在使用 HTML5,想知道在鼠标移动期间是否按住了鼠标右键或左键。鼠标右键有 event.button = 2 表示右键。左按钮的 event.button = 0。在鼠标移动期间,event.button = 0 始终。我提供了一些示例代码。我做错了什么?

<!DOCTYPE html>

<html lang="en">

<head>

<title>demo on detecting mouse buttons</title>
<meta charset="utf-8"/>

<style>
#mycanvas{ border-style:solid; width:400px; height:400px; border-width:2px;}
</style>


<script type="text/javascript">

function detectDown(event)
{
var string = "Mouse Down, event.button = " +event.button;
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.clearRect(0,0,300,20);
context.fillText(string,0,10);
}

function detectMove(event)
{
var string = "Mouse Move, event.button = " +event.button;
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.clearRect(0,30,300,20);
context.fillText(string,0,40);
}

function detectUp(event)
{
var string = "Mouse Up, event.button = " +event.button;
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.clearRect(0,60,300,20);
context.fillText(string,0,70);
}
</script>

</head>

<!-- -->

<body>

<canvas id="mycanvas"onmousedown="detectDown(event)" onmouseup="detectUp(event)" onmousemove="detectMove(event)" >
</canvas>

</body>

</html>

I am using HTML5 and want to know if the right mouse button or left mouse button is held down during a mouse move. The right mouse button has event.button = 2 for the right button. The left button has event.button = 0. During the mousemove, event.button = 0 always. I have supplied some sample code. What am I doing wrong?

<!DOCTYPE html>

<html lang="en">

<head>

<title>demo on detecting mouse buttons</title>
<meta charset="utf-8"/>

<style>
#mycanvas{ border-style:solid; width:400px; height:400px; border-width:2px;}
</style>


<script type="text/javascript">

function detectDown(event)
{
var string = "Mouse Down, event.button = " +event.button;
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.clearRect(0,0,300,20);
context.fillText(string,0,10);
}

function detectMove(event)
{
var string = "Mouse Move, event.button = " +event.button;
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.clearRect(0,30,300,20);
context.fillText(string,0,40);
}

function detectUp(event)
{
var string = "Mouse Up, event.button = " +event.button;
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.clearRect(0,60,300,20);
context.fillText(string,0,70);
}
</script>

</head>

<!-- -->

<body>

<canvas id="mycanvas"onmousedown="detectDown(event)" onmouseup="detectUp(event)" onmousemove="detectMove(event)" >
</canvas>

</body>

</html>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-12-19 11:10:26

鼠标移动事件必然与按钮无关。无论是否按下按钮,它都会报告鼠标移动。您需要在 mousedown 上创建一个标志来跟踪哪个按钮被按下;在 mouseup 上重置它。

The mouse move event is necessarily button-agnostic. It reports mouse movement, regardless of whether buttons are pressed or not. You need to create a flag on mousedown that tracks which button is down; reset it on mouseup.

顾北清歌寒 2024-12-19 11:10:26

事实上,你可以:

document.body.addEventListener('mousemove', e => console.log(`mousedown: ${(e.buttons | e.button) === 1} when mousemove`));
html, body {
  height: 100%;
}

div {
  border: 2px dashed #a10000;
  height: 100%;
  text-align: center;
}
<div>mousemove here</div>

注意:当您想运行IE8以下的代码时,请使用attachEvent绑定mousemove

Actually, you can:

document.body.addEventListener('mousemove', e => console.log(`mousedown: ${(e.buttons | e.button) === 1} when mousemove`));
html, body {
  height: 100%;
}

div {
  border: 2px dashed #a10000;
  height: 100%;
  text-align: center;
}
<div>mousemove here</div>

Notice: when you want to run the code below IE8, use attachEvent to bind mousemove.

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