How do i detect when spacebar is pressed?

well I want my character to jump when i press spacebar, but it doesn’t work.

void keyPressed(){
//i'd like to detect when space bar is pressed.

if(key==' '){}

//it did'nt work though :(

}

i saw (key==’ ') in other codes, but it doesn’t work for me.
thanks ! :grin:

this works:



boolean check = false; 

void draw() {
  if (check) {
    println("1");
  }
}

void keyPressed() {
  //i'd like to detect when space bar is pressed.

  if (key==' ') {

    check = true;
    //
  }//if
  //
}//func

Hello,

Explore the resources (tutorials, references, examples, etc.) here:

References:
https://processing.org/reference/keyPressed_.html < Example here
http://www.asciitable.com/
https://en.wikipedia.org/wiki/Escape_character
https://processing.org/reference/String.html

int value = 0;

void draw() 
  {
  fill(value);
  rect(25, 25, 50, 50);
  }

void keyPressed() 
  {
  //if (key == 32)
  if (key == ' ') //same as above
    {
    println("space");
    } 
  else 
    {
     //if (key == 10);
     if (key == '\n'); //same as above
     println("line feed");
    }
  }

You may have to pass a variable to draw() as in the keyPressed() reference.

:)