# Logical AND with keys

**URL:** <https://discourse.processing.org/t/logical-and-with-keys/44631>\
**Category:** Coding Questions\
**Created:** [June 23, 2024, 11:00am UTC](https://discourse.processing.org/t/logical-and-with-keys/44631 "2024-06-23T11:00:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)\
**Post date:** [June 23, 2024, 11:00am UTC](https://discourse.processing.org/t/logical-and-with-keys/44631/1 "2024-06-23T11:00:13Z")

</div>

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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 23, 2024, 11:28am UTC](https://discourse.processing.org/t/logical-and-with-keys/44631/2 "2024-06-23T11:28:25Z")

</div>

> [@Karrajo](#):
>
> ```auto
> void keyPressed() {
> if ( key=='j' && key=='k') 
> wei=0;
> }
> 
> ```

Key will only be equal to one value.  
See reference:  
_[keyPressed() / Reference / Processing.org](https://processing.org/reference/keyPressed_.html)_

> [@Karrajo](#):
>
> I can’t find this question in the forum

The right keywords will find an answer.

Try a search here:  
_[processing.org](http://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:

```auto
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.

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 23, 2024, 3:54pm UTC](https://discourse.processing.org/t/logical-and-with-keys/44631/3 "2024-06-23T15:54:01Z")

</div>

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:

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

```

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