# Switch case with two key pressed? Is it possible?

**URL:** <https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869>\
**Category:** Coding Questions\
**Created:** [June 14, 2020, 5:02pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869 "2020-06-14T17:02:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [June 14, 2020, 5:02pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/1 "2020-06-14T17:02:02Z")

</div>

Hello.

I would like to switch case with a combination of two letter or letter, one of each.

I did like that, but it doesn’t work

```auto
void keyPressed() {
  
      if (key=='5' && key=='T'){ 

println ("T55555TTTT5555");
   
      }
      
      if (key=='7'){       
          
     
             println ("CASE 7 PRRRRRRRRRRRRRRRREEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSEDDDDDDDDDD"); 

      }
          
  
   if (keyCode == LEFT && key=='1') {
  
        println ("LEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFTLEFT11111111111111");
       
}  

 else if (keyCode == DOWN && key=='2') {/
  
        println ("DOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWNDOWN222222222222222222222");    
}  
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 14, 2020, 6:40pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/2 "2020-06-14T18:40:41Z")

</div>

You can’t use boolean operators with switch statements.

[https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)

Use the if/else…

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [June 15, 2020, 5:17pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/3 "2020-06-15T17:17:38Z")

</div>

I have tried to adapt an example but it doesn’t work

```auto
 int month = 1;
 String monthString;
 
public void settings() {
  
   size(600, 600);
 
  }
  
void setup() {
  }

void draw () {
  
  keyPressed ();
  
   }
void keyPressed () {
  
  
 if ( key=='1' && key=='2' ) { 
   
  // if ( key=='2') {
     month =12 ;
 // } 
 }
  
switch (month) {
  
  
            case 1: monthString = "January";
                     break;
            case 2: monthString = "February";
                     break;
            case 3: monthString = "March";
                     break;
            case 4: monthString = "April";
                     break;
            case 5: monthString = "May";
                     break;
            case 6: monthString = "June";
                     break;
            case 7: monthString = "July";
                     break;
            case 8: monthString = "August";
                     break;
            case 9: monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
    
  }

```

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 15, 2020, 7:02pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/4 "2020-06-15T19:02:11Z")

</div>

`keyPressed()` creates an event listener, so no need to call it in `draw()`.

Not sure what you are going to do when the user needs to input November, but any case here is one way to accomplish what you are trying to do …

```auto
boolean[] keys;
boolean wasMultiPress = false;
final int NUM_KEYS = 2;
color col = color(3, 7, 11);
String monthString = null;

void setup() {
  size(200, 200);
  background(col);
  keys = new boolean[NUM_KEYS];
  for (int i = 0; i < NUM_KEYS; i++) {
    keys[i] = false;
  }
}

void draw() {
  background(col);

  if (keys[0] && keys[1]) {
    wasMultiPress = true;
  }
}

void updateMonth(int m) {
  switch (m) {

  case 1:  
    monthString = "January";
    break;
  case 2:  
    monthString = "February";
    break;
  case 3:  
    monthString = "March";
    break;
  case 4:  
    monthString = "April";
    break;
  case 5:  
    monthString = "May";
    break;
  case 6:  
    monthString = "June";
    break;
  case 7:  
    monthString = "July";
    break;
  case 8:  
    monthString = "August";
    break;
  case 9:  
    monthString = "September";
    break;
  case 10: 
    monthString = "October";
    break;
  case 11: 
    monthString = "November";
    break;
  case 12: 
    monthString = "December";
    break;
  default: 
    monthString = "Invalid month";
    break;
  }
  println(monthString);
}

void keyPressed() {
  if (key == '1') {
    keys[0]=true;
  }

  if (key == '2') {
    keys[1]=true;
}
}
void keyReleased() {
  if (key == '1') {
    keys[0]=false;
  }

  if (key == '2') {
    keys[1]=false;
  }

  if (!keys[0] && !keys[1] && wasMultiPress) {
    updateMonth(12);
    wasMultiPress = false;
  } else {
    updateMonth(Character.getNumericValue(key));
  }
} 

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [June 16, 2020, 11:07pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/5 "2020-06-16T23:07:39Z")

</div>

😀  
Thank you Mister.

And How could I do a case with for example the keypad DOWN and the keypad 1.  
Or the keypad A and the keypad 1 ?

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 17, 2020, 3:56pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/6 "2020-06-17T15:56:11Z")

</div>

You can up the number of array elements in `keys`, then add those checks in `keyPressed()/keyReleased()` …

then add to the if/else checking if multiple keys were selected or just single key.

At this point I would move that logic into it’s own function, ex. `checkIfMultipleKeys()` and invoke that in `keyReleased()`.

keep in mind most keyboards only detect 2 keys being pressed at same time.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 17, 2020, 8:13pm UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/7 "2020-06-17T20:13:46Z")

</div>

[Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G)

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [June 19, 2020, 11:09am UTC](https://discourse.processing.org/t/switch-case-with-two-key-pressed-is-it-possible/21869/8 "2020-06-19T11:09:26Z")

</div>

Ok it works with two keys together

```auto
static final String TABULAR = "\t";
static final color FG = #FF0000, BG = 255;
static final int KEYS = 0500, SMOOTH = 3;
static final float FPS = 120.0, WEIGHT = 1.0;
 
final boolean[] keysDown = new boolean[KEYS];
 
void setup() {
  size(01000, 0500);
  smooth(SMOOTH);
  frameRate(FPS);
 
  colorMode(RGB);
  strokeWeight(WEIGHT);
  stroke(BG);
}
 
void draw() {
  final int p = frameCount % width;
  line(p, 0, p, height);
  for (int k = 0; k < KEYS; ) if (isKeyDown(k++)) set(p, k, FG);
  
    processTwoKey();
}
 
void keyPressed() {
  processKey(keyCode, true);
  println(keyCode + TABULAR);
}
 
void keyReleased() {
  processKey(keyCode, false);
}
 
void processKey(final int k, final boolean isDown) {
  if (k < KEYS) keysDown[k] = isDown;
  

}

void processTwoKey (){//final int k, final boolean isDown) {
  
   if (keysDown[65]== true && keysDown[40]== true) // && keysDown[49] = true ) 

  println (" two keys together!!! ");
  
  }
 
boolean isKeyDown(final int k) {
  return keysDown[k];
}

```
