pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Cardputer Keyboard

APIs and example programs related to Cardputer keyboard input, applicable to Cardputer and Cardputer-Adv.

Example Program

Compilation Requirements

  • M5Stack board manager version >= 3.2.2
  • Board option = M5Cardputer
  • M5Cardputer library version >= 1.1.0
  • M5Unified library version >= 0.2.8
  • M5GFX library version >= 0.2.10
Note
When using the keyboard, you need to include the M5Cardputer.update() function in the main loop to read state updates. Also, minimize blocking operations; otherwise, keyboard changes may not be captured in time.
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#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:

Keyboard_Class API

begin

Function prototype:

void begin();

Function description:

  • Initialize the keyboard

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:

  • null

Return value:

  • null

isChange

Function prototype:

bool isChange();

Function description:

  • Detect whether the keyboard state has changed. Triggered when any key on the keyboard is pressed or released.

Input parameters:

  • null

Return value:

  • bool
    • true: The keyboard state has changed
    • false: The keyboard state has not changed

isPressed

Function prototype:

uint8_t isPressed();

Function description:

  • Read the number of keys that are currently pressed

Input parameters:

  • null

Return value:

  • uint8_t
    • The number of keys being pressed

isKeyPressed

Function prototype:

bool isKeyPressed(char c);

Function description:

  • Check whether the specified key is currently pressed

Input parameters:

  • char c
    • The key name, such as 'A', 'a', '1', ',', ' ' (space), KEY_LEFT_SHIFT, KEY_BACKSPACE, KEY_ENTER, KEY_FN, etc.

Return value:

  • bool
    • true: The specified key is pressed
    • false: The specified key is not pressed

getKey

Function prototype:

uint8_t getKey(Point2D_t keyCoor);

Function description:

  • Return the decimal ASCII code corresponding to the key at the specified coordinates

Input parameters:

  • Point2D_t keyCoor
    • keyCoor.x: The horizontal coordinate of the key, range [0, 13], with 0 being the leftmost column
    • keyCoor.y: The vertical coordinate of the key, range [0, 3], with 0 being the top row

Return value:

  • uint8_t
    • The decimal ASCII code corresponding to the key
  • GitHub Source code of the keyboard section in the M5Cardputer library
On This Page