A better configuration for the Apple keyboard
As a piece of hardware, the Apple keyboard is great – crisp and satisying to use. In terms of key layout – not so much. Many users coming from non-Mac systems find the following frustrating:
- The media keys defaults to on all the time. Most people, particularly developers, use the function keys (F2, F5 etc) more often.
- The Ctrl and Fn keys are swapped, meaning Ctrl (surely the most important special key) is no longer at the corner of the keyboard. I’m flumoxed by this design choice, also present on the Thinkpad – it’s pretty infuriating.
- Alt is no longer by the space bar. This is confusing for alt-tabbing between windows.
- The “super” (start or command) key is moved compared to PC keyboards
Fortunately, there is a simple way to configure the mappings directly in the kernel under Linux.
Patching hid-apple
Direct support for the HID quirks in the Apple keyboard is provided by applying a kernel patch to hid-apple
. This small kernel module will do this in a straightforward way: free5lot/hid-apple-patched.
This adds a number of configuration options in /etc/modprobe.d/hid_apple.conf
that can be used to adjust the layout of the keyboard as we please. On my configuration, I use:
options hid_apple fnmode=2
options hid_apple swap_fn_leftctrl=1
options hid_apple swap_opt_cmd=1
options hid_apple rightalt_as_rightctrl=1
These options, respectively:
- Switch to use the function keys F1-F12 by default, with the function key activating the media buttons (dim screen etc)
- Swap the left
Ctrl
andFn
keys, so the control key is back in the corner of the keyboard - Swap the “option” (
Alt
) and “command” (super
) keys, so Alt is back next to the spacebar - Set the right
Alt
key as a control key
On Linux Mint, I had success installing this via DKMS
(though I did need to create a /etc/depmod.d/hid-apple.conf
file as noted in that repo).
On Gentoo, as I was building a highly customized kernel, things weren’t quite as straightforward.
Updating the Kernel on Gentoo
My solution is very crude and I’m sure there’s a cleaner way, but it works, so here it is for posterity.
This github issue indicates that it is indeed possible to directly modify the kernel source code to include the hid-apple additions. I simply copied the patched version of drivers/hid/hid-apple.c
into my kernel source.
I did need to make a couple of modifications for it to compile. I was missing the apple “magic keys” headers – so I simply removed those sections. My modified patched hid-apple.c
file is available as a gist here. Simple copy it over drivers/hid/hid-apple.c
and you should be able to compile in kernel 5.x.
Touchpad and Keyboard Kernel support
For complete support of the media keys, keyboard backlight, and Apple touchpad, there are a few kernel modules that you’ll need to make sure you include.
-> Device Drivers
-> HID support
<*> Generic HID driver
-> Special HID drivers
<M> Apple {i,Power,Mac}Books (CONFIG_HID_APPLE)
-> Input device support
-> Mice
<*> Apple USB BCM5974 Multitouch trackpad support (CONFIG_MOUSE_BCM5974)
-> Hardware Monitoring Support
<*> Apple SMC (Motion sensor, light sensor, keyboard backlight) (CONFIG_SENSORS_APPLESMC)
An important caveat is that for the HID custom settings above to work, they must be compiled in module < M > rather than integrated mode.
Touchpad Touch-to-click
The backlight and special keys should now work. The final refinement is to activate touch-to-click on the touchpad, allow right-clicking, and (a personal choice this) turn off scrolling, zooming and gestures.
This is configured at the Xorg level as part of the libinput
driver. Create a configuration file at /etc/X11/xorg.conf.d/40-libinput
:
Section "InputClass"
Identifier "libinput touchpad catchall"
MatchIsTouchpad "on"
MatchDevicePath "/dev/input/event*"
Option "Tapping" "True"
Option "TappingDrag" "True"
Option "ScrollMethod" "none"
Option "ClickMethod" "buttonareas"
Driver "libinput"
EndSection