Anybody use the Keypad library?

I’m trying to monitor a 4x4 matrix of button sensors. I know that you can define custom keypads with this library. why is LIST_MAX defined as 10? I need to scan 16 sensors simultaneously.

I modified the example that comes with the lib

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; 
char keys[ROWS][COLS] = {
byte rowPins[ROWS] = {6,7,8,9}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {10,11,12,13}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long loopCount;
unsigned long startTime;
String msg;

void setup() {
    Serial.print("LIST_MAX: ");
    loopCount = 0;
    startTime = millis();
    msg = "";

void loop() {
    if ( (millis()-startTime)>5000 ) {
        Serial.print("Average loops per second = ");
        startTime = millis();
        loopCount = 0;

    // Fills kpd.key[ ] array with up-to 10 active keys.
    // Returns true if there are ANY active keys.
    if (kpd.getKeys())
        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
            if ( kpd.key[i].stateChanged )   // Only find keys that have changed state.
                switch (kpd.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
                    case PRESSED:
                    msg = " PRESSED.";
                    case HOLD:
                    msg = " HOLD.";
                    case RELEASED:
                    msg = " RELEASED.";
                    case IDLE:
                    msg = " IDLE.";
                Serial.print("Key ");
}  // End loop

lucasdachman: why is LIST_MAX defined as 10?

Just an arbitrary choice that that is usually enough and does not spend too much memory. Simply change it if you need more.

As oqibidipot said it was a somewhat arbitrary choice but to limit memory usage I based it on the midi standards. Also I assumed most humans were limited to 10 fingers. I made it configurable for those who might need more.

You should be able to increase that number without issue but understand that it is a list of C++ objects. Each entry on the list will consume several bytes of memory.

Best regards, Mark Stanley

great thank you! My project requires that all 16 buttons are able to be activated at the same time. I don’t think 16 key objects should take up too much memory. thanks for the info!

@mstanley do you think i'll have a problem with ghosting in my case?

Yeah, that's a very real possibility. It's fundamental to matrix decoding.

You could avoid the problems of ghosting by carefully building/coding your system so that it never uses close-together keypad values. This is really hard to do, though.

You could try increasing your keypad size in the sketch to use more pins and space out where you connect your sensors so that no more than 2 adjacent rows or columns are ever used at one time.

But the tried and true method is to use diodes across the columns/rows to prevent ghosting. If you can identify critical areas you can do like the keyboard manufacturers and just put diodes in the worst areas of the matrix.

Regards, Mark