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

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

// 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;
  }
}

1 Like

Have you seen this reference: https://processing.org/reference/keyCode.html

1 Like

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


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


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;
}

2 Likes

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

1 Like

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

I’ll use this approach fore sure.

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:

:)

1 Like

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

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:

:)

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

1 Like

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

2 Likes

Oh yeah , I got them. :slight_smile:

1 Like