Reason for being

How to use the KeypadEvent in your sketches. The event handler can be used for normal keypad handling routines like displaying characters but it really shines when you need to work with keypresses. For example you can detect the KEY_RELEASED event and take some action after the user releases the key.


Example

#include <Keypad.h>

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these pins, 
// eg. ROW0 = Arduino pin2.
byte rowPins[] = { 9, 8, 7, 6 };

// Connect keypad COL0, COL1 and COL2 to these pins, 
// eg. COL0 = Arduino pin6.
byte colPins[] = { 12, 11, 10 };

//create a new Keypad
Keypad keypad = Keypad(rowPins, colPins, sizeof(rowPins), sizeof(colPins));

#define ledPin 13 	// Use the LED on pin 13

void setup() 
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener
}

void loop() 
{
  keypad.getKey();
}


//take care of some special events
void keypadEvent(KeypadEvent eKey) {
  switch (eKey) {
  case '*': 
    digitalWrite(ledPin, HIGH); 
    break;
  case '#': 
    digitalWrite(ledPin, LOW); 
    break;
  case KEY_RELEASED:
    Serial.println("Key Released"); 
    break;
  default:
    if (eKey) {      // same as if(eKey != NO_KEY)
      Serial.println(eKey);
    }
  }
}

Share