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

**URL:** <https://discourse.processing.org/t/there-is-a-delay-when-i-use-keypressed-to-change-value/7028>\
**Category:** Coding Questions\
**Created:** [December 30, 2018, 8:46pm UTC](https://discourse.processing.org/t/there-is-a-delay-when-i-use-keypressed-to-change-value/7028 "2018-12-30T20:46:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jani](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jani](https://discourse.processing.org/u/Jani)\
**Post date:** [December 30, 2018, 8:46pm UTC](https://discourse.processing.org/t/there-is-a-delay-when-i-use-keypressed-to-change-value/7028/1 "2018-12-30T20:46:21Z")

</div>

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:

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

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

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

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 30, 2018, 9:59pm UTC](https://discourse.processing.org/t/there-is-a-delay-when-i-use-keypressed-to-change-value/7028/2 "2018-12-30T21:59:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 30, 2018, 11:24pm UTC](https://discourse.processing.org/t/there-is-a-delay-when-i-use-keypressed-to-change-value/7028/3 "2018-12-30T23:24:04Z")

</div>

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:

```auto
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:

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 30, 2018, 11:27pm UTC](https://discourse.processing.org/t/there-is-a-delay-when-i-use-keypressed-to-change-value/7028/4 "2018-12-30T23:27:09Z")

</div>

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. .\_.
