Arduino Mega 2560 - Keypad 4X4

You need a simple state machine. So you have to save your current state to variable and check the key presses depending on the value of that state variable. In it's simplest form this is kind of:

if (state == 1) {
  if (key == 'A') {
    // do something
    state = 2;
  }
} else if (state == 2) {
  if (key == 'B') {
    // do anything
    state = 3;
  }
} else if (state == 3) {
  if (key == 'C') {
    state = 1;
  }
}

I hope you got the point.