How to escape while control structure with implement of switch case

Follow the complete library example, save the key value it is returned when a key is pressed

//add this
byte new_key;
void setup(){
// whatever you had
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    new_key = key;  //new_key is only updated when a key is pressed
  }
//then add your
//switch (new_key), 
//or the slower if (key == '1') etc.

}