Help to understand keypad code.

Hello.

I am quite new to programming but I am reading and learning non stop, however I have hit a wall and I can't figure it out.

I have a keypad that in the end will be used to enter a target voltage level for a adjustable power supply, but I can't even understand how the following code works.

/* Numeric keypad and I2C LCD
   http://tronixstuff.com/tutorials > chapter 42
   Uses Keypad library for Arduino
   http://www.arduino.cc/playground/Code/Keypad
   by Mark Stanley, Alexander Brevig */
 
#include "Keypad.h"
#include "Wire.h" // for I2C LCD
#include "TWILiquidCrystal.h"

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {
  5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  9, 8, 7, 6}; //connect to the column pinouts of the keypad
int count=0;
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Keypad test!");  
  delay(1000);
  lcd.clear();
}
 
void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    lcd.print(key);
    Serial.print(key);
    count++;
    if (count==17)
    {
      lcd.clear();
      count=0;
    }
  }
}

Lets focus on the loop, this code prints out on the lcd every key you hit in succession one after another.
And the following loop prints out the key that you press but when you press a new key it prints out at the same location overwriting the first:

void loop()
{
/*   char key = keypad.getKey();
  
  if (int(key) != 0) 
  {
    lcd.setCursor(0,2);
    lcd.print(key);
    Serial.println(key);
  }*/

Although I understand what the code says I don't follow how it all works out as it does, but my real problem is that I don't understand how I would do to be able to read each key press so that I can do something else with the information.
How to store each press in a global variable, or something like that?
I can't really ask for something specific seeing as I am totally lost.

And the following loop prints out the key that you press

It does nothing of the sort - it's all comment

The function keypad.getKey(); will give you the key you pressed.
If you want, for example to keep 5 keys in a variable, and write that key to the LCD, you can do something like this:

/* Numeric keypad and I2C LCD
   http://tronixstuff.com/tutorials > chapter 42
   Uses Keypad library for Arduino
   http://www.arduino.cc/playground/Code/Keypad
   by Mark Stanley, Alexander Brevig */
 
#include "Keypad.h"
#include "Wire.h" // for I2C LCD
#include "TWILiquidCrystal.h"

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{{'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {
  5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  9, 8, 7, 6}; //connect to the column pinouts of the keypad
int count=0;
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

///// ADD THIS ARRAY VARIABLE TO STORE THE KEYS /////
#define MAX_KEYS 5
char hit_keys[MAX_KEYS];

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Keypad test!");  
  delay(1000);
  lcd.clear();
}
 
void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    lcd.print(key);
    Serial.print(key);
    hit_keys[count] = key;   // this line will "save" the keys in the NEW VARIABLE
    count++;
    if (count>=MAX_KEYS)   // I change the this line to only hold 5 keys
    {
      lcd.clear();
      count=0;
      Serial.println(";");   // I add this line to start pint the new "set" of keys in a new line
    }
  }
}

Do you understand this code?