How to change background color for just a short period of time after pressing a key?

hello everyone.
i want to press the enter key and than change the color of the screen to green for 1 second. After that 1 second it should go back to white. And I want to do that in the keyPressed() method.

My work so far:

void keyPressed() {
if(key == ENTER) {
println(“enter Pressed.”);

but it doesnt change the color. It just pauses and than continue without changeing anything.

how can i fix that?

Don’t use delay

Instead in draw() you use a variable bkCol in the background command

background (bkCol);

Before setup () say color bkCol = background_color; or so

On Enter Key

Now, in the enter key section you change bkCol=success_color; so we change the background which is displayed in draw().
Here you also say timer=millis(); which stores the current milliseconds.

Before setup() please add int timer=0;


At the end of draw() say

// evaluate if one second is over 
if(millis()-timer >= 1000){
   bkCol=background_color;  // reset color background_color

The program is just running on, draw() runs 60 times per second. And we just change the variable for the background.

And we reset the color after a timer is over.

I hope this helps.

Hey, and welcome to the forum!
Great to have you here!


1 Like

Hello @Rebello,

Some references to understand events and delay():

Here is a good example (without a timer):

There are lots of examples of timing out there. Find one that suits you and once you understand it integrate it into your code.

Some timers are based on these references:


1 Like

Thank you guys so much for helping me so quickly.
That helped, now it works.

also i have to say that you are way more friendly than the people on StackOverflow

1 Like


Try to play with this