2021-07-10 arduino 用于矩阵键盘的 开源库函数 keypad.h使用说明

  • Post author:
  • Post category:其他


先实例化一个键盘:

确定行引脚

确定列引脚

const byte rows = 4; //4行
const byte cols = 4; //4列
char keys[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','K'},
  {'7','8','9','M'},
  {'#','0','*','O'}
};
byte rowPins[rows] = {5, 4, 3, 2}; //连接到行的引脚connect to the row pinouts of the keypad
byte colPins[cols] = {8, 7, 6}; //连接到列的引脚connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );//构建库函数,参数是制作键盘矩阵地图,行引脚,列引脚,行数,列数)




库函数

初始化并使硬件键盘产生映射(键盘地图 keys[rows][cols])

void begin(make Keymap(userKeymap))

请参见文件->示例->键盘->示例-> CustomKeypad


接受按键返回值


char getKey()


返回按键的状态(4个)



空闲,按下,释放,保持


KeyState getState()


按键状态的切换


boolean keyStateChanged()

设置用户必须按住按钮直到触发HOLD状态的毫秒数。

setHoldTime(unsigned int time)


设置延迟时间来实现键盘软件防抖动


setDebounceTime(unsigned int time)


增加键盘监听事件


addEventListener(keypadEvent)

监听键盘是按下,释放,还是保持?

详见下面具体示例2。

具体例子说明



接受键入示例:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'0','1','2','3'},
  {'4','5','6','7'},
  {'8','9','A','B'},
  {'C','D','E','F'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //例化键盘,例化名为customKeypad, 矩阵