# How do i detect when spacebar is pressed?

**URL:** <https://discourse.processing.org/t/how-do-i-detect-when-spacebar-is-pressed/26845>\
**Category:** Coding Questions\
**Created:** [January 6, 2021, 7:13pm UTC](https://discourse.processing.org/t/how-do-i-detect-when-spacebar-is-pressed/26845 "2021-01-06T19:13:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanto2016](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@nanto2016](https://discourse.processing.org/u/nanto2016)\
**Post date:** [January 6, 2021, 7:13pm UTC](https://discourse.processing.org/t/how-do-i-detect-when-spacebar-is-pressed/26845/1 "2021-01-06T19:13:22Z")

</div>

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

```auto
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 ! 😁

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 6, 2021, 7:25pm UTC](https://discourse.processing.org/t/how-do-i-detect-when-spacebar-is-pressed/26845/2 "2021-01-06T19:25:36Z")

</div>

this works:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 6, 2021, 8:47pm UTC](https://discourse.processing.org/t/how-do-i-detect-when-spacebar-is-pressed/26845/3 "2021-01-06T20:47:17Z")

</div>

Hello,

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

> **[Processing.org](https://Processing.org/)**

References:  
[https://processing.org/reference/keyPressed\_.html](https://processing.org/reference/keyPressed_.html) \< Example here  
[http://www.asciitable.com/](http://www.asciitable.com/)  
[https://en.wikipedia.org/wiki/Escape\_character](https://en.wikipedia.org/wiki/Escape_character)  
[https://processing.org/reference/String.html](https://processing.org/reference/String.html)

```auto
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.

`:)`
