# Solution to prevent spam for keyReleased()?

**URL:** <https://discourse.processing.org/t/solution-to-prevent-spam-for-keyreleased/6996>\
**Category:** Coding Questions\
**Created:** [December 29, 2018, 7:17pm UTC](https://discourse.processing.org/t/solution-to-prevent-spam-for-keyreleased/6996 "2018-12-29T19:17:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![immortalTech](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/immortaltech/32/2992_2.png) [@immortalTech](https://discourse.processing.org/u/immortalTech)\
**Post date:** [December 29, 2018, 7:17pm UTC](https://discourse.processing.org/t/solution-to-prevent-spam-for-keyreleased/6996/1 "2018-12-29T19:17:09Z")

</div>

Hello! So basically I have programmed a breakout game. I made a pause feature going, but it’ll break if it’s spammed (and practically anything will break if spammed, but this pause feature is like a critical component to the game and it has to be like semi-flawless (since nothing is ever flawless irl tbh).

I have a few solutions to prevent spamming keys: making my own setTimeout or setInterval and clearInterval to regulate between times. OR

Although, I did try the setTimeout stuff, but I am sorta new to java and processing itself, but i’m willing to learn and I need in-depth explanations.

Part of my code:

```auto
//size is 480 by 480

public boolean run = true;

void keyReleased(){

[Redacted]
  
  if(!isGameOver){ //remove IF condition for your sake
    if(key == 'p'){
      run = !run;
      if(run == true){
        fill(211, 211, 211, 255);
        rect(0, 0, 480, 480);
        println("bg music plays"); //backgroundMusic.play();
      }
      if(run == false){
        fill(211, 211, 211, 100);
        rect(0, 0, 480, 480);
        println("bg music pauses"); //backgroundMusic.pause();
        fill(0);
        text("Press P to unPause!", 130, 300);
      }
    }
  }
}

```

Key Question: Is there an efficient or non-efficient way, yet simple method to regulate the spamming of the keys per KeyReleased()?

I want the spamming to halt since it will enable players cheating, if you spam P to pause and move the mouse to move the paddle, (as there’s a slight second for the game to be resumed, but really should only resume until 2-3 seconds or 2000/3000 ms.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [December 29, 2018, 8:03pm UTC](https://discourse.processing.org/t/solution-to-prevent-spam-for-keyreleased/6996/2 "2018-12-29T20:03:03Z")

</div>

Taking a step back, I think you should probably fix your code so that spamming a key doesn’t break it.

But to answer your question, you can use the `frameCount` variable or the `millis()` function to get the current frame or millisecond count. Use that to introduce an offset. Something like:

```auto
if(frameCount > keyPressedFrameCount + timeoutFrameCount){
  doTheThing();
}

```

---

<div class="post-metadata">

**Author:** ![immortalTech](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/immortaltech/32/2992_2.png) [@immortalTech](https://discourse.processing.org/u/immortalTech)\
**Post date:** [December 30, 2018, 7:02pm UTC](https://discourse.processing.org/t/solution-to-prevent-spam-for-keyreleased/6996/3 "2018-12-30T19:02:10Z")

</div>

It doesn’t break if it’s spammed, only when i attempted to add a timer. Without a timer, it won’t break if you spam the keys. Although, the player can actually cheat by spamming the key. Maybe I can add something to make it harder for them to cheat this way.
