keyReleased acting strangely

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:

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.

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

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

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);}
}

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:

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.

Btw, in the future please post a MCVE so we know exactly what you’re trying to do.

1 Like

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

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: