# Report Number of Key Clicks During Timer

**URL:** <https://discourse.processing.org/t/report-number-of-key-clicks-during-timer/31364>\
**Category:** Beginners\
**Created:** [July 21, 2021, 2:27pm UTC](https://discourse.processing.org/t/report-number-of-key-clicks-during-timer/31364 "2021-07-21T14:27:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BruceTheWayneJohnson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brucethewaynejohnson/32/15857_2.png) [@BruceTheWayneJohnson](https://discourse.processing.org/u/BruceTheWayneJohnson)\
**Post date:** [July 21, 2021, 2:27pm UTC](https://discourse.processing.org/t/report-number-of-key-clicks-during-timer/31364/1 "2021-07-21T14:27:56Z")

</div>

I am currently trying to get my program to print how many times ‘a’ is pressed while the timer counts to 2000ms. So far, all I can get printed is some wildly larger number than how many times I pressed ‘a’.  
It says anywhere from 90 to 120 even when I only click 3 or 4 times.

Is there a problem in my draw function?

\<// The duration of the time  
int timerDuration = 2000;

//The time at which we start the timer  
int startTime = 0;

// Whether or not the timer was started  
boolean timerState = false;

int aVal;

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

void draw(){  
background(255);

if (timerState) {  
//We subtract the current time and the time when we startedt he timer  
int currentDuration = millis() - startTime;

fill (0);  
textAlign(CENTER, CENTER);  
text(currentDuration, width /2, height /2);

if (key == ‘a’) {  
aVal++;  
}

//If the event is over  
if (currentDuration \> timerDuration) {  
timerState = false;  
println("‘a’ pressed “, aVal, " times”);  
aVal = 0;  
}  
}  
}

void mousePressed() {  
startTime = millis();  
timerState = true;  
}

>

---

<div class="post-metadata">

**Author:** ![StainlessHolz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stainlessholz/32/13542_2.png) [@StainlessHolz](https://discourse.processing.org/u/StainlessHolz)\
**Post date:** [July 21, 2021, 4:23pm UTC](https://discourse.processing.org/t/report-number-of-key-clicks-during-timer/31364/2 "2021-07-21T16:23:36Z")

</div>

Hi,  
Yes your count goes up every time the draw loops and the a key was pressed.  
Maybe even if it was the last key that was pressed.  
You need to look into the keyPressed()/keyReleased() functions.  
These only get triggered once and are independent of the draw loop.

Please use the code button when you post code, it makes it easier to read.

---

<div class="post-metadata">

**Author:** ![BruceTheWayneJohnson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brucethewaynejohnson/32/15857_2.png) [@BruceTheWayneJohnson](https://discourse.processing.org/u/BruceTheWayneJohnson)\
**Post date:** [July 21, 2021, 7:42pm UTC](https://discourse.processing.org/t/report-number-of-key-clicks-during-timer/31364/3 "2021-07-21T19:42:45Z")

</div>

So the chunk of code currently located in draw() would function normally in the keyPressed() function? I’ll give that a try.

Thanks

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 21, 2021, 8:37pm UTC](https://discourse.processing.org/t/report-number-of-key-clicks-during-timer/31364/4 "2021-07-21T20:37:03Z")

</div>

> [@BruceTheWayneJohnson](#):
>
> if (key == ‘a’) {  
> aVal++;

move this into a function

```auto

void keyPressed() {

}

```
