# Unable to make my macro work with new firmware

**URL:** <https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195>\
**Category:** Programming\
**Tags:** question, plugin\
**Created:** [February 7, 2022, 9:47am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195 "2022-02-07T09:47:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![rloic](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/rloic/32/3491_2.png) [@rloic](https://community.keyboard.io/u/rloic)\
**Post date:** [February 7, 2022, 9:47am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/1 "2022-02-07T09:47:44Z")

</div>

Hello,

I just upgrade my atreus from kaleidoscope 1.99.3 to 1.99.5 and I struggled to update some macro.

I have macro that do different thing if modifier is pressed or not. A simple example :

```auto
  case KEY_COMMA:
    if (keyToggledOn(event.state)) {
      if (Kaleidoscope.hid().keyboard().wasModifierKeyActive(Key_LeftShift)) {
        return MACRO(I(15), U(LeftShift), T(Comma), D(LeftShift));
      } else {
        return MACRO(I(15), T(M));
      }
    }

```

I was using MACRODOWN method but just migrate to use new way.  
My problem is when LeftShift is held I need a macro with no modifier active. I formerly solve this by begining Macro releasing the modifier key. But with the last version it does not work anymore.

I try unsuccessfully to use `Kaleidoscope.hid().keyboard().clearModifier()` or `Runtime.handleKeyEvent()` to do so.

Does anyone have an idea to solve this ?

Thanks

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [February 7, 2022, 3:36pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/2 "2022-02-07T15:36:59Z")

</div>

I have two solutions for you for the above macro:

1. A special purpose “ShiftBlocker” plugin, submitted as a [PR with example code](https://github.com/keyboardio/Kaleidoscope/pull/1097).
2. The existing [CharShift plugin](https://kaleidoscope.readthedocs.io/en/latest/plugins/Kaleidoscope-CharShift.html).

The problem you’re having is that `MACRO()` can’t release a key that wasn’t pressed by the Macros plugin. Since Kaleidoscope’s event-driven redesign, the Macros plugin works by pressing and releasing keys on a small supplemental virtual keyboard, and cannot effect keys held on the main keyboard by calling `Macros.release()`, which is what `U(LeftShift)` does, because that will only release that key on the supplemental keyboard.

I would be happy to look at the rest of your Macros code and see if there are any that need more complex adjustments, and I can also help you set up CharShift (or the custom ShiftBlocker plugin) if you want.

---

<div class="post-metadata">

**Author:** ![rloic](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/rloic/32/3491_2.png) [@rloic](https://community.keyboard.io/u/rloic)\
**Post date:** [February 7, 2022, 3:57pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/3 "2022-02-07T15:57:36Z")

</div>

Thanks.

I just have a quick look at your solutions and believe the “ShiftBlocker” plugin is what I need.  
The CharShift plugin would not do the job for some case where I use different macro depending on OS and alt codes for windows.

If you want to take a look to my code : [https://github.com/rloic82/Kaleidoscope/blob/feature/fr\_azerty/examples/Devices/Keyboardio/AtreusBepo/AtreusBepo.ino](https://github.com/rloic82/Kaleidoscope/blob/feature/fr_azerty/examples/Devices/Keyboardio/AtreusBepo/AtreusBepo.ino)

I think I will be able to implement the ShiftBlocker. I will come back as soon as I did it.

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [February 7, 2022, 4:23pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/4 "2022-02-07T16:23:33Z")

</div>

I also recommend using the following instead of `wasModifierKeyActive()`:

```auto
bool isShiftKeyHeld() {
  for (Key key : kaleidoscope::live_keys.all()) {
    if (key.isKeyboardShift())
      return true;
  }
  return false;
}

```

Unlike `wasModifierKeyActive()` (and `isModifierKeyActive()`, which is no longer problematic), you won’t get a false positive for a key with a modifier flag (e.g. `LSHIFT(Key_1)`).

_[Edit: fixed typo in the code.]_

---

<div class="post-metadata">

**Author:** ![rloic](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/rloic/32/3491_2.png) [@rloic](https://community.keyboard.io/u/rloic)\
**Post date:** [February 8, 2022, 10:47am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/5 "2022-02-08T10:47:33Z")

</div>

@merlin I implement your ShiftBlocker plugin which was exactly what I need. All my macros work as I want again.

Thanks a lot for helping me.

Just for those who will use your ` isShiftKeyHeld()` function, I notice a little mistake on line

```auto
for (Key key : kaleidoscope:live_keys.all()) {

```

but it should be

```auto
for (Key key : kaleidoscope::live_keys.all()) {

```

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [February 8, 2022, 1:42pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/6 "2022-02-08T13:42:44Z")

</div>

Thanks for catching my typo! Sorry about that.

---

<div class="post-metadata">

**Author:** ![nikkez](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/nikkez/32/4098_2.png) [@nikkez](https://community.keyboard.io/u/nikkez)\
**Post date:** [February 22, 2022, 8:18pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/7 "2022-02-22T20:18:37Z")

</div>

@merlin thank you for your suggestions. It also helped me a lot. I managed to convert most of my macros to CharShift. I still have a few problems with tilde.

I’m trying to get the default layout so that the rightmost key of the left side is backtick when pressed normal and tilde when pressed with shift. On the finnish keyboard the tilde is achieved pressing MACRO(D(RightAlt), T(RightBracket, U(RightAlt), T(Space)).

I cannot figure out how to use the ShiftBlocker to achieve these combinations.

Any help is appreciated.

The repository is here [finnish-atreus/MyAtreus.ino at master · nikkez/finnish-atreus · GitHub](https://github.com/nikkez/finnish-atreus/blob/master/MyAtreus/MyAtreus.ino).

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [February 23, 2022, 4:11am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/8 "2022-02-23T04:11:55Z")

</div>

Your whole `macroAction()` function is the deprecated version, so it needs to be replaced with the newer `KeyEvent` version. I haven’t tested this code, so there may be some errors in it, but if I got it all right, it should serve as a replacement for your old `macroAction()` function:

```nohighlight
const macro_t *macroAction(uint8_t macro_id, KeyEvent &event) {
  // All these macros only play when a key toggles on, so it's more efficient to
  // just test for that once.
  if (keyToggledOn(event.state)) {

    switch (macro_id) {

    case MACRO_VERSION_INFO:
      Macros.type(PSTR("Keyboardio Atreus - Kaleidoscope "));
      Macros.type(PSTR(BUILD_INFORMATION));
      break;

    case M_BACKTICK_AND_TILDE:
      // Check for any `shift` keys that are currently being held.
      bool shift_active = false;
      for (Key key : live_keys.all()) {
        if (key.isKeyboardShift())
          shift_active = true;
      }
      if (shift_active) {
        // If one or more `shift` keys are held, we can't use `MACRO()` because
        // we need to disable ShiftBlocker when we're done.
        ShiftBlocker.enable();
        Macros.tap(RALT(Key_RightBracket));
        Macros.tap(Key_Space);
        ShiftBlocker.disable();
      } else {
        // It's more efficient to use a single `Tr()` with a key value that
        // contains a modifier flag than to use separate commands for pressing
        // and releasing the modifier.
        return MACRO(Tr(LSHIFT(Key_Equals)), T(Space));
      }
      break;

    case M_CARET:
      return MACRO(Tr(LSHIFT(Key_RightBracket)), T(Space));

    case M_PARENTHESIS:
      return MACRO(D(LeftShift), T(8), T(9), U(LeftShift), T(LeftArrow));

    case M_ANGLES:
      return MACRO(T(NonUsBackslashAndPipe), Tr(LSHIFT(Key_NonUsBackslashAndPipe)), T(LeftArrow));

    case M_DOUBLE_EQUALS:
      return MACRO(D(LeftShift), T(0), T(0), U(LeftShift));

    case M_ALT_SHIFT_COMMA:
      // Modifier flags can be combined.
      return MACRO(Tr(LSHIFT(LALT(Key_Comma))));

    case M_ALT_SHIFT_X_T:
      // Modifier flag preprocessor macros are not sensitive to the order in
      // which they are applied to the key value.
      return MACRO(Tr(LALT(LSHIFT(Key_X))), T(T));

    default:
      break;
    }
  }
  return MACRO_NONE;
}

```

If you have any further questions, or if something doesn’t work properly, don’t hesitate to ask again!

---

<div class="post-metadata">

**Author:** ![nikkez](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/nikkez/32/4098_2.png) [@nikkez](https://community.keyboard.io/u/nikkez)\
**Post date:** [February 28, 2022, 6:45pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/9 "2022-02-28T18:45:21Z")

</div>

Thank you for your help! Now it works.

There is still one problem but I’m not sure what causes it. For some reason sometimes the macro is not played or the resulting keypresses are not visible.

I added some printlines to serial to verify this. The execution is inside the macroAction function and I get the output to serial that certain key is pressed but nothing is output from the keyboard.

The code is here [finnish-atreus/MyAtreus.ino at master · nikkez/finnish-atreus · GitHub](https://github.com/nikkez/finnish-atreus/blob/master/MyAtreus/MyAtreus.ino).

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [February 28, 2022, 7:26pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/10 "2022-02-28T19:26:48Z")

</div>

It is just in the case where ShiftBlocker is getting activated, or is it happening for other macros, too?

---

<div class="post-metadata">

**Author:** ![nikkez](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/nikkez/32/4098_2.png) [@nikkez](https://community.keyboard.io/u/nikkez)\
**Post date:** [February 28, 2022, 9:46pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/11 "2022-02-28T21:46:52Z")

</div>

It is happening for other macros too.

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [March 1, 2022, 4:07am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/12 "2022-03-01T04:07:23Z")

</div>

Thanks. I’ll try to reproduce this when I get a chance, but I don’t currently have a hypothesis that fits.

---

<div class="post-metadata">

**Author:** ![rloic](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/rloic/32/3491_2.png) [@rloic](https://community.keyboard.io/u/rloic)\
**Post date:** [March 1, 2022, 8:29am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/13 "2022-03-01T08:29:17Z")

</div>

Whereas I use a lot of macro, I have no issues with them.  
I see only 2 differences between our codes.

- Your enum lists ended with a comma which, in my mind, means a continuation but there is not.
- To avoid multiple repetition of the test `if (keyToggledOn(event.state))` you put it before the switch code.

Perhaps one of this point is the cause of your problem but not sure at all.

---

<div class="post-metadata">

**Author:** ![nikkez](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/nikkez/32/4098_2.png) [@nikkez](https://community.keyboard.io/u/nikkez)\
**Post date:** [March 1, 2022, 3:33pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/14 "2022-03-01T15:33:49Z")

</div>

Thank you @rloic and @merlin for your help! I was testing the keyboard only in Ubuntu in Gnome Terminal. For some reason Gnome Terminal seems to be the problem. The macros work fine for instance in vscode, gedit and xterm but Gnome Terminal “swallows” the macros maybe once in 10 presses.

---

<div class="post-metadata">

**Author:** ![merlin](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/merlin/32/3416_2.png) [@merlin](https://community.keyboard.io/u/merlin)\
**Post date:** [March 1, 2022, 8:14pm UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/15 "2022-03-01T20:14:28Z")

</div>

> [@rloic](#):
>
> Your enum lists ended with a comma which, in my mind, means a continuation but there is not.

Trailing commas are pretty common in lists with one item per line, and are especially nice for version control. If another item is added to the end of a list without a trailing comma, the diff will show three lines (two for adding a comma to one line, and one for the new line) instead of just one.

---

<div class="post-metadata">

**Author:** ![rloic](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/rloic/32/3491_2.png) [@rloic](https://community.keyboard.io/u/rloic)\
**Post date:** [March 2, 2022, 8:36am UTC](https://community.keyboard.io/t/unable-to-make-my-macro-work-with-new-firmware/5195/16 "2022-03-02T08:36:09Z")

</div>

> [@merlin](#):
>
> Trailing commas are pretty common in lists with one item per line, and are especially nice for version control. If another item is added to the end of a list without a trailing comma, the diff will show three lines (two for adding a comma to one line, and one for the new line) instead of just one.

Thanks for the information.  
I didn’t know this usage and as the atreus customization is my only C++ code use case I have very limited skills.
