If you are doing more than a few keys, another general option is to use implement @TfGuy44’s boolean approach using a HashMap.
import java.util.Map;
HashMap<Character,Boolean> flags = new HashMap<Character,Boolean>();
Any key that gets pressed, put it in the map with a true
… any key that gets released, take it out with a false.
void keyPressed(){
flags.put(key, Boolean.TRUE);
}
void keyReleased(){
flags.put(key, Boolean.FALSE);
}
This can be used for simultaneous keypresses – or for a large collection of toggles:
void keyReleased(){
if(flags.get(key)!=null){
flags.put(key, !(flags.get(key)));
} else {
flags.put(key, Boolean.TRUE);
}
}