Sync the NUMPAD layer state with the host

Instead of trying to track numlock ourselves, rely on the host telling us what
it thinks the state is. This is much more reliable than what we were doing, and
hopefully fixes most of - if not all - the issues we were having with NumLock.

Signed-off-by: Gergely Nagy <kaleidoscope@gergo.csillger.hu>
pull/365/head
Gergely Nagy 7 years ago
parent 393bba00ba
commit a1c0fdfb42

@ -5,15 +5,29 @@
byte NumLock_::row = 255, NumLock_::col = 255; byte NumLock_::row = 255, NumLock_::col = 255;
uint8_t NumLock_::numPadLayer; uint8_t NumLock_::numPadLayer;
bool NumLock_::isOn;
cRGB numpad_color = CRGB(255, 0, 0); cRGB numpad_color = CRGB(255, 0, 0);
void NumLock_::begin(void) { void NumLock_::begin(void) {
Kaleidoscope.useLoopHook(loopHook); Kaleidoscope.useLoopHook(loopHook);
Kaleidoscope.useEventHandlerHook(eventHandlerHook);
} }
void NumLock_::loopHook(bool postClear) { void NumLock_::loopHook(bool postClear) {
if (!postClear || !Layer.isOn(numPadLayer)) if (!postClear)
return;
bool numState = !!(Keyboard.getLEDs() & LED_NUM_LOCK);
if (numState != isOn) {
isOn = numState;
if (isOn) {
Layer.on(numPadLayer);
} else {
Layer.off(numPadLayer);
LEDControl.set_mode(LEDControl.get_mode_index());
}
}
if (!isOn)
return; return;
for (uint8_t r = 0; r < ROWS; r++) { for (uint8_t r = 0; r < ROWS; r++) {
@ -36,21 +50,4 @@ void NumLock_::loopHook(bool postClear) {
LEDControl.setCrgbAt(row, col, color); LEDControl.setCrgbAt(row, col, color);
} }
Key NumLock_::eventHandlerHook(Key key, byte row, byte col, uint8_t key_state) {
if (key != Key_KeypadNumLock)
return key;
if (!keyToggledOn(key_state))
return key;
if (Layer.isOn(numPadLayer)) {
Layer.off(numPadLayer);
LEDControl.set_mode(LEDControl.get_mode_index());
} else {
Layer.on(numPadLayer);
}
return key;
}
NumLock_ NumLock; NumLock_ NumLock;

@ -14,9 +14,9 @@ class NumLock_ : public KaleidoscopePlugin {
private: private:
static void loopHook(const bool postClear); static void loopHook(const bool postClear);
static Key eventHandlerHook(Key key, byte row, byte col, uint8_t key_state);
static byte row, col; static byte row, col;
static bool isOn;
}; };
extern NumLock_ NumLock; extern NumLock_ NumLock;

Loading…
Cancel
Save