Calculate clicks per second

Asked

Viewed 1,803 times

2

How do I calculate how many clicks were given in 1 second?

  • I assume you are thinking of solutions using Jnativehook, as in your other question. Or else the Robot. Correct?

  • I just want to know how to calculate, I know that no matter where I’m going to use, it’s going to be the same... it’s only going to change when it’s time to increase the click...

1 answer

1

First you should capture click-through events and set a timestamp for each. In the case of events that are inherited from java.awt.event.InputEvent, this timestamp will be present in the method getWhen().

After collecting a list of clicks, what you should do is something more or less like this, depending on how you want to measure (using java 8 or higher):

public double mediaDeCliquesPorSegundo(Collection<? extends InputEvent> events) {
    return mediaPorSegundo(events.stream().map(InputEvent::getWhen).collect(Collectors.toList()));
}

public long cliquesNoUltimoSegundo(Collection<? extends InputEvent> events) {
    return noUltimoSegundo(events.stream().map(InputEvent::getWhen).collect(Collectors.toList()));
}

public double mediaPorSegundo(Collection<Long> events) {
    long max = events.stream().reduce(Long::max).orElse(0L);
    long min = events.stream().reduce(Long::min).orElse(0L);
    if (max == min) {
        throw new IllegalArgumentException("Precisa de pelo menos dois eventos em tempos diferentes para poder calcular");
    }
    return events.size() / (double) (max - min);
}

public long noUltimoSegundo(Collection<Long> events) {
    long max = events.stream().reduce(Long::max).orElse(0L);
    return events.stream().filter(x -> x > max - 1000L).count();
}

If your event class is not subclass of java.awt.event.InputEvent, you must use one of the methods mediaDeCliquesPorSegundo and cliquesNoUltimo. If it is not, after producing in some way a Collection<Long> containing timestamps, you must use one of the methods mediaPorSegundo or noUltimoSegundo.

If your event class is something specific like MeuEvento with a method getTimestamp, you can do something similar to the methods mediaDeCliquesPorSegundo and cliquesNoUltimo to obtain the Collection<Long>:

public double mediaDeMeusCliquesPorSegundo(Collection<? extends MeuEvento> events) {
    return mediaPorSegundo(events.stream().map(MeuEvento::getTimestamp).collect(Collectors.toList()));
}

public long meusCliquesNoUltimoSegundo(Collection<? extends MeuEvento> events) {
    return noUltimoSegundo(events.stream().map(MeuEvento::getTimestamp).collect(Collectors.toList()));
}
  • And if not to use with an inputEvent? I just want to know how you calculate...

  • @pauloabr Edited. See if clarified for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.