There is a delay when i use keypressed() to change value

Hi all! I am a newbie of processing. I find that the keyPressed() can’t change the value directly until the all codes in draw() are executed for one time.
Like my example below,I want to use keyPressed() function to change the value of b(then I print b)
but the b’s value changes only after the delay() in draw().
Then how could I change the b’s value directly when I press some key?
Thank you for your help!

The processing:

void setup() {
  size(200, 200);
  b=0;
}

void keyPressed() {
  if (keyCode == LEFT)
      b+=1;
      print("\nkeyb="+b);  
}

void draw() {
  delay(5000);
   print("\ndrawb="+b);
}

I can’t imagine ANY situation in which putting a call to delay() in draw() makes any sense. First of all, remove that call. Then try telling us what you hope to accomplish. Do you only want to update and redraw your scene every 5 seconds? If so, then delay() is not even the right function for that. Try being creative with millis() instead.

The reason why that happens is because both draw() and keyPressed() are run sequentially on a single thread. keyPressed() is processed only as soon as draw() finishes, which you noticed.

The reason it’s done that way is because else there could be some code problems, from little frustrations on what happens, to complex threading problems no sane programmer would want to deal with.

For example, let’s say that your draw() is this:

void draw(){
  println("New draw cycle");
  if(b==2){ println("b==2"); }
  delay(5000);
  if(b==2){ println("b==2"); }
}

Notice that the first and third lines are exactly the same, both check if b==2.
Now, if keyPressed() would be processed instantly on a separate thread, both statements, even if they are run one after another, would produce different results.

In rare occasions it could be anticipated and intended, but else it could easily just cause a lot of headache as to why two identical if statements, run one after another, with nothing between them that could change the result, give different results.

However, what you could do instead is to let draw() run at full speed, and run the code inside of it not as often. For example, here’s your code, changed to produce the result you want it to:

int b=0; //You didn't define it in the original code
void setup() {
  size(200, 200);
}

void keyPressed() {
  if (keyCode == LEFT) { //Also you forgot {} brackets there!
    b+=1;
    print("\nkeyb="+b);
  }
}

void draw() {
  if(frameCount%300 == 0){ //5000 milliseconds is ~300 frames - this is true on every 300th frame.
    print("\ndrawb="+b);
  }
}

Also, a bit offtopic.
I feel like you’re bashing on newbies a bit too hard sometimes!
This is an example, proof-of-concept sketch, where delay(5000); means “something that takes 5000 milliseconds to process”. It makes sense not as a final enterprise-ready product of genuine code, but as an example to illustrate what they mean.

Also, I think you are forgetting an old saying of “if it’s stupid, and it works, it ain’t stupid.”

Please, be nicer. ._.