Skip to content

Easy solution for querying NumLock, CapsLock and ScrollLock #179

@kilr00y

Description

@kilr00y

As I'm new to the whole ESP32/Arduino-Stuff and have no previous knowledge of C++, i spent way too much time figuring this out.

There is an old pull request for this feature, which however was never merged due to compatibility concerns with the Arduino Keyboard library found here. Since the structure of the current version has changed much since the pull request, it cannot be cherry picked. Now here's my (simple, but working) solution to the problem:

Add the following lines to the public section in BleKeyboard.h

bool NumLockOn;
bool CapsLockOn;
bool ScrollLockOn;

In BleKeyboard.cpp replace void BleKeyboard::onWrite(BLECharacteristic* me) with this new version and add the typedef block

typedef struct{
        uint8_t bmNumLock : 1;
        uint8_t bmCapsLock : 1;
        uint8_t bmScrollLock : 1;
        uint8_t bmReserved : 5;
} KbdLEDsType;

void BleKeyboard::onWrite(BLECharacteristic* me) {
  KbdLEDsType* myKbdLEDs = (KbdLEDsType*)(me->getValue().c_str());
  NumLockOn    = myKbdLEDs->bmNumLock;
  CapsLockOn   = myKbdLEDs->bmCapsLock;
  ScrollLockOn = myKbdLEDs->bmScrollLock;
}

You can now query the state of the special Keys by using the variables

bleKeyboard.NumLockOn
bleKeyboard.CapsLockOn
bleKeyboard.ScrollLockOn

in your Sketch. Not as elegant as using Callbacks, but the changes to the code are minimal and hence it should keep on working for some versions down the road.

I sure hope this will help somebody who walks down the same path as i did :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions