From 230d8e3c9b3ffd7cb43663eda3c3acdd73be133b Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Mon, 8 Jun 2020 02:59:00 +0200 Subject: [PATCH 1/2] Use shift state to invert next/prev LED effect key direction Handling of the respective keys is moved to `beforeReportingState` as the state of shift is only then available. Usage together with OneShot requires OneShot to be initialized before LEDControl. Signed-off-by: Johannes Becker --- src/kaleidoscope/plugin/LEDControl.cpp | 23 +++++++++++++++++++---- src/kaleidoscope/plugin/LEDControl.h | 1 + 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/kaleidoscope/plugin/LEDControl.cpp b/src/kaleidoscope/plugin/LEDControl.cpp index c43a8cca..92a62c9d 100644 --- a/src/kaleidoscope/plugin/LEDControl.cpp +++ b/src/kaleidoscope/plugin/LEDControl.cpp @@ -32,6 +32,7 @@ LEDMode *LEDControl::cur_led_mode_; uint8_t LEDControl::syncDelay = 32; uint16_t LEDControl::syncTimer = 0; bool LEDControl::enabled_ = true; +Key LEDControl::pending_next_prev_key_ = Key_NoKey; LEDControl::LEDControl(void) { } @@ -155,10 +156,10 @@ kaleidoscope::EventHandlerResult LEDControl::onKeyswitchEvent(Key &mappedKey, Ke return kaleidoscope::EventHandlerResult::OK; if (keyToggledOn(keyState)) { - if (mappedKey == Key_LEDEffectNext) { - next_mode(); - } else if (mappedKey == Key_LEDEffectPrevious) { - prev_mode(); + if (mappedKey == Key_LEDEffectNext || mappedKey == Key_LEDEffectPrevious) { + // Handling of these keys is delayed into `beforeReportingState` + // so that we can incorporate the shift modifier state. + pending_next_prev_key_ = mappedKey; } else if (mappedKey == Key_LEDToggle) { if (enabled_) disable(); @@ -174,6 +175,20 @@ kaleidoscope::EventHandlerResult LEDControl::beforeReportingState(void) { if (!enabled_) return kaleidoscope::EventHandlerResult::OK; + if (pending_next_prev_key_ != Key_NoKey) { + bool is_shifted = + kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_LeftShift) || + kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_RightShift); + + if ((pending_next_prev_key_ == Key_LEDEffectNext && !is_shifted) || + (pending_next_prev_key_ == Key_LEDEffectPrevious && is_shifted)) { + next_mode(); + } else { + prev_mode(); + } + pending_next_prev_key_ = Key_NoKey; + } + if (Runtime.hasTimeExpired(syncTimer, syncDelay)) { syncLeds(); syncTimer += syncDelay; diff --git a/src/kaleidoscope/plugin/LEDControl.h b/src/kaleidoscope/plugin/LEDControl.h index 123147a9..e416f1c8 100644 --- a/src/kaleidoscope/plugin/LEDControl.h +++ b/src/kaleidoscope/plugin/LEDControl.h @@ -161,6 +161,7 @@ class LEDControl : public kaleidoscope::Plugin { static uint8_t num_led_modes_; static LEDMode *cur_led_mode_; static bool enabled_; + static Key pending_next_prev_key_; }; class FocusLEDCommand : public Plugin { From 16e3324e28f68f7418048af922ee382b877bf437 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Sun, 7 Jun 2020 23:43:25 -0700 Subject: [PATCH 2/2] astyle Signed-off-by: Jesse Vincent --- src/kaleidoscope/plugin/LEDControl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kaleidoscope/plugin/LEDControl.cpp b/src/kaleidoscope/plugin/LEDControl.cpp index 92a62c9d..61d2dfc2 100644 --- a/src/kaleidoscope/plugin/LEDControl.cpp +++ b/src/kaleidoscope/plugin/LEDControl.cpp @@ -177,8 +177,8 @@ kaleidoscope::EventHandlerResult LEDControl::beforeReportingState(void) { if (pending_next_prev_key_ != Key_NoKey) { bool is_shifted = - kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_LeftShift) || - kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_RightShift); + kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_LeftShift) || + kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_RightShift); if ((pending_next_prev_key_ == Key_LEDEffectNext && !is_shifted) || (pending_next_prev_key_ == Key_LEDEffectPrevious && is_shifted)) {