# 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:** 1\
**Showing post:** 8

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

`:)`

---

_[View the full topic](https://discourse.processing.org/t/keypressed-combos-how-can-i-check-for-up-and-shift-in-a-switch/44148)._
