Making the Magic Keyboard Happen, Linux Edition


Over the past couple of months I’ve transitioned to my new 2019 XPS 13 from my mid 2012 Macbook pro, which held up admirably over the past 8 years. The laptop is great. Getting wayland working with ubuntu was a snap and allowed me to scale the display to 1.25, making the laptop usable as a laptop.

Over the past couple of months I’ve transitioned to using the windows keyboard on the laptop, which I hated at first, why is the ctrl key so far away!? After much turmoil and many messed up Emacs sessions I retrained my muscle memory. Great, now I can use my laptop day to day no worries and nothing bad will ever happen.

So, I’m travelling for work this week, and thought I’d bring my compact magic keyboard. It’s small, compact and, importantly, reliable. I’ve used it umpteen times before while presenting and have never had an issue.

I set up at my desk, boot up emacs to show something to my boss, and boom! I’m looking at the workspace manager. The command key is where meta would be and the fn key is where ctrl would be. Oops.

After some digging I found that the command key and meta could be swapped by setting /sys/module/hid_apple/parameters/swap_opt_cmd to 11. Also, to address the issue of the ctrl and fn keys being swapped on the magic keyboard I set my capslock key to ctrl2. One small step for me, one large step towards fighting carpel tunnel!

These modifications were trivial. I’m ready for live demos tomorrow! Great!!

The next morning, things tick along well for a while, but during a demonstration I keep pressing fn in place of ctrl. This seems harmless, save for the fact that none of my code is compiling. “Why the H@CK are there these xbs and xss everywhere :eyeroll:”

There was no time during the demo to fix it, but for tomorrow I want everything running smoothly. I guess it’s back to the drawing board. How hard could it be to swap the positions of two keys?

After several hours of struggle back in my hotel room, it turns out modifying the fn key location is H@CKING impossible on my machine in userspace. Enter hid-apple-patched3. This is a modification to the kernel that allows you greater customization over the interface with the magic keyboard. One such modification is swapping the position of the fn key and the ctrl key. Hallelujah!

This was my first kernel modification. I used dkms so ideally as I update my kernel this fix will keep working. I give it a 5050 that something terrible will happen. But we just have to wait and see. For now my magic keyboard functions identically to the built in keyboard on my laptop, and that is all that matters. Time for a day of live demonstrations, hopefully with no more xs and os all over my source code!

Peace.


  1. For more details on how to make this change permenant check out https://wiki.archlinux.org/index.php/Apple%5FKeyboard [return]
  2. I used gnome tweaks. [return]
  3. For instructions on how to get hid-apple-patched working on your machine check out https://github.com/free5lot/hid-apple-patched/tree/master [return]