Keypad Event

Hi, I'm confused about Keypad Events in the keypad library. Could someone just quickly explain what it does?

Hi JoeWawaw,

I've seen people use Keypad Events for various things but the main reason it's there is to let you know when a key was activated and which state it is in. There are four states; IDLE, PRESSED, HOLD, and RELEASED. In the beginning the main thing I wanted know was when a user was HOLDing a key down. As I worked through the problem of how to make that happen I came up with a way to determine all four states. Now it more closely matches what your keyboard does with your computer.

Very quickly, the PRESSED state occurs first, right when the users presses a key.

Following PRESSED the next state can be either HOLD or RELEASED.

If the key is pressed longer than 500 milliseconds (default) you will reach the HOLD state. However, if the key was pressed and released in less than 1/2 a second then the next state is RELEASED.

The RELEASED state occurs after a PRESSED or HOLD state and only after the user lets go of the key.

Finally, the IDLE state occurs only after the RELEASED state. I consider IDLE to be the default state where nothing is happening.

Knowing when a key is pressed, released, or being held provides a great way to simplify some coding problems.

Example: You want to see just how fast you can press a key. If you start a counter when you get the PRESSED event then you can simply stop the counter and display the results when you get the RELEASED event.

Example: You want a single key to do more than one thing. Detect the HOLD event and change the function of the key. If the users HOLDs the key a second time then switch back to the original function.

If you have something in mind just let me know and I can help you simplify your code.

Thanks a lot!

Can you point me to an example for knowing if the '1' key is being held? I have a volume up/down on key '1' and '3' respectively. I have it working one press at a time but need to do a hold and speed up the volume change.

Can you point me to an example for knowing if the '1' key is being held? I have a volume up/down on key '1' and '3' respectively. I have it working one press at a time but need to do a hold and speed up the volume change.

How about you posting YOUR code?

This is my first post, so apologies if I am putting it in the wrong place.

I am sending commands to a ptz camera from a matrix keypad. Reading the keypad and sending hex command strings is fine, but the pan command starts the camera panning, and it needs a different command to stop, so I want to test the pan key to find out when it is released, and then send the stop command. I am trying to use the addEventListener but I have clearly got the syntax wrong, and it is not compiling. Any help would be appreciated.

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
byte panup[9] = {0x81,0x01,0x06,0x01,0x10,0x10,0x03,0x01,0xFF};
byte panstop[9] = {0x81,0x01,0x06,0x01,0x10,0x10,0x03,0x03,0xFF};
byte camera[4] = {0x88,0x30,0x01,0xFF};
byte interface[5] = {0x88,0x01,0x00,0x01,0xFF};
byte infrared_on[6]= {0x81,0x01,0x06,0x08,0x02,0xFF};
byte infrared_off[6]= {0x81,0x01,0x06,0x08,0x03,0xFF};

const byte ROWS = 9; //nine rows
const byte COLS = 3; //three columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'5','m','+'},
{'4','c','x'},
{'3','=','A'},
{'2','9','#'},
{'1','m','+'},
{'0','c','x'},
{'6','=','A'},
{'7','9','#'},
{'8','m','+'},
};
byte rowPins[ROWS] = {2,3,4,5,6,7,8,9,10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad elmopad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
Serial.begin(9600);
elmopad.addEventListener(elmopadEvent); // Add an event listener for this keypad
}

void loop(){
char elmokey = elmopad.getKey();

if (elmokey=='7')
{Serial.write(camera,sizeof(camera));}
if (elmokey=='5')
{Serial.write(panup,sizeof(panup));
while(elmopad.getState()==PRESSED)
Serial.write(panstop,sizeof(panstop));
}
if (elmokey=='8')
{Serial.write(interface,sizeof(interface));}
if (elmokey=='1')
{Serial.write(infrared_on,sizeof(infrared_on));}
if (elmokey=='2')
{Serial.write(infrared_off,sizeof(infrared_off));}

}

I am trying to use the addEventListener but I have clearly got the syntax wrong, and it is not compiling.

What error messages do you get?

I keep changing the code, so this may not be quite right, but something like elmopadevent not declared in this scope, but in the examples I have looked at I don't see a declaration
Thanks

It's not just a declaration you need, you'll have to write the event handler function. Check this example in the playground: http://playground.arduino.cc//KeypadTutorial/EventKeypad. The keypadEvent function passed to addEventListener is defined in the sketch.

Ok thanks, I must have another go at that, I had read it, but for a soldering iron man like me, I was having trouble transferring it.

wildbill - Thank you for prompting me to read that example again. I decided the switch function was probably easier than the while function, and I have successfully watched for PRESSED state and sent the pan command, then watched for RELEASED and sent the stop command, so the camera pans while I hold the button down.
Now I can get on and write the other commands in a similar way!!

Please can you post the final working code. It will really be helpful to my project as I'M having almost the same issue as yours