# Problem with keyPressed() and SPACE

**URL:** <https://discourse.processing.org/t/problem-with-keypressed-and-space/7468>\
**Category:** Coding Questions\
**Created:** [January 13, 2019, 11:38am UTC](https://discourse.processing.org/t/problem-with-keypressed-and-space/7468 "2019-01-13T11:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoLi](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@JoLi](https://discourse.processing.org/u/JoLi)\
**Post date:** [January 13, 2019, 11:38am UTC](https://discourse.processing.org/t/problem-with-keypressed-and-space/7468/1 "2019-01-13T11:38:31Z")

</div>

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

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

```

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

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

```

* * *

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

```

* * *

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

```

Please help me!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 13, 2019, 12:27pm UTC](https://discourse.processing.org/t/problem-with-keypressed-and-space/7468/2 "2019-01-13T12:27:45Z")

</div>

-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

```auto
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/key.html)  
[https://processing.org/reference/keyCode.html](https://processing.org/reference/keyCode.html)

to get something drawn and moving can try

```auto
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++;
  
}

```

---

<div class="post-metadata">

**Author:** ![YEeter](https://avatars.discourse-cdn.com/v4/letter/y/96bed5/32.png) [@YEeter](https://discourse.processing.org/u/YEeter)\
**Post date:** [October 27, 2020, 3:52pm UTC](https://discourse.processing.org/t/problem-with-keypressed-and-space/7468/3 "2020-10-27T15:52:32Z")

</div>

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

```auto
if(key == ' ') {
  background(#0DFF33) ; 
}

```
