mouseWheel event.getCount OS difference

Has anyone any insight into why getCount on the mousewheel returns differently on OSX(10.14) to Windows(10) ?
On OSX the code below will return a range from -12 to 12 dependent on how far the wheel is scrolled.
On Windows I just get -1 or 1, no matter how far the wheel is scrolled.

void draw() {
}

void mouseWheel(MouseEvent event) {
  println(event.getCount());
}

Cheers,
mala

2 Likes

I don’t have a Mac :apple:. But anyways, here’s my blind :dark_sunglasses: attempt to convert the value returned by MouseEvent::getCount() to be always -1 for scroll-up & 1 for scroll-down regardless the OS: :crossed_fingers:

int count;

void mouseWheel(final MouseEvent e) {
  count = Integer.signum(e.getCount());
  if (platform == MACOSX)  count *= -1;
  println(count);
}

void draw() {
  noLoop();
}
1 Like

@GoToLoop, Thanks I can confirm that does work on OSX.

I should have been more clear in my original question though, as I was looking to do the opposite of your solution and make the code on Windows return the same way as the code on OSX does.

It seems to me that on OSX the OS or java? Is adding all the clicks of the scroll wheel up and that is what the mouseEvent.getCount() is getting… this total then seems to be reset if there is no scroll event for a certain amount of time or the direction scrolled is changed.
I guess I can build for the Windows version some code that will add up all the 1 & -1 and run a timer so this total would be reset if no scroll event occurs within a certain time.

I would really like to understand why the same mouseEvent returns different results on a different OS, but google hasn’t pointed me in the right direction yet :slightly_frowning_face:

Hi @mala
here’s how i approached this.
this will return a range from -12 to 12. and reset if no wheel for certain amount of the time

void setup() {
  size(640, 200);
  fill(255);
  noStroke();
}
void draw() {
  background(51);
  float rectW = far * 10;
  text((int)far, width/2 + far * 10, height/2- 5);
  rect(width/2, height/2, rectW, 30);
  
  // Reset
  far = millis() - start > 1000 ? 0 : far;
}


float far = 0; //wheel traveled
float multiplier = 1f; // wheel smoothness
int start; 
void mouseWheel(MouseEvent e) {
  start = millis();
  far = constrain(far + e.getCount() * multiplier, -12, 12);
}
2 Likes

Any particular reason for using float instead of int to store millis() & getCount()? :thinking:

yes :smiley:
since getCount() is just return -1 and 1, i equipped it with smoothness (how far each wheel click should go). by tweaking the smoothness we can adjust scrolling detail
for that millis() it is already stored in int.
hope that make sense

1 Like

By smoothness you mean your constant multiplier, right?
But currently, it’s just the integer value 1 in your code.
So if we replace all float declarations w/ int:

int rectW = far * 10;
int far = 0;
int multiplier = 1;

Your sketch still works. :grinning:

Anyways, I’ve modded your sketch to use int. Here it comes: :joy_cat:

/**
 * Wheel Millis Count (v2.0.1)
 * Humayung (2019/May/19)
 * mod by GoToLoop
 *
 * https://Discourse.Processing.org/t/
 * mousewheel-event-getcount-os-difference/11378/7
 */

static final int MAX_MILLIS = 1000, MAX_FAR = 12, MULT = 1;
static final int MULT_W = 10, H = 30, OFFSET_H = -5;
static final color BG = 070, FG = -1;

int start, far;

void setup() {
  size(300, 100);
  fill(FG);
  noStroke();
}

void draw() {
  if (millis() - start >= MAX_MILLIS)  far = 0;

  final int cx = width >> 1, cy = height - H >> 1;
  final int w = far * MULT_W, tx = w + cx - int(far > 0)*MULT_W;

  background(BG);
  text(far, tx, cy + OFFSET_H);
  rect(cx, cy, w, H);
}

void mouseWheel(final MouseEvent e) {
  start = millis();
  far = constrain(e.getCount()*MULT + far, -MAX_FAR, MAX_FAR);
  if (platform == MACOSX)  far *= -1;
}
1 Like

@humayung Thank you. Your solution is pretty much what I was thinking and works on OSX and Windows.

@GoToLoop This line :

if (platform == MACOSX) far *= -1;

breaks your version of humayungs code on OSX.

1 Like

Funny, I’ve heard once Mac OS would getCount() the opposite of Windows & Linux. :apple:

But I don’t have a Mac. So just erase that if (platform == MACOSX) far *= -1; checking. :stuck_out_tongue:

I’ve found this explanation at MouseEvent::getCount(): :computer_mouse:

So Mac’s “Natural Scrolling” can be active or not: :scroll:

However, I still dunno whether “Natural Scrolling” is on by default. :grey_question:

P.S.: From what I’ve found out so far, “natural scrolling” seems to be on by default since MacOS Lion (2011): :unamused:

Scrolling is reversed by default, to act more like a touch screen device, so that content moves in the direction of finger movement on touch-pad or mouse (with the scrollbar moving in the opposite direction), rather than the scrollbar moving in the direction of finger movement (with the content moving in the opposite direction).

2 Likes

@GoToLoop Yes I’ve tested with natural scrolling on & off , but all this does is reverse the direction not change the count.
If I’m reading the java docs correctly then I think this is actually an issue with Processing on Windows, if you look at “getWheelRotation()” here -> https://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseWheelEvent.html#getWheelRotation()
it says returns the number of clicks… whilst in processing this number is always 1 never mind how far you scroll the wheel.

3 Likes

thts right! You can change it as you wish. for general purposes, I leave 1 in a float. You can change it as needed :slight_smile:

In my perspective, since on osx the getCount() returns a range (@mala said, i dont have a mac too) , map() it to desired range.
look like thats far *= -1 just flipped the direction.

so, it might be

// Assuming getCount() returns a range from -12 to 12
far = map(e.getCount(), -12, 12, -MAX_FAR, MAX_FAR);
1 Like