Logical AND with keys

Hi,

Hello, I can’t find this question in the forum, let’s see if you can help me. I’m trying to create a condition when I press two keys at the same time, but it doesn’t work for me. Does anyone know if it’s a syntax issue or if it just can’t be done?

Here’s my code:

float wei=1;

void setup() {
  size(500, 500);
}

void draw() {
  background(0);
  noFill();
  stroke(255);
  strokeWeight(wei);
  rectMode(CENTER);
  rect(width/2, height/2, 80, 80);
  wei+=0.1;
}

void keyPressed() {
  if ( key=='j' && key=='k') 
    wei=0;
}

Key will only be equal to one value.
See reference:
keyPressed() / Reference / Processing.org

The right keywords will find an answer.

Try a search here:
processing.org < Search in upper right

Or limit search to just this forum.

Try search words like multiple key press.

A simple example that is NOT complete:

boolean mbl = false;
boolean mbr = false;

void setup() {
  }

void draw() {
  }

void mousePressed() 
  {
  if (mouseButton == LEFT)  mbl = true; 
  if (mouseButton == RIGHT) mbr = true; 

  if (mbl&mbr) println("Success!");
  }

You should be resetting the state of mbl and mbr after success.

Example is only for understanding logic.
Start with simple code only and build on that.

The search will reveal other ways to do this.

It does work integrated with your code.

:)

Another technique is to use a boolean array with two elements, each key setting its value true or false based on whether it’s pressed or not. That means you’ll have to add a keyReleased() method. Then in draw() something like this:

  if ((keyDwn[0])&&(keyDwn[1])) {
    wei=0;
  } else {
    wei+=0.1;
  }

That should arrest growth of the rectangle if that’s the goal.