Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy solution for querying NumLock, CapsLock and ScrollLock #179

Open
kilr00y opened this issue Aug 4, 2022 · 0 comments
Open

Easy solution for querying NumLock, CapsLock and ScrollLock #179

kilr00y opened this issue Aug 4, 2022 · 0 comments

Comments

@kilr00y
Copy link

kilr00y commented Aug 4, 2022

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant