# keyPressed with multiple keys

**URL:** <https://discourse.processing.org/t/keypressed-with-multiple-keys/21040>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 19, 2020, 2:15am UTC](https://discourse.processing.org/t/keypressed-with-multiple-keys/21040 "2020-05-19T02:15:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![homeCoder](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@homeCoder](https://discourse.processing.org/u/homeCoder)\
**Post date:** [May 19, 2020, 2:15am UTC](https://discourse.processing.org/t/keypressed-with-multiple-keys/21040/1 "2020-05-19T02:15:18Z")

</div>

Hello,  
i am trying to write a program which shows two arrays with numbers and if I press the number of an array element, this element should disappear and the others replace the gap. That’s function, but I wan’t to do something special like: If I press ‘2’ and then ‘d’ and then ‘Enter’ the second element should disappear. I already tried it with multiple if-clings but it didn’t worked. Does someone has an idea?

```auto
void keyPressed()                             
{
  position = key;
  println(position);
  position = Character.getNumericValue(key);
println(position);

      if (keyPressed)
      {
        loesche = true;
      }

}

```

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 19, 2020, 4:50am UTC](https://discourse.processing.org/t/keypressed-with-multiple-keys/21040/2 "2020-05-19T04:50:26Z")

</div>

Hi,

Each key press triggers `keyPressed()` event. So if you want to track keys “2”, “d” and enter, you need to store key presses or have a state change.

Here’s a code that prints “Wohoo” only and only if keys 2 and d are pressed in succession. It’s also a simple state machine.

```auto
boolean step1 = false;
boolean step2 = false;

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

void draw() {
}

void keyPressed() {
  if(key=='d') {
    step1=true;
  } else if(key=='2' && step1==true) {
    step2 = true;
    println("Wohoo");
  } else {
    step1 = false;
    step2 = false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![homeCoder](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@homeCoder](https://discourse.processing.org/u/homeCoder)\
**Post date:** [May 19, 2020, 12:34pm UTC](https://discourse.processing.org/t/keypressed-with-multiple-keys/21040/3 "2020-05-19T12:34:31Z")

</div>

Thanks for your fast respond. I implemented your suggestion now into my code, but I have little trouble. In your code you made the key ‘d’ to an one time activation button to become the possibility to println Wohoo if you pressed ‘2’. But I want to make the first key responsive to the numbers 1 to 9. For example if you press number ‘4’ than ‘d’ and than Enter, the 4th element of the array gets deleted.  
`if(key = elementname[i]) {...}` I tried this version but that didn’t help. Than I tried this: `if(key >= '1' && key <= '9') ` now it’s function half. The system now detect all numbers but don’t assign them to the array element with the same number.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 19, 2020, 1:54pm UTC](https://discourse.processing.org/t/keypressed-with-multiple-keys/21040/4 "2020-05-19T13:54:54Z")

</div>

Okey, so you’d need to store the numeric value too. Because key presses give characters you’d also need a way to change characters to numbers. As you already found out characters can be used in comparison as they are stored with two bytes and can be treated as numbers. Numbers are ordered as 0,1,2,3,…,9, so if we reduce value of char ‘0’ from a char that is a number we get the same numeric value.

```auto

void keyPressed() {
  if(key >= '1' && key <= '9') {
    step1=true;
    value = key - '0';
  }

```
