September 2, 2019
hardware

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.

@this-is-a-tag
Waiting
* Work in progress
Done
Flagged
Hey! Have you tried Pomoday?
It's a keyboard-oriented, very easy to use and very powerful web-based task management software.
Powered by a lot of ☕