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

Hello @vkbr,

This was my initial effort:

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:

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

*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 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:

:)