[JAVA] Simultaneous key press in Processing

** Introduction **

For super beginners. You can also press and hold it for a long time.

** How to do **

The keyPressed function / keyReleased function determines whether each key is pressed or released.

//Stores the input status of each key
boolean right, left, down, up;

void keyPressed() {
  if (keyCode == RIGHT) right = true;
  if (keyCode == LEFT)  left  = true;
  if (keyCode == DOWN)  down  = true;
  if (keyCode == UP)    up    = true;
}

void keyReleased() {
  if (keyCode == RIGHT) right = false;
  if (keyCode == LEFT)  left  = false;
  if (keyCode == DOWN)  down  = false;
  if (keyCode == UP)    up    = false;
}

Simultaneous press / long press processing can be performed with just this. later

python


  if (right) {
    /*Processing when the right key is pressed*/
  }
  if (left) {
    /*Processing when the left key is pressed*/
  }
  if (down) {
    /*Processing when the down key is pressed*/
  }
  if (up) {
    /*Processing when the up key is pressed*/
  }

You can divide the processing for each key like this.

However, this is not very smart, so let's manage the input state collectively with HashMap.

python


// keyCode(int type)And its input state(boolean type)Store
HashMap<Integer, Boolean> key = new HashMap<Integer, Boolean>();

void keyPressed() {
  key.put(keyCode, true);
}

void keyReleased() {
  key.put(keyCode, false);
}

In addition, create a KeyState class that manages the input state.

KeyState.pde


static class KeyState {
  static HashMap<Integer, Boolean> key;
  
  //Initialization of input state
  static void initialize() {
    key = new HashMap<Integer, Boolean>();

    key.put(RIGHT, false);
    key.put(LEFT,  false);
    key.put(UP,    false);
    key.put(DOWN,  false);
  }
  
  //Receives and updates keyCode and its input status
  static void putState(int code, boolean state) {
    key.put(code, state);
  }
  
  //Receives keyCode and returns its input status
  static boolean getState(int code) {
    return key.get(code);
  }
}

When getting the input status, it looks like this

main.pde


void setup() {
  KeyState.initialize();
}

void draw() {
  if (keyState.getState(RIGHT)) {
    /*Processing when the right key is pressed*/
  }
  if (keyState.getState(LEFT)) {
    /*Processing when the left key is pressed*/
  }
  if (keyState.getState(DOWN)) {
    /*Processing when the down key is pressed*/
  }
  if (keyState.getState(UP)) {
    /*Processing when the up key is pressed*/
  }
}

void keyPressed() {
  KeyState.putState(keyCode, true);
}

void keyReleased() {
  KeyState.putState(keyCode, false);
}

Thank you for your support.

in conclusion

The direction key is ** keyCode (int type) **, but Tab and Enter are stored in ** key (char type) **, so you need to make another effort.

Recommended Posts

Simultaneous key press in Processing
Use MouseListener in Processing
Write Processing in IntelliJ IDEA
Measured parallel processing in Java
Mazume judgment processing in fishing
Try implementing asynchronous processing in Azure
Date processing in Java (LocalDate: Initialization)
Implementation of asynchronous processing in Tomcat
Self-pipe technique useful in IO.select processing
Event processing is performed in Spring.
About file copy processing in Java