# keyPressed and keyReleased

**URL:** <https://discourse.processing.org/t/keypressed-and-keyreleased/24296>\
**Category:** Coding Questions\
**Created:** [October 2, 2020, 11:02pm UTC](https://discourse.processing.org/t/keypressed-and-keyreleased/24296 "2020-10-02T23:02:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaspervb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jaspervb/32/10571_2.png) [@jaspervb](https://discourse.processing.org/u/jaspervb)\
**Post date:** [October 2, 2020, 11:02pm UTC](https://discourse.processing.org/t/keypressed-and-keyreleased/24296/1 "2020-10-02T23:02:42Z")

</div>

Hello there,

I need some help with processing.

I am using the following code to lock and unlock rectangles with keycode. This works perfectly… In this case i want to add the following posibility: when you press a key for the first time, the rectangle will appear… if you press that same button for the second time, the rectangle will dissapear.

Thanks

```auto
boolean[] keys = new boolean[3];
 
void setup() {
  size (300, 300);
  background(255);
  keys[0] = false;
  keys[1] = false;
  keys[2] = false;
}
 
 
void draw() {
  background(255);
 
 // conditional drawing
  if (keys[0]) {
    rect(50, 100, 50, 50);
  }
  if (keys[1]) {
    rect(125, 100, 50, 50);
  }
  if (keys[2]) {
    rect(200, 100, 50, 50);
  }
}
 
void keyPressed() {
 
  switch (key) {   
  case 'a' : 
    keys[0] = true;
    break;
  case 's' : 
    keys[1] = true;
    break;
  case 'd' : 
    keys[2] = true;
    break;
  }
}
 
void keyReleased() {
 
  switch (key) {   
  case 'a' : 
    keys[0] = false;
    break;
  case 's' : 
    keys[1] = false;
    break;
  case 'd' : 
    keys[2] = false;
    break;
  }
}

```

---

<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:** [October 2, 2020, 11:18pm UTC](https://discourse.processing.org/t/keypressed-and-keyreleased/24296/2 "2020-10-02T23:18:55Z")

</div>

you could check if the rectangle is there and if so, make it go off and if not, switch it on

```auto
if ( keys[0] == true ) 
   keys[0] = false; 
else   
   keys[0] = true;

```

Here is a shorter form

(by the way Keyreleased is not necessary; that’s more when you want to happen something during the key is down)

```auto
boolean[] keys = new boolean[3];

void setup() {
  size (300, 300);
  background(255);
  keys[0] = false;
  keys[1] = false;
  keys[2] = false;
}

void draw() {
  background(255);

  // conditional drawing
  if (keys[0]) {
    rect(50, 100, 50, 50);
  }
  if (keys[1]) {
    rect(125, 100, 50, 50);
  }
  if (keys[2]) {
    rect(200, 100, 50, 50);
  }
}

void keyPressed() {

  switch (key) {   
  case 'a' : 
    keys[0] = 
      ! keys[0];
    break;
  case 's' : 
    keys[1] = 
      ! keys[1];
    break;
  case 'd' : 
    keys[2] = 
      ! keys[2] ;
    break;
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![jaspervb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jaspervb/32/10571_2.png) [@jaspervb](https://discourse.processing.org/u/jaspervb)\
**Post date:** [October 2, 2020, 11:23pm UTC](https://discourse.processing.org/t/keypressed-and-keyreleased/24296/3 "2020-10-02T23:23:27Z")

</div>

Thank you very much! This helps me a lot!

---

<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:** [October 2, 2020, 11:23pm UTC](https://discourse.processing.org/t/keypressed-and-keyreleased/24296/4 "2020-10-02T23:23:59Z")

</div>

> [@Chrisir](#):
>
> ```auto
> keys[0] = 
> ! keys[0];
> 
> ```

This means:

- set keys[0] to NOT keys[0] which means
- set keys[0] to its opposite

The ! means logical NOT, see reference : [! (logical NOT) / Reference / Processing.org](https://www.processing.org/reference/logicalNOT.html)
