Is there any way to call my library's keyEvent () method before the sketch's keyPressed () method?

As it says in the title, I would like to know if there is any way to be able to capture imputs made on the keyboard and process logic before Processing invokes the keyPressed() method, on the end user’s side. I am building a utility library (in fact I already finished it) and I noticed in my tests that, if the end user wants to print (toString()) the values ​​processed inside my library to the console, via the keyPressed() method (which makes the most sense in the context of my library), what happens is that the printed result is simply the state of the variables at a time before they are actually updated. This will lead the end user to a total mistake in relation to the current state of the values ​​of my library, even though the final behavior of the program is correct, since in this case it does not matter if Processing first invokes the keyboard events from the sketch or from my library.

Here’s a mcve of the mentioned problem:
Lib.java file

import processing.core.PApplet;
import processing.event.KeyEvent;

public class Lib {
  private PApplet p;
  private char key;
  private boolean state;

  public Lib(PApplet p) {
    this.p = p;
    p.registerMethod("keyEvent", this);
  }

  public void keyEvent(KeyEvent e) {
    if (e.getAction() == KeyEvent.PRESS) {
      key = e.getKey();
      state = !state;
    }
  }

  public boolean isActive() {
    return state;
  }
  
  public char getKey(){
    return key;
  }

  public String toString() {
    return String.format("key: %s | state: %s", key, state);
  }
}

here’s the sketch:

Lib lib;

void setup() {
  fill(0);
  textSize(30);
  lib = new Lib(this);
  println(lib);
}

void draw() {
  background(lib.isActive() ? #00FF00 : 100);
  text(lib.getKey(), 40, 60);
}

void keyPressed() {
  println(lib);
}

As you can see, the final behavior of the program is correct, in this example when pressing a key, our program must turn a green color on or off as the background color, and the text shows the key we press. The problem is in the information that is printed to the console. I would be very grateful if someone could help me with this issue in the most standardized way possible.

Thank you!

I don’t think so :frowning:

Can you make another method that is triggered by change of the state, for example?