UP | HOME

Implementing firmware level key lock

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.

Date: 2019-02-08 Fri 00:00

Author: Huy Tran

Created: 2019-04-22 Mon 14:40