I'm wondering if it's possible to write a kernel extension
Probably, but this isn’t the way forward here. Writing code for the macOS kernel is very painful and, more importantly, Apple is in the process of phasing it out entirely. See Deprecated Kernel Extensions and System Extension Alternatives.
macOS does its keyboard event processing in user space and that’s where you should look for this. Historically this was done in two layers:
-
A key code remap
-
A mapping from key code to characters
It’s not clear which layer you’re looking for. Layer 1 makes sense when keys just appear in the ‘wrong place’, whereas the layer 2 makes sense where the keys are in the right place but generate the wrong character.
It’s been a long time since I’ve looked at this in depth. I’m not sure if there’s any public way to modify layer 1 these days, but layer 2 is definitely still supported via .keylayout
files. See Technote 2056 Installable Keyboard Layouts for the docs.
Worst comes to worst, with enough work you can reasonably emulate most layer 1 mappings via layer 2.
Creating a layout from scratch is quite a lot of work. AFAICT most folks use the Ukelele app for this https://software.sil.org/ukelele/. I’ve never used it myself though.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"