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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: