hammerjs / hammer.js

A javascript library for multi-touch gestures :// You can touch this
http://hammerjs.github.io
MIT License
24.11k stars 2.63k forks source link

Pan with middle mouse button? #1174

Open schoening opened 6 years ago

schoening commented 6 years ago

Hi, is it possible to make the middle mouse button trigger the pan gesture instead of the left one?

tolokoban commented 5 years ago

Ideally, it would be great if you could provide a "buttons" attribute on the pan event object to know which button was pressed during paning.

tolokoban commented 5 years ago

If in the code of {hammer.js} you look for this code:

      // on start we want to have the left mouse button down
      if (eventType & INPUT_START && ev.button === 0) {
        this.pressed = true;
      }

replace it with this one:

      // on start we want to have the left and middle mouse button down
      if (eventType & INPUT_START && ev.button < 2) {
        this.pressed = true;
      }

and if you look for:

      // on start we want to have the left mouse button down
      if (eventType & INPUT_START && ev.button === 0) {
        this.pressed = true;
      }

and replace it with this:

      // on start we want to have the left or middle mouse button down
      if (eventType & INPUT_START && ev.button < 2) {
        this.pressed = true;
      }

then, the pan event is emitted with the middle button too. You can check the attribute evt.srcEvent.buttons to know which button was pressed.

I don't know if this will break something else in Hammer lib. But until now, I didn't see any problem.

schoening commented 5 years ago

@tolokoban thank you, I am going to give it a try. Is the bitwise AND on purpose or did you mean to write && ?

tolokoban commented 5 years ago

It's on purpose, but it's just the original code here. It seems that the inputType can be multiple.