我正在编写一个 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.
如果需要,您可以设置全局 mousedown
和 mouseup
处理程序,以适本地设置标志,然后在任何给定时间点,您可以相对准确地确定是否/按下了哪个鼠标按钮。
关于javascript - Firefox 鼠标移动事件.which,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7938839/