I recently celebrated my 23rd birthday and was gifted by my colleagues with a wonderful device. Apple Slim Aluminum Keyboard.
Yes Yes Yes. It is with a capital letter every word.
In my humble opinion, the best keyboard for the developer is not found. Especially for a Linux lover like me.
The keyboard is comfortable, you will not say anything, I will not write about it anymore, and it has already been written everywhere. However, not without problems.
My favorite work computer with Gentoo completely refused to work with multimedia keys and other pleasures of the modern world. A little googling, I discovered that I am not alone, and a huge number of people also suffer from this. However, I did not find any clear and detailed instructions in Russian (and in English). Especially for sufferers like me, this short manual is intended.
Here in the comments ask for a picture of the keyboard:

And
here you can read about it.
I will make a reservation that we will perform all subsequent operations in the superuser mode. And also, all the proposed keycodes work only for the Slim keyboard. For the laptop keyboard will have to repeat everything yourself.
1. Compile the bucket.
The first thing that I had to do as a user of such a friendly distribution as Gentoo was to recompile the kernel, of course.
Without this, the X server completely refuses to see the missing keys.
I note that Ubuntu users will not have to do this as the developers have already done everything for them.
I assume that the kernel sources you already have are / usr / src / linux
We go there and make make menuconfig
In the dialog that opens, we find:
Device Drivers → HID Devices
And tick the box “Enable support for Apple laptop / aluminum USB special keys”.
Next, as usual, make && make modules_install && make install
If it is not compiled, we call our Linux neighbor, google forums, etc. in search of a solution to find out what is wrong and how to overcome this problem.
2. A bit of shamanism.
I note that after the kernel has been successfully installed, everything will practically work. But, as is customary in the Linux world, not without a catch.
Now, in order to press, for example, the F1 button, you must first hold down the non-working Fn key. Believe me, that is very annoying and makes you think about suicide.
To correct this disgrace, we do such a thing:
echo 2 > /sys/module/hid/parameters/pb_fnmode
In order for this change to be applied each time the system is booted, this command must be added to the end of the startup script.
In different distributions it is in different places.
Let me give you an example for Gentoo:
/
etc/conf.d/local.start
and for Ubuntu:
/etc/rc.local
3. And that's not all.
What do you think? Left just a little bit.
Unfortunately, the "E" button does not work, it is also "~". And there are reverse apostrophes on it. The necessary button in general.
Also, the buttons in the top row do not work: Print Screen, Scrool Lock and Pause.
Not so often I use them, but still not on their own from the fact that they do not work.
Well, perhaps the most important drawback is that there is no Insert button, but there are F16-F19 which, as it were, have nowhere to go.
We will deal with these problems.
')
To change the keyboard layout, we will use two standard X utilities: xev and xmodmap. Changes will not be made under the superuser, but under your system user, in your home directory.
Run xev and try to press the keys.
After clicking we read what falls out on the screen. I clicked on the letter "e" and saw this:
KeyRelease event, serial 34, synthetic NO, window 0x3c00001,
root 0x1a6, subw 0x0, time 31989750, (533,667), root:(538,692),
state 0x10, keycode 94 (keysym 0x3c, less), same_screen YES,
XLookupString gives 1 bytes: (3c) "<"
XFilterEvent returns: False
What does this tell us?
The key with keycode 94 has been pressed. A typed character “<” is assigned to it. What does not suit us.
Run the xmodmap utility with the -pke keys
The current keyboard layout will fall out on the screen.
We are looking for the desired keycode:
keycode 94 = bar bar brokenbar less slash
Create a new file in the root directory with the name .Xmodmap.
In it, we will form our new layout.
Copy the line found above into this file and edit.
In order to know what to replace “less greater ...” you will have to read the entire output of xmodmap -pke and find what suits us.
And here it is:
keycode 49 = grave asciitilde Cyrillic_io Cyrillic_IO
Replace so what would happen:
keycode 94 = grave asciitilde Cyrillic_io Cyrillic_IO
For the test, run: xmodmap ~ / .Xmodmap
And try to press. Hooray? Everything is working.
In the same way we find the key codes for F13 - F15 and assign the necessary actions to them.
I assigned this:
keycode 191 = Print Sys_Req Print Sys_Req Print Sys_Req
keycode 192 = Scroll_Lock NoSymbol Scroll_Lock NoSymbol Scroll_Lock
keycode 193 = Pause Break Pause Break Pause Break
Well, Insert hang on F16:
keycode 194 = Insert NoSymbol Insert NoSymbol Insert
As a result, I received such an .Xmodmap file:
keycode 94 = grave asciitilde Cyrillic_io Cyrillic_IO
keycode 191 = Print Sys_Req Print Sys_Req Print Sys_Req
keycode 192 = Scroll_Lock NoSymbol Scroll_Lock NoSymbol Scroll_Lock
keycode 193 = Pause Break Pause Break Pause Break
keycode 194 = Insert NoSymbol Insert NoSymbol Insert
I note that after this file appears in your home directory, nothing else needs to be done. It will automatically apply after restarting the X server.
4. For maniacs
Some, accustomed to the arrangement of keys typical for ordinary keyboards: Ctrl | Win | Alt can't get used to the fact that on the Apple keyboard this layout is changed to: Ctrl | Alt | Cmd
Cmd is the same as Win. For linux, it looks like “Meta” or “Super.”
To do this, using the same xmodmap we came up with the following remapping:
clear Mod4
clear Mod1
keycode 115=Alt_L
keycode 64=Super_L
add Mod4 = Super_L
add Mod1 = Alt_L
5. For dissenters.
Of course, the proposed scheme is not for everyone. But guided by this instruction, everyone can customize their keyboard as he wants.
And of course, the part of the instruction that is responsible for keying the keys can also help those who are not the happy owners of the Apple keyboard.
For example, you can fine tune the multimedia keys on the keyboards on which they are present. :) Or subtract the layout that doesn't suit you.
ZY: During the experiments, it may happen that you can not press any button and want to return everything as it was.
In order not to restart the X-server, you can use the following command: setxkbmap -model evdev -layout us, ru
This will return the layout to working condition. Instead of “evdev” you should try to substitute your keyboard model. You can spy on this name in advance with the command: cat /etc/X11/xorg.conf | grep XkbModel or in the keyboard settings of your desktop manager.
UPD: Added a picture of the keyboard.