Running List of Mouse Clicks

I have some code for a running list of mouse click events, where the events can be deleted from the list after a delay. I keep a list of mouse coords and expiration times. I draw circles if the expiration time is > millis(), and delete the stored event if it isn’t.

Here’s the code:

import java.util.Iterator;

ArrayList<TimedLocation> timeLocsArray;
int startTime;
int delay = 4000; // in millis
int count = 0;


public void setup() {
  size(640, 480);
  timeLocsArray = new ArrayList<TimedLocation>();
  startTime = millis();
}

public void draw() {
  background(color(192));
  runTimeArray();
}

public void mouseClicked() {
  int randomShift = (int) random(delay/4) - delay/8;
  timeLocsArray.add(new TimedLocation(mouseX, mouseY, millis() + delay + randomShift));
  println("-- click, delay = "+ (delay + randomShift));
}

public void keyPressed() {
  if (key == 'm') {
    Iterator<TimedLocation> iter = timeLocsArray.iterator();
    int i = 0;
    println("-- Stored events: "+ timeLocsArray.size());
    while (iter.hasNext()) {
      TimedLocation tl = iter.next();
      println("-- TimedLocation "+ i++ +": ", tl.x, tl.y, tl.stopTime);
    }
  }
}

public void runTimeArray() {
  int currentTime = millis();
  timeLocsArray.forEach(tl -> {
    tl.setStale(tl.stopTime() < currentTime);
    if (!tl.isStale()) {
      drawCircle(tl.getX(), tl.getY());
    }
  }
  );
  timeLocsArray.removeIf(TimedLocation::isStale);
}

public void drawCircle(int x, int y) {
  fill(color(233, 220, 199, 128));
  noStroke();
  circle(x, y, 60);
}

// class to track mouse click location and expiration time (stopTime)
// track in an array, when millis() > stopTime, call setStale(true) 
// then delete stale elements from the array
public class TimedLocation {
  private int x;
  private int y;
  private int stopTime;
  private boolean isStale;

  public TimedLocation(int x, int y, int stop) {
    this.x = x;
    this.y = y;
    this.stopTime = stop;
    this.isStale = false;
  }

  public int getX() {
    return this.x;
  }

  public int getY() {
    return this.y;
  }
  
  public int stopTime() {
    return this.stopTime;
  }

  public boolean isStale() {
    return this.isStale;
  }
  public void setStale(boolean stale) {
    this.isStale = stale;
  }
}

And actually, the code seems to work really well at tracking and deleting events. However, sometimes I find myself clicking on the display window and the click is not detected by Processing – audible click of the mouse button, but no response. I can’t bounce around clicking really fast and have all the audible clicks get detected and show up as pale yellow circles, which might be nice. I can apparently click repeatedly in one place and expect the click to be detected.

I have tried a few things:
Closing all other apps, especially the browser.
In MacOS, set mouse Tracking speed to fast, set Double click speed to fast.
Put a new battery in my Logitech mouse and check its settings.
Use the touchpad on my laptop.
Use a drawing tablet.

None of these “solutions” has changed the basic problem: not all clicks are captured. Clicking on one spot, apparently gets clicks. Darting around and clicking, misses clicks.

Maybe I just need a new mouse, but other input devices seem to have the same problem.

Is there some setting I have missed? Or is this a feature?

REVISION: The drawing tablet seems to get all the clicks when I dart around. Nothing else does.

Anyhow, I hope the code is useful.

cheers,

Paul

And lo! Once I have posted to the Forum the answer to my problem arrives with a Head Smack.

I should call mousePressed(), not mouseClicked().

Remedied. And again, I hope the code it useful to someone besides me.

// Paul

1 Like