# On/off with keypressed and released

**URL:** <https://discourse.processing.org/t/on-off-with-keypressed-and-released/17211>\
**Category:** Coding Questions\
**Created:** [January 20, 2020, 6:00pm UTC](https://discourse.processing.org/t/on-off-with-keypressed-and-released/17211 "2020-01-20T18:00:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![floflopm](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@floflopm](https://discourse.processing.org/u/floflopm)\
**Post date:** [January 20, 2020, 6:00pm UTC](https://discourse.processing.org/t/on-off-with-keypressed-and-released/17211/1 "2020-01-20T18:00:40Z")

</div>

Hi,

the following bit of my script I would like to activate only when a key is pressed and hold. I would like to deactivate it as soon as the key is released. How can I do this?

Thanks,

```auto
// (it is located at the end of void draw section)
float[][] temp = previous;//ripple. if disables, only creates static pixels
        previous = current;
        current = temp;
```

---

<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 20, 2020, 7:31pm UTC](https://discourse.processing.org/t/on-off-with-keypressed-and-released/17211/2 "2020-01-20T19:31:09Z")

</div>

```auto
if(keyPressed) {

}

```

---

<div class="post-metadata">

**Author:** ![adamskii\_uk](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@adamskii\_uk](https://discourse.processing.org/u/adamskii_uk)\
**Post date:** [September 18, 2020, 9:49pm UTC](https://discourse.processing.org/t/on-off-with-keypressed-and-released/17211/3 "2020-09-18T21:49:02Z")

</div>

You could try something similar to the following two examples. 🙂

```auto
 if (keyPressed && (key == 'a' || key == 'A')) {
    float[][] temp = previous;
    previous = current;
    current = temp;
 }

```

```auto
if (keyPressed && (key == CODED) && (keyCode == LEFT)) {
   float[][] temp = previous;
   previous = current;
   current = temp;
 }

```

Edit: Sorry I just realised this is an old thread from over 8 months ago.
