I want to press the B button for 2 seconds or longer to page up, and press the B button for 4 seconds or longer to page down.

Corresponding source code
BleKeyboard bleKeyboard ("Test_Switch", "kalon00");
void setup () {
  M5.begin ();
  bleKeyboard.begin ();
void loop () {
    if (M5.BtnB.wasReleasefor (2000)) {
    bleKeyboard.press (KEY_PAGE_UP);
  if (M5.BtnB.wasReleasefor (4000)) {
    bleKeyboard.press (KEY_PAGE_DOWN);
What I tried

I tried changing the number of seconds to press, but it didn't work. Is it wrong to use wasReleasefor in the first place?

Supplementary information (FW/tool version, etc.)

Arduino IDE 1.8.13
M5stickC Plus
Windows 10

  • Answer # 1

    According to the explanation that I examined the Button class of M5StickC

    wasReleasefor (x)

    Seems to be a function that returns True only once when the button is pressed for more than x milliseconds and then released.

    In the program in question, it is difficult to determine whether the button was pressed for more than 4 seconds or more than 2 seconds.

    The reason is that if the button is pressed for 4 seconds or more and then released, the probability of whether the condition of the first if statement in the loop becomes true or the condition of the second if statement becomes true is 50. This is because it seems that the operation will be in% increments.
    (If you press and release the button for more than 4 seconds, both M5.BtnB.wasReleasefor (2000) and M5.BtnB.wasReleasefor (4000) should return True, and at that moment which code in the loop (which one) Whether the if statement) is executed is fifty-fifty)

    After isReleased () is true, record the time when isPressed () becomes true, and then determine the time the button was pressed by the difference from the time when isReleased () became true. I think the common method of doing it is better.

  • Answer # 2

    If you want to use the information of M5.Btn, you need M5.update () ;.

    void loop () {
      M5.update ();