Apple Magic Keyboard on Linux

This is a guide for Apple Magic Keyboard, US layout, connected (via USB) to Ubuntu 20.04 running on a ThinkPad.

To see how the keys are mapped by default, run xmodmap in a terminal. I made this diagram to make it clearer:

+-------------------+  +-------------+
|                   |  |             |
| Shift             |  | ~           |
|                   |  |             |
|                   |  |             |
|                   |  | Key: 94     |
|                   |  | tilde       |
|                   |  |             |
+-------------------+  +-------------+
+-------------+  +-------------+  +-------------+  +-------------+
|             |  |             |  |             |  |             |
| fn          |  | control     |  | option      |  | command     |
|             |  |             |  |             |  |             |
|             |  |             |  |             |  |             |
|             |  | Key: 37     |  | Key: 64     |  | Key: 133    |
|             |  | Name: Ctrl_L|  | Alt_L       |  | Super_L     |
|             |  | control     |  | mod1        |  | mod4        |
+-------------+  +-------------+  +-------------+  +-------------+

Remap ~, control, option and command keys

I want to swap tilde (~) with § and to remap:

So, let's create a file .xmodmap-apple and paste:

keycode 94 = grave asciitilde grave asciitilde
clear control
clear mod1
clear mod4
keycode 37 = Super_L
keycode 64 = Alt_L Meta_L
keycode 133 = Control_L
add control = Control_L Control_R
add mod1 = Alt_L Meta_L
add mod4 = Super_L Super_R

Save your current xmodmap mapping:

xmodmap -pke >.xmodmap.orig

Then, in a terminal run:

xmodmap .xmodmap-apple

You have to run this command every time you log-in.

Misc

How to switch Left Alt with Left Ctrl:

clear control
clear mod1
keycode 37 = Alt_L Meta_L
keycode 64 = Control_L
add control = Control_L Control_R
add mod1 = Alt_L Meta_L

Next ... How to connect the keyboard via Bluetooth....

Source 1

Source 2