# keyReleased acting strangely

**URL:** <https://discourse.processing.org/t/keyreleased-acting-strangely/135>\
**Category:** Coding Questions\
**Created:** [May 19, 2018, 1:11pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135 "2018-05-19T13:11:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [May 19, 2018, 1:11pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135/1 "2018-05-19T13:11:55Z")

</div>

I decided to take the challenge of programming a game in processing, and I realized when I let go of a key, _keyCode would not change!_ so I wrote this at the end:

```auto
void keyReleased() {
  keyCode = ENTER;
}

```

It worked, but there was one problem… when I was holding a button, _it sometimes was triggering keyReleased!_ I’m used to using processing, but this is too confusing! please help.

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [May 19, 2018, 1:30pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135/2 "2018-05-19T13:30:27Z")

</div>

I don’t know about the keyReleased function but try this: After you find the code of a key you want to, set the keyCode to 0

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [May 19, 2018, 4:53pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135/3 "2018-05-19T16:53:22Z")

</div>

That messed up part of my code…  
Here is the complete part (currently), hope this helps!

```auto
void getinput () {
  if (keyCode == 0) {
    xacc/=2;
  }else{
    if (keyCode == RIGHT) {
      xacc+=speed;
    };
    if (keyCode == LEFT) {
      xacc-=speed;
    };
  }
  keyCode = 0;
  if (abs(xacc)>xaccmax) {xacc=xaccmax*sign(xacc);}
  if (abs(yacc)>yaccmax) {yacc=yaccmax*sign(yacc);}
}

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [May 19, 2018, 5:20pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135/4 "2018-05-19T17:20:07Z")

</div>

Different systems will use the key events slightly differently. Generally `keyCode` holds the key that was most recently interacted with- it doesn’t reset when you release a key.

You can use the `keyPressed` variable to check that a key is currently pressed, and then check the `keyCode` variable. That way you only take some action when the key is actually pressed.

If you need more control over the key events, then you could store the state of each key you care about in a `boolean` variable. Here’s an example:

```auto
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;

float circleX = 50;
float circleY = 50;

void draw() {
  background(200);  
  
  if (upPressed) {
    circleY--;
  }
  
  if (downPressed) {
    circleY++;
  }
  
  if (leftPressed) {
    circleX--;
  }
  
  if (rightPressed) {
    circleX++;
  }
  
  ellipse(circleX, circleY, 20, 20);
}

void keyPressed() {
  if (keyCode == UP) {
    upPressed = true;
  }
  else if (keyCode == DOWN) {
    downPressed = true;
  }
  else if (keyCode == LEFT) {
    leftPressed = true;
  }
  else if (keyCode == RIGHT) {
    rightPressed = true;
  }
}

void keyReleased() {
  if (keyCode == UP) {
    upPressed = false;
  }
  else if (keyCode == DOWN) {
    downPressed = false;
  }
  else if (keyCode == LEFT) {
    leftPressed = false;
  }
  else if (keyCode == RIGHT) {
    rightPressed = false;
  }
}

```

Shameless self-promotion: I wrote a tutorial on key events in Processing, including the above technique, available [here](http://happycoding.io/tutorials/processing/input#handling-multiple-keys).

Btw, in the future please post a [MCVE](https://stackoverflow.com/help/mcve) so we know exactly what you’re trying to do.

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [May 19, 2018, 6:26pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135/5 "2018-05-19T18:26:37Z")

</div>

That worked flawlessly! Sorry for not posting a MCVE, I’m new here =P

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 21, 2018, 4:47pm UTC](https://discourse.processing.org/t/keyreleased-acting-strangely/135/6 "2018-05-21T16:47:23Z")

</div>

An even more general approach is to create a HashMap which contains a list of whatever key characters you have that are currently active – then take them out and add them as needed. This can be a good way of handling multiple simultaneous key presses, sticky keys, on-off toggle keys, etc.

```
import java.util.Map;
HashMap<Character,Boolean> flags = new HashMap<Character,Boolean>();

```

There are some simple examples of using this approach on the old forum:

- [https://forum.processing.org/two/discussion/18261/how-to-choose-a-movie-file-to-play](https://forum.processing.org/two/discussion/18261/how-to-choose-a-movie-file-to-play)
