jQuery: Detecting pressed mouse button during mousemove event

前端 未结 7 1490
心在旅途
心在旅途 2020-11-30 05:22

I tried to detect which mouse button -if any- is the user pressing during a mousemove event under jQuery, but I\'m getting ambiguous results:

no button press         


        
相关标签:
7条回答
  • 2020-11-30 06:00

    Those variables are updated when the mousedown event (amongst others) fires; you're seeing the value that remains from that event. Note that they are properties of that event, not of the mouse itself:

    Description: For key or button events, this attribute indicates the specific button or key that was pressed.

    There is no value for "no button press", because the mousedown event will never fire to indicate that there's no button being pressed.

    You'll have to keep your own global [boolean] variable and toggle it on/off on mousedown/mouseup.

    • This will only work if the browser window had focus when the button was pressed, which means a focus-switch between the user depressing the mouse button and your mousemove event firing will prevent this from working properly. However, there's really not much you can do about that.
    0 讨论(0)
提交回复
热议问题