Trouble with Keypad library unable to detect HOLD key status

I am unable to get a HOLD status from a key press even though I hold it down for a long time. The only response seems to be when I initially press a key. The attached file is a sample serial monitor output. It represents 4 key presses: first f key, second f key held for about 2 minutes, third 1 key, then 1 key again but held for more than a minute.

I must not be understanding the return from the event listener or getstate function.

Suggestions?

[code[#include <Arduino.h>

#include <Key.h>
#include <Keypad.h>

// matrix keys are attached to pins 2 through 9

const byte ROWS=4; //four rows
const byte COLS=4; //four columns
char keys[ROWS][COLS]=
{
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','a','b'},
{'c','d','e','f'}
};
byte rowPins[ROWS]={5,4,3,2};
byte colPins[COLS]={9,8,7,6};

Keypad keypad = Keypad( makeKeymap(keys), rowPins,colPins, ROWS, COLS );
char key;

// Taking care of some special events.

void keypresent(){
Serial.println(key);

switch (keypad.getState ()){

case RELEASED:
Serial.print("Released key # " );
Serial.println(key);
break;
case HOLD:
Serial.print("Hold key # " );
Serial.println(key);
break;
case IDLE:
Serial.print("Idle key # " );
Serial.println(key);
break;
case PRESSED:
Serial.print("Pressed key # " );
Serial.println(key);
break;
}

}

void setup()
{
Serial.begin(9600);
// now we can begin to set up the key matrix handling

//keypad.addEventListener(keypadEvent); //add an event listener
keypad.setHoldTime(2000); //set key hold time to 2 seconds
keypad.setDebounceTime(25); //set debounce time to 25 millisecond

Serial.println("setup complete");
}

void loop() {

key = keypad.getKey();
// Serial.print("each tme through the loop");
// Serial.println(key);
// delay(25);
if(key != NO_KEY ){
Serial.println(key);
keypresent();
}

}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
Serial.println("keypad event entry");
switch (keypad.getState()){

case IDLE:{
Serial.println("idle state");
}
case HOLD:{

Serial.print("key held ");
Serial.println(key);
}
break;

case RELEASED:
if (key == 'c') {
Serial.println("key was released");

}
break;

case PRESSED:{

Serial.print("key pressed ");
Serial.println(key);
}
break;
}
Serial.println("exit keypad event");
}/code]

Key test.txt (110 Bytes)

FYI..

your codes tags are broken/invalid.. :slight_smile:

Can you be more specific when you say "codes tags" are invalid? I copied the code from the example in the keypad library. What should they be?

At the top of every forum topic is a sticky thread in bold named how to use this forum-please read. Somehow it is easy to miss. It tells how to properly post code.

There's also a link on the top of every main forum page very cleverly titled Read this before posting a programming question ...

I know that name is kind of cryptic. But, you should have, you know, read it before posting a programming questions. Please do so now and pay particular attention to Item #6 which tells you how to properly post your code using Code Tags.

I will attempt to correct the code/tag errors. BUT since I had read the topics on how to post your comments were NOT helpful at all. Rather than just say "your code/tags are broken/invalid", you could have pointed out that I had inserted the sketch code in the wrong place between the code/tags. Then you might have been able to not only help me but also others from making the same mistake.

#include <Arduino.h>


#include <Key.h>
#include <Keypad.h>

// matrix keys are attached to pins 2 through 9

const byte ROWS=4; //four rows
const byte COLS=4; //four columns
char keys[ROWS][COLS]=
{
  {'0','1','2','3'},
  {'4','5','6','7'},
  {'8','9','a','b'},
  {'c','d','e','f'}
};
byte rowPins[ROWS]={5,4,3,2};
byte colPins[COLS]={9,8,7,6};

Keypad keypad = Keypad( makeKeymap(keys), rowPins,colPins, ROWS, COLS );
char key;



// Taking care of some special events.

void  keypresent(){
    Serial.println(key);
    
   
    switch (keypad.getState ()){
      
      case RELEASED:
        Serial.print("Released key # " );
        Serial.println(key);
        break;
      case HOLD:
        Serial.print("Hold key # " );
        Serial.println(key);
        break;
      case IDLE:
        Serial.print("Idle key # " );
        Serial.println(key);
        break;  
      case PRESSED:
        Serial.print("Pressed key # " );
        Serial.println(key);
        break;    
    }


    }
    


  
void setup()
{
  Serial.begin(9600);
            //  now we can begin to set up the key matrix handling 

  //keypad.addEventListener(keypadEvent);   //add an event listener             
  keypad.setHoldTime(2000);               //set key hold time to 2 seconds
  keypad.setDebounceTime(25);            //set debounce time to 25 millisecond

  Serial.println("setup complete");
    } 


void loop() {

  
      key = keypad.getKey();
//         Serial.print("each tme through the loop");
//         Serial.println(key);
//         delay(25);
      if(key != NO_KEY ){
        Serial.println(key);
        keypresent();
      }

}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
    Serial.println("keypad event entry");
    switch (keypad.getState()){
      
    case IDLE:{
          Serial.println("idle state");
    }
    case HOLD:{
       
          Serial.print("key held ");
          Serial.println(key);
    }
        break;

    case RELEASED:
        if (key == 'c') {
            Serial.println("key was released");

        }
        break;

    case PRESSED:{
    
          Serial.print("key pressed ");
          Serial.println(key);        
        }
        break;
    }
    Serial.println("exit keypad event");
}

I will post it exactly as it is..

You had broken/invalid code tags.. period.

You attempted to do so.. but messed them up. Operator error.

You can just highlight all your 'code' (text) and hit the code button to format it as well I believe..

Why do you have keypad.addEventListener(); commented out?

Thanks for the notice that I had commented out the eventlistner routine. Bringing it back inline has allowed me to move forward.