Key pressed variable

Hello,

You can adapt this example for your needs:


boolean toggle = false;

void setup()
  {
  println(toggle);
  println("Click on sketch window with mouse to make it active!");
  println("Hit a key to toggle background colour!");
  println(toggle);
  }

void draw() 
  {    
  }

void keyPressed()
  {
  toggle = !toggle;
  if (toggle)
    {
    background(255, 0, 0);
    println(toggle);
    }
  else 
    {
    background(0, 255, 0);
    println(toggle);
    }
  }

Understand what it is doing before using it.

References:

:)

2 Likes