# How to make keyPressed work inside a while loop

**URL:** <https://discourse.processing.org/t/how-to-make-keypressed-work-inside-a-while-loop/3257>\
**Category:** Coding Questions\
**Created:** [September 4, 2018, 6:54pm UTC](https://discourse.processing.org/t/how-to-make-keypressed-work-inside-a-while-loop/3257 "2018-09-04T18:54:28Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 6, 2018, 6:26am UTC](https://discourse.processing.org/t/how-to-make-keypressed-work-inside-a-while-loop/3257/7 "2018-09-06T06:26:22Z")

</div>

Hi Yamin,

You need to handle that in the `keyPressed()` function in the code I posted.

What you want to do is check if the answer is correct every time a key is pressed. If the answer is correct then you change the state (aka you go to the next question). Otherwise you don’t and the same question stays.

You can check out this thread, the logic is the same: [Quiz game - need help](https://discourse.processing.org/t/quiz-game-need-help/3166/22)

---

_[View the full topic](https://discourse.processing.org/t/how-to-make-keypressed-work-inside-a-while-loop/3257)._
