diff --git a/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.cpp b/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.cpp index d8014d7994..6f609ad604 100644 --- a/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.cpp +++ b/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.cpp @@ -107,6 +107,36 @@ void DynamicMacros::play(uint8_t macro_id) { if (macro_id >= macro_count_) return; + auto &storage = Runtime.storage(); + // Define a lambda function for common key operations to reduce redundancy + auto setKeyAndAction = [this, &key, ¯o, &pos]() { + // Keycode variants of actions don't have flags to set, but we want to make sure + // we're still initializing them properly. + + key.setFlags((macro == MACRO_ACTION_STEP_KEYCODEDOWN || macro == MACRO_ACTION_STEP_KEYCODEUP || MACRO_ACTION_STEP_TAPCODE) ? 0 + : Runtime.storage().read(pos++)); + + key.setKeyCode(Runtime.storage().read(pos++)); + + switch (macro) { + case MACRO_ACTION_STEP_KEYCODEDOWN: + case MACRO_ACTION_STEP_KEYDOWN: + this->press(key); + break; + case MACRO_ACTION_STEP_KEYCODEUP: + case MACRO_ACTION_STEP_KEYUP: + this->release(key); + break; + case MACRO_ACTION_STEP_TAP: + case MACRO_ACTION_STEP_TAPCODE: + this->tap(key); + break; + default: + break; + } + }; + + pos = storage_base_ + map_[macro_id]; while (pos < storage_base_ + storage_size_) { @@ -126,60 +156,27 @@ void DynamicMacros::play(uint8_t macro_id) { } case MACRO_ACTION_STEP_KEYDOWN: - key.setFlags(Runtime.storage().read(pos++)); - key.setKeyCode(Runtime.storage().read(pos++)); - press(key); - break; case MACRO_ACTION_STEP_KEYUP: - key.setFlags(Runtime.storage().read(pos++)); - key.setKeyCode(Runtime.storage().read(pos++)); - release(key); - break; case MACRO_ACTION_STEP_TAP: - key.setFlags(Runtime.storage().read(pos++)); - key.setKeyCode(Runtime.storage().read(pos++)); - tap(key); - break; - - case MACRO_ACTION_STEP_KEYCODEDOWN: - key.setFlags(0); - key.setKeyCode(Runtime.storage().read(pos++)); - press(key); - break; case MACRO_ACTION_STEP_KEYCODEUP: - key.setFlags(0); - key.setKeyCode(Runtime.storage().read(pos++)); - release(key); - break; case MACRO_ACTION_STEP_TAPCODE: - key.setFlags(0); - key.setKeyCode(Runtime.storage().read(pos++)); - tap(key); + case MACRO_ACTION_STEP_KEYCODEDOWN: + setKeyAndAction(); break; - case MACRO_ACTION_STEP_TAP_SEQUENCE: { - while (true) { - key.setFlags(Runtime.storage().read(pos++)); - key.setKeyCode(Runtime.storage().read(pos++)); - if (key == Key_NoKey) - break; - tap(key); - delay(interval); - } - break; - } + case MACRO_ACTION_STEP_TAP_SEQUENCE: case MACRO_ACTION_STEP_TAP_CODE_SEQUENCE: { + bool isKeycodeSequence = macro == MACRO_ACTION_STEP_TAP_CODE_SEQUENCE; while (true) { - key.setFlags(0); - key.setKeyCode(Runtime.storage().read(pos++)); - if (key.getKeyCode() == 0) + key.setFlags(isKeycodeSequence ? 0 : storage.read(pos++)); + key.setKeyCode(storage.read(pos++)); + if (key == Key_NoKey) break; tap(key); delay(interval); } break; } - case MACRO_ACTION_END: default: return;