How to register 2 keys at a time

Following up on @Sarah’s answer – I have shared a HashMap solution to this problem and ones like this a few times in the past. The HashMap approach is a bit nicer than int[1000] in that it can store any key you press, but ONLY stores the keys you press. Then instead of checking for keys, you do this:

void keyPressed(){
  flags.put(key, Boolean.TRUE);
}
void keyReleased(){
  flags.put(key, Boolean.FALSE);
}

Now you can check your map with get() and if it is true, the key is down.

Here is the code and explanation:

3 Likes