From d4f80b101ccde794121078f73defffc8e97e7ece Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Tue, 17 Nov 2020 15:31:36 -0600 Subject: [PATCH] Adapt ShapeShifter to keyboard state array Signed-off-by: Michael Richters --- .../src/kaleidoscope/plugin/ShapeShifter.cpp | 25 +++++++++++-------- .../src/kaleidoscope/plugin/ShapeShifter.h | 4 --- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.cpp b/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.cpp index c1fed6e9..3692c8c3 100644 --- a/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.cpp +++ b/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.cpp @@ -16,25 +16,21 @@ */ #include +#include "kaleidoscope/keyswitch_state.h" +#include "kaleidoscope/LiveKeys.h" namespace kaleidoscope { namespace plugin { const ShapeShifter::dictionary_t *ShapeShifter::dictionary = NULL; -bool ShapeShifter::mod_active_; - -EventHandlerResult ShapeShifter::beforeReportingState() { - mod_active_ = kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_LeftShift) || - kaleidoscope::Runtime.hid().keyboard().isModifierKeyActive(Key_RightShift); - return EventHandlerResult::OK; -} EventHandlerResult ShapeShifter::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { - if (!dictionary) + // Only act on keys that toggle on to prevent cycles (if the dictionary has + // two keys mapped to each other). + if (!keyToggledOn(key_state)) return EventHandlerResult::OK; - // If Shift is not active, bail out early. - if (!mod_active_) + if (!dictionary) return EventHandlerResult::OK; Key orig, repl; @@ -52,6 +48,15 @@ EventHandlerResult ShapeShifter::onKeyswitchEvent(Key &mapped_key, KeyAddr key_a if (orig == Key_NoKey) return EventHandlerResult::OK; + bool shift_detected = false; + + for (KeyAddr k : KeyAddr::all()) { + if (live_keys[k].isKeyboardShift()) + shift_detected = true; + } + if (! shift_detected) + return EventHandlerResult::OK; + repl = dictionary[i].replacement.readFromProgmem(); // If found, handle the alternate key instead diff --git a/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.h b/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.h index cdb2dc72..3b8ad7da 100644 --- a/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.h +++ b/plugins/Kaleidoscope-ShapeShifter/src/kaleidoscope/plugin/ShapeShifter.h @@ -33,10 +33,6 @@ class ShapeShifter : public kaleidoscope::Plugin { static const dictionary_t *dictionary; EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); - EventHandlerResult beforeReportingState(); - - private: - static bool mod_active_; }; }