Remap different keyboard using IOHID

Hi there!

I'm currently new to Mac OS dev (coming from a history of Linux dev).

I have an external BT keyboard and a couple keys are mapped differently than my Mac keyboard (§ to ` for example). I'm wondering if it's possible to write a kernel extension that detects the key sent from the BT keyboard and remap the key code.

I don't really know whether this is possible and if it is, where to look in the ecosystem. It's a bit different from Linux.

Thank you!

Replies

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:

  1. A key code remap

  2. 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"