processingkeycodectrl

How Processing knows that user is pressing multiple key at the same time?


I have no idea how Processing knows that a user is pressing Ctrl and some character at the same time.

Multiple buttons at same time only. Is it possible?

ex: (Ctrl+r).


Solution

  • You have to first check if Ctrl has been pressed. If it has been pressed then you save a boolean as true. The next time you press a button you check if the button is the button you want (i.e. 'r') and if the boolean is true. If both are true then Processing knows...

    Here's a demonstration:

    boolean isCtrlPressed = false;
    boolean isRPressed = false;
    void draw() {
      background(0);
      fill(255);
      if (isCtrlPressed) background(255, 0, 0);
      if (isRPressed) background(0, 255, 0);
      if (isCtrlPressed && isRPressed) background(255, 255, 0);
    }
    void keyPressed() {
      if (keyCode == CONTROL && isCtrlPressed == false) isCtrlPressed = true;
      if (char(keyCode) == 'R') isRPressed = true;
    
    }
    void keyReleased() {
      if (keyCode == CONTROL) isCtrlPressed = false;
      if (char(keyCode) == 'R') isRPressed = false;
    }