Functions

This function will wait forever until someone presses a key. Warning: It blocks all other code until a key is pressed. That means no blinking LED’s, no LCD screen updates, no nothing with the exception of interrupt routines.

char getKey()

Returns the key that is pressed, if any. This function is non-blocking.

KeyState getState()

Returns the current state of any of the keys.
The four states are IDLE, PRESSED, RELEASED and HOLD.

boolean keyStateChanged()

New in version 2.0: Let’s you know when the key has changed from one state to another. For example, instead of just testing for a valid key you can test for when a key was pressed.

setHoldTime(unsigned int time)

Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.

setDebounceTime(unsigned int time)

Set the amount of milliseconds the keypad will wait until it accepts a new keypress/keyEvent. This is the “time delay” debounce method.

addEventListener(keypadEvent)

Trigger an event if the keypad is used. You can load an example in the Arduino IDE.
[See File -> Examples -> Keypad -> Examples -> EventSerialKeypad] or see the KeypadEvent Example code.

For Now

Here’s the list of multi-keypress functions and the keylist definition.

Key key[LIST_MAX]

bool getKeys()

bool isPressed(char keyChar)

int findInList(char keyChar)

FAQ

How do I use multiple Keypads?

Keypad is a class. Therefore to use multiple Keypad, you must create an instance for each of them. In the example above, the Keypad instance keypad) was bound to the digital pins 2, 3, 4, 5, 6, 7 and 8.

To add a Keypad bound to digital pins 9, 10, 11, 12, 13, 14, 15 and 16, you could create the following instance keypad2:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

constbyteROWS2=4;//four rows

constbyteCOLS2=4;//four columns

charkeys2[ROWS2][COLS2]={

{'.','a','d','1'},

{'g','j','m','2'},

{'p','t','w','3'},

{'*',' ','#','4'}

};

byterowPins2[ROWS2]={12,11,10,9};//connect to the row pinouts of the keypad

bytecolPins2[COLS2]={16,15,14,13};//connect to the column pinouts of the keypad

And now it’s just a matter of using whatever function is wanted on each keypad:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

//update instances and possibly fire funcitons

voidloop(){

charkey1=keypad.getKey();

charkey2=keypad2.getKey();

if(key1!=NO_KEY||key2!=NO_KEY){

Serial.print("You pressed: ");

Serial.print(key1!=NO_KEY?key1:"nothing on keypad");

Serial.print(" and ");

Serial.print(key2!=NO_KEY?key2:"nothing on keypad2");

Serial.println(".");

}

}

How do I use setDebounceTime(unsigned int time)?

In Arduino follow the main menu from File-> Examples-> Keypad-> Examples->DynamicKeypad. Once the sketch is open find setup() and there you will see:

Arduino

1

2

3

4

5

6

7

voidsetup(){

Serial.begin(9600);

digitalWrite(ledPin,HIGH);// Turns the LED on.

keypad.addEventListener(keypadEvent);// Add an event listener.

keypad.setHoldTime(500);// Default is 1000mS

keypad.setDebounceTime(250);// Default is 50mS

}

This shows that the debounce time will allow one key press every 250 milliseconds. If multiple key presses occur within that time frame (as would happen when a key is bouncing) then those extra presses are simply ignored.