Huy's Notes
Implementing hardware level key lock

Implementing hardware level key lock

#hardware #keyboard

One of the features of TMK/QMK firmware is the KC_LOCK key, when you hold a key, and press KC_LOCK, that key become persistent even if you release it. Just like the caps lock key.

This is pretty helpful for Emacs user, so they can move freely without holding Ctrl key.

To implement it, we can just add a new flag check, for example ctrlLockOn, to where we handle Ctrl keypress:

-    else if (keys[i].code == CTRL_KEY) {
+    else if (keys[i].code == CTRL_KEY || ctrlLockOn) {
      modifiers |= MODIFIERKEY_CTRL;
    }

And triggering ctrlLockOn where we desired. For example, I want to toggle ctrlLockOn when user pressed Fn + Ctrl + Shift + Alt, so the code would be:

+    if (keys[i].code == FN_KEY) {
+      fn = 1;
+    }

...

+    if (modifiers == CTRL_KEY | ALT_KEY | SHIFT_KEY && fn == 1) {
+      ctrlLockOn = ctrlLockOn ? 0 : 1;
+    }

By doing this, we can also press the above sequence again to turn off locking.

Referred in


If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.