# Macros based on key

**URL:** <https://community.keyboard.io/t/macros-based-on-key/2176>\
**Category:** Programming\
**Created:** [November 7, 2018, 8:55pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176 "2018-11-07T20:55:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amos](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/amos/32/639_2.png) [@amos](https://community.keyboard.io/u/amos)\
**Post date:** [November 7, 2018, 8:55pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/1 "2018-11-07T20:55:12Z")

</div>

I want a macro that when I hit the keys, it does ctrl-b and then the key pressed. I can hard-code each macro, but I was hoping for a simpler solution. I tried to use `T(Macros.col)` to tap the number, but I get an error. I tried to search the forums, but I couldn’t find anything. Does anyone know how I could dynamically do this?

---

<div class="post-metadata">

**Author:** ![algernon](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/algernon/32/4404_2.png) [@algernon](https://community.keyboard.io/u/algernon)\
**Post date:** [November 7, 2018, 9:35pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/2 "2018-11-07T21:35:02Z")

</div>

I think you want the PrefixLayers plugin:

> [@PrefixLayers - A Plugin for Screen/Tmux users](https://community.keyboard.io/t/prefixlayers-a-plugin-for-screen-tmux-users/667):
>
> Plugin name: PrefixLayers Source URL: [https://github.com/jamesnvc/Kaleidoscope-PrefixLayer](https://github.com/jamesnvc/Kaleidoscope-PrefixLayer) Description: Inspired by this [neat idea](https://github.com/ilianaw/Model01-Firmware/commit/7dad53bf1855e68a4c93a1686cad5e75b539cb0b) and [@algernon suggesting it be a plugin](https://trunk.mad-scientist.club/@algernon/98920372754259733), I made a plugin that lets you set a prefix sequence of keys to be tapped before every key on a particular layer. The initial purpose was to have a TMUX layer that is mostly transparent and has every key in that layer prefixed with a Ctrl-b. It’s been generalized to have a list of layers and a sequence of keys to be sent fo…

Though, that might not be updated for the v2 plugin API yet… I guess I’ll have to submit a PR to move it forward. The plugin does precisely what you wish to do 🙂

---

<div class="post-metadata">

**Author:** ![james.nvc](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/james.nvc/32/54_2.png) [@james.nvc](https://community.keyboard.io/u/james.nvc)\
**Post date:** [November 7, 2018, 9:36pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/3 "2018-11-07T21:36:22Z")

</div>

Ah yes, I was meaning to update this for the v2 API…I’ll have a look at that later today.

---

<div class="post-metadata">

**Author:** ![amos](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/amos/32/639_2.png) [@amos](https://community.keyboard.io/u/amos)\
**Post date:** [November 7, 2018, 9:49pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/4 "2018-11-07T21:49:11Z")

</div>

I don’t want a whole layer prefixed. I just want the num keys prefixed. I want it to be the default for the numbers because I actually use a layer with hometown for normal numbers.

Amos King  
Binary Noggin

---

<div class="post-metadata">

**Author:** ![james.nvc](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/james.nvc/32/54_2.png) [@james.nvc](https://community.keyboard.io/u/james.nvc)\
**Post date:** [November 7, 2018, 10:05pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/5 "2018-11-07T22:05:04Z")

</div>

Ah, I see. Well, in any case my plugin is updated to the version 2 API; I think it would be fairly straightforward to modify the code to the keys to trigger the prefix configurable & use that instead of the layer; basically, if you make the keys of the dict my plugin uses be key codes instead of layers, then instead of the check for `Layer.isOn` in the key handler, you can check if the pressed key equals the key in question.

---

<div class="post-metadata">

**Author:** ![amos](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/amos/32/639_2.png) [@amos](https://community.keyboard.io/u/amos)\
**Post date:** [November 7, 2018, 10:33pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/6 "2018-11-07T22:33:08Z")

</div>

Looks like I will have to do some reading of the code. Thanks for the pointer!

---

<div class="post-metadata">

**Author:** ![algernon](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/algernon/32/4404_2.png) [@algernon](https://community.keyboard.io/u/algernon)\
**Post date:** [November 7, 2018, 11:01pm UTC](https://community.keyboard.io/t/macros-based-on-key/2176/7 "2018-11-07T23:01:33Z")

</div>

Hmm… Another option: since macros have an index, you can use that to figure out which one’s pressed.

Something like this:

```auto
void CtrlBNumber(uint8_t macroIndex, uint8_t keyState) {
  uint8_t number = macroIndex - M_ONE;
  if (!keyToggledOn(keyState))
    return;
  handleKeyswitchEvent(LCTRL(Key_B), UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED);
  kaleidoscope::hid::sendKeyboardReport();
  handleKeyswitchEvent(LCTRL(Key_B), UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED);
  kaleidoscope::hid::sendKeyboardReport();
  handleKeyswitchEvent((Key){.keyCode = Key_1.keyCode + number}, 
                       Macros.row, Macros.col, keyState);
}

```

Just make sure your macros are in the proper order (with `0` being the last number), and this _should_ work. It’s completely untested however.

---

<div class="post-metadata">

**Author:** ![amos](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/amos/32/639_2.png) [@amos](https://community.keyboard.io/u/amos)\
**Post date:** [November 8, 2018, 12:24am UTC](https://community.keyboard.io/t/macros-based-on-key/2176/8 "2018-11-08T00:24:52Z")

</div>

Thanks, that might work. I might make a prefix plug-in that add a prefix to a key that is pushed without a layer being required. Or I might just use that macro hack but it feels dirty.

Amos King  
Binary Noggin

---

<div class="post-metadata">

**Author:** ![algernon](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/algernon/32/4404_2.png) [@algernon](https://community.keyboard.io/u/algernon)\
**Post date:** [November 8, 2018, 6:43am UTC](https://community.keyboard.io/t/macros-based-on-key/2176/9 "2018-11-08T06:43:25Z")

</div>

The plugin would need to do something very similar too. The advantage of the plugin would be that it is easier to share, and a tiny bit more efficient. But under the hood, it would be doing the same.

It may feel dirty, but rest assured it isn’t dirtier than any other trick we make the keyboard do.
