Idempotent layer switching

Hello,

What LockLayer currently does is more like toggling, i.e. every time it’s ran the meaning of the keypresses changes. Would it be possible to make functions for idempotent switching of layers?

Example: assume we have EnableLayer and DisableLayer and I map left palm key to EnableLayer(FUNCTION) and right palm key to DisableLayer(FUNCTION). Then:

  • Pressing left palm any number of times should result in FUNCTION layer being “enabled”
  • To “disable” it, I would have to press right palm key

Reason: I’d like to implement vim-like modal cursor movement keys. Toggling doesn’t work for me because I don’t always remember which layer is currently active - I need a stateless behavior in which pressing a key will always result in known result.

You can achieve this with a macro:

enum {
 // various other macros...
 MACRO_LAYER_1_ON,
 MACRO_LAYER_2_ON,
 // ...and so on
};

void enableLayerMacro(uint8_t keyState, uint8_t macroIndex) {
  if (keyToggledOn(keyState)) {
    Layer.on(macroIndex - MACRO_LAYER_1_ON + 1);
  }
}

Similar thing for disabling, and in macroActions, you can then do:

case MACRO_LAYER_1_ON ... MACRO_LAYER_X_ON:
  enableLayerMacro(keyState, macroIndex);
  return MACRO_NONE;
case MACRO_LAYER_1_OFF ... MACRO_LAYER_X_OFF:
  disableLayerMacro(keyState, macroIndex);
  return MACRO_NONE;
3 Likes