# keyPressed() combos - How can I check for UP and SHIFT in a switch()

**URL:** <https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148>\
**Category:** Coding Questions\
**Created:** [March 24, 2024, 3:52am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148 "2024-03-24T03:52:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [March 24, 2024, 3:52am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/1 "2024-03-24T03:52:59Z")

</div>

Hello folks.  
I have this gigantic switch for handling keys inputs.  
Thats all fine.

But I’m using “keyEvent.isShiftDown()” (sugested by chat gpt) and PDE complains:

“The field PApplet.keyEvent is deprecated”

How should I be doing it correctlly?  
thanks

```auto
// the line with the question is marked below you can just scroll down : )
void keyPressed() {
  switch(key) {
  case 'a':
  case 'A':
    z_speed -= 0.001;
    println("Z speed: " + z_speed);
    break;

  case 'z':
  case 'Z':
    z_speed += 0.001;
    println("Z speed: " + z_speed);
    break;

  case 's':
  case 'S':
    x_speed -= 0.001;
    println("X speed: " + x_speed);
    break;

  case 'x':
  case 'X':
    x_speed += 0.001;
    println("X speed: " + x_speed);
    break;

  case 'c':
  case 'C':
    y_speed -= 0.001;
    println("Y speed: " + y_speed);
    break;

  case 'd':
  case 'D':
    y_speed += 0.001;
    println("Y speed: " + y_speed);
    break;

  case 'q':
  case 'Q':
    z_speed = 0;
    println("Z speed: " + z_speed);
    break;

  case 'w':
  case 'W':
    x_speed = 0;
    println("X speed: " + x_speed);
    break;

  case 'e':
  case 'E':
    y_speed = 0;
    println("Y speed: " + y_speed);
    break;

  case 'p':
  case 'P':
    trav_speed = 0;
    println("trav_speed: " + trav_speed);
    break;

  case 'i':
  case 'I':
    trav_speed += 0.01;
    println("trav_speed: " + trav_speed);
    break;

  case 'o':
  case 'O':
    trav_speed -= 0.01;
    println("trav_speed: " + trav_speed);
    break;

  case CODED: //<<<<<<<<<<<<<<<<<<<< ONLY NEED TO LOOK FROM HERE <<<<<<<<<<<<<<
    switch(keyCode) {
    case LEFT:
      caixa.rotate(0, 1, 0, y_off_inc);
      break;

    case RIGHT:
      caixa.rotate(0, 1, 0, -1 * y_off_inc);
      println(x_off);
      break;

    case UP: 
      if (keyEvent.isShiftDown()) { //<-<-<-<-<-<-<-<-<- <<<<<<<<< <<<<<<< <<<<<<<
        println("SHIFT and UP pressed");   
      } else {
        caixa.rotate(1, 0, 0, x_off_inc);
        println(x_off);
      }
      break;

    case DOWN:
      caixa.rotate(1, 0, 0, -1 * x_off_inc);
      println(x_off_inc);
      break;
    }
    break;
  }
}

```

---

<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:** [March 24, 2024, 6:13am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/2 "2024-03-24T06:13:33Z")

</div>

Have you seen this reference: [https://processing.org/reference/keyCode.html](https://processing.org/reference/keyCode.html)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 24, 2024, 7:19am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/3 "2024-03-24T07:19:25Z")

</div>

> [@vkbr](#):
>
> deprecated

deprecated does not mean “wrong” or “not permitted”.

Does it work? Then you can ignore the message for now.

**Idea**

you can make a boolean shiftIsDown and set it: When shift is pressed set it to true and in the function keyReleased set it to false.

* * *

**Demo**

```auto

boolean shiftIsDown = false;

void setup() {
  size(660, 660);
  background(0);
}

void draw() {
  background(0);
  text(str(shiftIsDown), 19, 19);
}

// ----------------------------------------------------------------------------

void keyPressed() {
  if (keyCode==SHIFT)
    shiftIsDown = true;
}

void keyReleased() {
  if (keyCode==SHIFT)
    shiftIsDown = false;
}

```

* * *

**Demo 2 with println**

```auto

boolean shiftIsDown = false;

void setup() {
  size(660, 660);
  background(0);
}

void draw() {
  background(0);
  text(str(shiftIsDown), 19, 19);
}

// ----------------------------------------------------------------------------

void keyPressed() {
  if (key==CODED) {
    // CODED
    if (keyCode==SHIFT) {
      shiftIsDown = true;
    } else if (keyCode==UP) {
      if (shiftIsDown)
        println("shift-up");
      else
        println("up");
    }

    // leave
    return;
  }

  //-----
  // not CODED

  switch(key) {
  case 'a':
    println("a");
    break;
  }
}

void keyReleased() {
  if (keyCode==SHIFT)
    shiftIsDown = false;
}

```

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [March 24, 2024, 2:28pm UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/4 "2024-03-24T14:28:58Z")

</div>

Yes, sure. I was under the impression that I was missing something simple on how to integrate this in a `switch`. Thanks

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [March 24, 2024, 2:30pm UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/5 "2024-03-24T14:30:41Z")

</div>

Thanks @Chrisir. So nice yours demos \<3  
: )

I’ll use this approach fore sure.

---

<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:** [March 25, 2024, 10:26am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/6 "2024-03-25T10:26:48Z")

</div>

Hello @vkbr,

You can certainly build on concepts introduced along with your `switch` structures:

- `keyPressed()` and `keyReleased()`
- a variable to track if shift key is pressed or not
- two separate `switch` structures; one for _key_ and the other for _keyCode_
- You do not have to check ` if (key == CODED)`. Experiment with this.

Focus on readable code and then optimize.  
Good topic and it engaged me.  
I used a _switch/case_ structure and came out of it with a working and readable example and learned a lot.

Reference:

> **[Interactivity](https://processing.org/tutorials/interactivity/)**
>
> Introduction to interactivity with the mouse and keyboard.

`:)`

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [March 30, 2024, 3:32am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/7 "2024-03-30T03:32:02Z")

</div>

Thanks @glv  
would you care to share your “working and readable example” ?  
: )

---

<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:** [March 30, 2024, 7:39pm UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/8 "2024-03-30T19:39:49Z")

</div>

Hello @vkbr,

This was my initial effort:

```auto
boolean shiftPressed = false;

void setup()
  {
  background(255, 0, 0);
  }

void draw()
  {    
  }

void keyPressed()
  {
  //println(key, keyCode);
  
  switch(key) 
    {
    case 'a':
    case 'A':
      println("a or A:", key);
      break;
      
    // Add extra key cases here   
      
    }      

  switch(keyCode) 
    {
    case SHIFT:
      shiftPressed = true;
      println("shift pressed");
      background(0, 255, 0); // or in draw()
      break;      

    case UP:
      if (shiftPressed)
        {
        println("shift + up");
        background(0, 128, 255); // or in draw()
        }          
      
      if (!shiftPressed)
        println("up");
      
      break;   
        
    case LEFT:
      println("left");
      break;
      
    // Add extra keycode cases here  
            
    }
  }    
 
void keyReleased()
  {
  if(keyCode == SHIFT)
    {
    shiftPressed = false;
    background(255, 0, 0); // or in draw()
    println("shift released");
    }
  }

```

A search for “shift” in the forum came up with this which is similar to what I did:

> [@Key and coded key shift combo (cmd + something)](https://discourse.processing.org/t/key-and-coded-key-shift-combo-cmd-something/43199/15):
>
> Below the basics of how I set it up. There are three functions to handle the special keys and a function to handle the normal key. Those functions return a boolean that indicates if the function handled the key or not. It only shows some examples of possible keys / key combinations. Replace the print statements in the handleXXX functions by whatever needs to be done. boolean hasAlt = false; boolean hasCtrl = false; boolean hasShift = false; void setup() { // give the window the focus surf…

The triple nested if statements work and demonstrate concepts but not \*readable:

> [@Chrisir](#):
>
> ```auto
> void keyPressed() {
> if (key==CODED) {
> // CODED
> if (keyCode==SHIFT) {
> shiftIsDown = true;
> } else if (keyCode==UP) {
> if (shiftIsDown)
> println("shift-up");
> else
> println("up");
> }
> 
> // leave
> return;
> }
> 
> ```

\*Readable is subjective. My group of young programmers could understand the case statements but the nested if statements were a challenge and left for another day.

I focus on readable and maintainable code in my projects these days. Optimizing code and [making it lean](https://discourse.processing.org/t/10-print-chr-205-5-rnd-1-goto-10/15483/55) is in my blood from my embedded programming days but not always necessary and learning to break that habit when it is not necessary. It can be fun though and engages the lobes!

Reference:

> **[How can you balance performance and readability in your software code?](https://www.linkedin.com/advice/0/how-can-you-balance-performance-readability-your#)**
>
> Learn how to balance performance and readability in your software code with tips and best practices for software design.

`:)`

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [March 30, 2024, 7:57pm UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/9 "2024-03-30T19:57:29Z")

</div>

Ok. Thanks a lot @glv. It’s very clear indeed. I also value that.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 30, 2024, 8:46pm UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/10 "2024-03-30T20:46:16Z")

</div>

I made 2 demos.

When you understand the first,  
the second one is easier to read.

Nevertheless you can make new functions for codedKeys and notCodedKeys.

You can use switch () instead of if

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [March 31, 2024, 1:19am UTC](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148/11 "2024-03-31T01:19:43Z")

</div>

Oh yeah , I got them. 🙂
