APIs and example programs related to Cardputer keyboard input, applicable to Cardputer and Cardputer-Adv.
M5Cardputer.update()
function in the main loop to read state updates. Also, minimize blocking operations; otherwise, keyboard changes may not be captured in time.#include "M5Cardputer.h"
M5Canvas canvas(&M5Cardputer.Display);
String data = "> ";
void setup() {
auto cfg = M5.config();
M5Cardputer.begin(cfg, true); // enableKeyboard
M5Cardputer.Display.setRotation(1);
M5Cardputer.Display.setTextSize(0.5);
M5Cardputer.Display.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
M5Cardputer.Display.drawRect(0, 0, M5Cardputer.Display.width(), M5Cardputer.Display.height() - 28, GREEN);
M5Cardputer.Display.fillRect(0, M5Cardputer.Display.height() - 4, M5Cardputer.Display.width(), 4, GREEN);
canvas.setTextSize(0.5);
canvas.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
canvas.setTextScroll(true);
canvas.createSprite(M5Cardputer.Display.width() - 8, M5Cardputer.Display.height() - 36);
canvas.println("Press Key and Enter to Input Text");
canvas.pushSprite(4, 4);
M5Cardputer.Display.drawString(data, 4, M5Cardputer.Display.height() - 24);
}
void loop() {
M5Cardputer.update();
if (M5Cardputer.Keyboard.isChange()) {
if (M5Cardputer.Keyboard.isPressed()) {
Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();
for (auto i : status.word) {
data += i;
}
if (status.del) {
data.remove(data.length() - 1);
}
if (status.enter) {
data.remove(0, 2);
canvas.println(data);
canvas.pushSprite(4, 4);
data = "> ";
}
M5Cardputer.Display.fillRect(0, M5Cardputer.Display.height() - 28, M5Cardputer.Display.width(), 25, BLACK);
M5Cardputer.Display.drawString(data, 4, M5Cardputer.Display.height() - 24);
}
}
}
Running result as shown in the figure:
Function prototype:
void begin();
Function description:
When calling M5Cardputer.begin()
, you can set the parameter enableKeyboard
to true
to initialize the keyboard together.
M5Cardputer.begin(m5::M5Unified::config_t cfg, bool enableKeyboard);
Input parameters:
Return value:
Function prototype:
bool isChange();
Function description:
Input parameters:
Return value:
Function prototype:
uint8_t isPressed();
Function description:
Input parameters:
Return value:
Function prototype:
bool isKeyPressed(char c);
Function description:
Input parameters:
'A'
, 'a'
, '1'
, ','
, ' '
(space), KEY_LEFT_SHIFT
, KEY_BACKSPACE
, KEY_ENTER
, KEY_FN
, etc.Return value:
Function prototype:
uint8_t getKey(Point2D_t keyCoor);
Function description:
Input parameters:
Return value: