Problem with keyPressed() and SPACE

I am new here and with Processing.
My Problem is:

void keyPressed()
{
 if(key == SPACE)
 {
 ellipse(10, 10, 20, 20);
 }
}

Processing “says” that “SPACE” is an invalid character constant.
I also tried these:

void keyPressed()
{
 if(key == 'SPACE')
 {
  ellipse(10, 10, 20, 20);
 }
}

void keyPressed()
{
 if(key == ' ')
 {
  ellipse(10, 10, 20, 20);
 }
}

void keyPressed()
{
 if(key == CODED)
 {
  if(key == SPACE)
  {
   ellipse(10, 10, 20, 20);
  }
 }
}

Please help me!

1 Like

-a- better have a clean program structure
-b- need to know that keyboard only work AFTER click on the small canvas window
-c- you better call

void keyPressed()

only one time
-d- it helps when for diagnostic you first print every key input

void setup(){
}
void draw(){
}
void keyPressed() {
  println("key: "+key+" keyCode: "+keyCode);
  if ( key == ' ' )    println("[space]");
  if ( key == 32  )    println(" also [space]");
  if ( keyCode == UP ) println("[UP]");
}

https://processing.org/reference/key.html
https://processing.org/reference/keyCode.html

to get something drawn and moving can try

int posx,posy,rad=20;

void setup(){
  size(200,200);
  println("use e a d x for move, - + for size");
}
void draw(){
  background(200,200,0);
  translate(width/2,height/2);
  stroke(0,200,0);
  fill(0,0,200);
  ellipse(posx,posy,rad,rad);
}
void keyPressed() {
//  println("key: "+key+" keyCode: "+keyCode);
//  if ( key == ' ' )    println("[space]");
//  if ( key == 32  )    println(" also [space]");
//  if ( keyCode == UP ) println("[UP]");
  
  // move something
  if ( key == 'e' ) posy--;
  if ( key == 'x' ) posy++;
  if ( key == 'a' ) posx--;
  if ( key == 'd' ) posx++;
  
  if ( key == '-' ) rad--;
  if ( key == '+' ) rad++;
  
}

1 Like

So it tried this 2 but for me it worked with:

if(key == ' ') {
  background(#0DFF33) ; 
}
1 Like