Sunday 6 November 2016

Wired Apple keyboard on PC with Linux and windows

The wired USB Apple keyboard A1243 (MB110B/B)




Whilst this is an apple product it can also be used on a PC. Even during the PC boot, as it is a USB keyboard (albiet with a bunch of extra keys), all the necessary and standard keys (delete, esc, F12 etc) work. Once the OS (linux as well as windows) there's a couple of minor tweaks that can be made to make it just a little less Apple-ly. Regardless of the keys labels printed, pressing keys are laid out in UK PC standard - SHIFT-2 generates " and @ between L and RETURN.



Windows

This is fairly straight fwd and the keyboard will be recognised the first time you plug it in - to get use of the multimedia keys, you will need to download the BootCamp Support files (try here since the Boot Camp landing page seems to be useless) and extract the BootCamp.msi and AppleKeyboardInstaller64.exe which you can run as normal.

Microsoft do provide a legacy keyboard layout editor that can also be used to generate a new keyboard layout that can be then loaded.

Linux

Recent 4.x kernels support this keyboard pretty much out the box; the only slight complication is that you need force 2 kernel parameters so it behaves a little more PC'y such as the cmd and alt keys switching. With default kernel parameters, my UK keyboard switches the location of the grave/notsign and the backslash/bar are switched.

To fix it's relatively easy.
# /etc/modprobe.d/apple.conf
options hid_apple  iso_layout=0  swap_opt_cmd=1

# force reload of module with new parameters
$ modprobe -r hid-apple; modprobe hid-apple

# can also dynamically update by echo 1 or 0 to this file
$ cat /sys/module/hid_apple/parameters/iso_layout


There is one additional parameter which is fnmode which controls whether pressing the F1-F12 keys generates a F1-F12 response or the media (vol up/down etc) keycode.

As an aside, to determine what parameters a kernel module can take there are 2 simple ways:
# from the 'sysfsutils' package
$ systool -vm hid_apple
Module = "hid_apple"

  Attributes:
    coresize            = "16384"
    initsize            = "0"
    initstate           = "live"
    refcnt              = "0"
    taint               = ""
    uevent              = 

  Parameters:
    fnmode              = "1"
    iso_layout          = "0"
    swap_opt_cmd        = "1"
...
or by looking at the /sys/module/module name/parameters/ directory. The downside to these 2 techniques requires the module to be loaded via modprobe.

More background and information regarding the apple keyboards under linux can be found at this useful Ubuntu community page.

Automating via udev

A cleaner way to do this is via udev so the actions are only executed when this keyboard is attached: this requires as set of rules/actions and a script to be run. The script:
#!/bin/bash
# /etc/modprobe.d/hid-apple.conf 
# options hid_apple iso_layout=0 swap_opt_cmd=1

/sbin/modprobe hid-apple
echo 0 > /sys/module/hid_apple/parameters/iso_layout 
echo 1 > /sys/module/hid_apple/parameters/swap_opt_cmd

And the rule:
# /etc/udev/rules.d/99-apple-a1243.rules
#
# udevadm test $(udevadm info -q path -n /dev/input/by-id/usb-Apple_Inc._Apple_Keyboard-event-kbd)
# udevadm control --reload-rules
# for a 2015 version of Apple a1243

ACTION=="add|change", SUBSYSTEM=="input", ATTRS{idVendor}=="05ac", ATTRS{idProduct}=="0250", RUN+="/usr/bin/udev-apple-a1243.sh"
Since the modprobe does not distinguish between the different Apple keyboards it may be useful to use the udev rules (specifying the product version) to determine which configuration values are updated - for example, the Apple Wireless keyboard (A1314) USB identifers (05ac:021e) does not need the modification to the iso_layout.

No comments:

Post a Comment