E
E
e_asphyx2015-11-02 18:44:36
JavaScript
e_asphyx, 2015-11-02 18:44:36

How to distinguish wheel event from touchpad and mouse?

The task is as follows: to do something like switching slides by rotating the mouse wheel. The mouse wheel rotates discretely, and it is wise to toggle on each wheel event that corresponds to wheel encoder steps. The touchpad, on the other hand, works continuously and generates a long series of wheel events, so it makes sense to accumulate movement and switch when the specified offset is exceeded. Is it possible to somehow distinguish?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Bukreev, 2015-11-02
@denisbookreev

many mice do not have a step at the wheel
some have a switch between stepped wheel rotation and smooth
in general, users are used to the fact that they regulate page scrolling, and not scripts
, for example, I don’t like it when such a feature is arranged on sites
, it is necessary to implement convenience, and not script complexity

E
e_asphyx, 2015-11-03
@e_asphyx

In short, I looked at existing solutions, was not satisfied with them and made it simpler and more reliable: median filter plus timeout:

this.wheelTimeStamp = 0;
    this.prevDelta = 0;
    this.filterBuf = [];

    this.wheel = function(e) {
      e.preventDefault();
      if(Math.abs(e.originalEvent.deltaY) <= Math.abs(e.originalEvent.deltaX)) return;
      var delta = e.originalEvent.deltaY;
      if(this.filterBuf.length == 15) {
        this.filterBuf.shift();
      }
      this.filterBuf.push(Math.abs(delta) - Math.abs(this.prevDelta));
      var tmpBuf = this.filterBuf.slice();
      tmpBuf.sort();
      var dd = tmpBuf[Math.floor(this.filterBuf.length / 2)];
      this.prevDelta = delta;
      if(dd > 0) {
        if(e.timeStamp - this.wheelTimeStamp > Phy.WheelTimeDelta) {
          this.filterBuf = [];
          this.wheelTimeStamp = e.timeStamp;
          this.scroll(delta < 0 ? "up" : "down");
        }
      }
    };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question