javascript - Firefox 鼠标移动事件.which

标签 javascript drawing html5-canvas

我正在编写一个 HTML5 Canvas 绘图应用程序,我需要能够判断在 mousemove 事件期间是否按下了鼠标左键。在 Chrome 上,这有效:

if (event.which == 1) { <do stuff> }

但在 FireFox 中,event.which 始终为 1,无论是否按下按钮。

如何在 mousemove 事件期间检测鼠标按钮是否被按下?

最佳答案

简而言之,你不能。 MDN,例如 explains :

When a mouse button event occurs, a number of additional properties are available to determine which mouse buttons were pressed and the location of the mouse pointer. The event's button property can be used to determine which button was pressed, where possible values are 0 for the left button, 1 for the middle button and 2 for the right button. If you've configured your mouse differently, these values may be different.

...

The button and detail properties only apply to the mouse button related events, not mouse movement events. For the mousemove event, for example, both properties will be set to 0.

如果需要,您可以设置全局 mousedownmouseup 处理程序,以适本地设置标志,然后在任何给定时间点,您可以相对准确地确定是否/按下了哪个鼠标按钮。

关于javascript - Firefox 鼠标移动事件.which,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7938839/

相关文章:

javascript - 将 CSS Transition 与点击事件联系起来

javascript - 带有自己的 headless 浏览器的 Heisenbug

javascript - 使用 wheel event a increments 遍历数组

javascript - 试图在 Canvas 中实现的CSS动画

javascript - React redux,操作有效负载无法在 try/catch block 中看到变量

c# - .NET 的社交网络有向图库

c# - 在 Canvas 上显示 DrawingVisual

iphone - 使用 OpenGL 在 iPad 上绘图应用程序

javascript - 在饼图切片的外部区域添加边框

javascript - 如何处理html5的canvas图片加载异步?