Nexus 7 Bluetooth Keyboard and UK Layout

I’ve really been enjoying playing with my Nexus 7 but have discovered a slight problem. I decided to purchase a bluetooth keyboard and so grabbed myself a Perixx PERIBOARD-804. This won out over the Zagg Keys Flex and the Logitech keyboards thanks to compact size, internal USB-rechargeable battery and UK keyboard layout. Unfortunately, while testing this out on the Nexus 7 I discovered that the Nexus doesn’t come with UK keyboard layouts for external keyboards. I haven’t been this annoyed by a keyboard layout since I bought a Mac. Anyway, after a bit of digging around online I found that it’s possible (with root) to upload a custom character mapping for my device to make it work as expected. You can read more about the specs of these files over at the Android Open Source Project.

New Configuration

Two files need to be created to ensure that all the buttons work as expected:

  • A Keyboard Layout which defines which android key code results from a key press.  This lives in /system/usr/keylayout and is named Vendor_<VendorID>_Product_<ProductID>.kl
  • A Key Character Map which defines which android key code produces which character (with modifiers).  This lives in /system/usr/keychars and is named Vendor_<VendorID>_Product_<ProductID>.kcm
Each of these locations contains a Generic.kl/Generic.kcm file that can be used as a template for any changes. These files are used when no other suitable configuration is present so I suggest not editing them directly, or at least backing them up.

Identifying the Device

The VendorID and ProductID in the filenames are used to match the configuration to the specific device.  With the keyboard connected this can be found in a shell (adb or on device) by looking in /proc/bus/input/devices.

shell@android:/ $ cat /proc/bus/input/devices
...
I: Bus=0005 Vendor=04f2 Product=1063 Version=0505
N: Name="Bluetooth Keyboard                   "
P: Phys=10:BF:48:F3:8B:E2
S: Sysfs=/devices/platform/tegra_uart.2/tty/ttyHS2/hci0/hci0:12/input4
U: Uniq=90:7F:61:8E:78:A4
H: Handlers=sysrq event3 keychord
B: PROP=0
B: EV=12001f
B: KEY=4c37fff 72ff32d bf544456 0 0 1 30f90 8b17c007 ffff7bfa d951dfff febeffdf ffefffff ffffffff fffffffe
B: REL=40
B: ABS=101 30000
B: MSC=10
B: LED=1f

Here my Vendor ID is 042f and my Product ID is 1063. Once you’ve created your baseline files by copying (adb pull is your friend) the generic configuration and renaming to match your Vendor & Product IDs you can jump into them and make the changes you require.

Changing Keys

In my case most of the work was in the key character map file (moving quote marks, tilde, etc.) and you may not need to make any changes to the keyboard layout at all. One change I had to make was to support the # key on a UK keyboard (which has the ~ character above it). The generic keyboard layout was sending it as a backslash, along with the actual backslash key. To find which key to change I needed to know the shortcode being sent by the keyboard and for this you can use KeyTest.apk made by Chris Boyle. This app will run and report the short code for all key presses so you can hunt down the one you’re interested in. In my case this meant changing key 43 BACKSLASH to key 43 POUND in my layout file and then putting a corresponding entry in my key character map:

key POUND {
    label:                              '#'
    base:                               '#'
    shift:                              '~'
}

Note that this format for the character map is supported from Android 3.0+ (the Nexus 7 is on 4.1 at the time of writing). It’s worth digging into the documentation to read up on the various modifier keys you can react to. Power users may decide that they want alt characters for all of their keys to produce special characters. The AOSP docs explain a bit more about any escape characters you might need to use in special cases as well as how to represent more exotic characters as unicode code points. Wikipedia should serve to provide a reference for the latter.

Installation

Once you’re done with your new configuration you’ll want to deploy this to your device. I did all of this using adb as follows.

  1. Put my new configuration in two local directories: keylayout and keychars
  2. Create a keyboard folder in the root of your /sdcard partition.
  3. adb push your configuration to the device:
    adb push keylayout /sdcard/keyboard/keylayout
    adb push keychars /sdcard/keyboard/keychars
  4. Launch adb shell and run the following commands:
    su
    mount -o remount rw /system
    cat /sdcard/keyboard/keylayout/Vendor_04f2_Product_1063.kl > /system/usr/keylayout/Vendor_04f2_Product_1063.kl
    cat /sdcard/keyboard/keychars/Vendor_04f2_Product_1063.kcm > /system/usr/keychars/Vendor_04f2_Product_1063.kcm
    chmod 644 /system/usr/keychars/Vendor_04f2_Product_1063.kcm
    chmod 644 /system/usr/keylayout/Vendor_04f2_Product_1063.kl
    reboot
  5. Wait for your device to reboot
  6. Profit

Profit

Voila! You should now have a working keyboard layout for your device! If you’ve also got the Perixx 804 then you can take my configuration files from a repo on GitHub. Please feed back any mistakes you find and shout if you’d like any more layouts uploaded. If I have time I might try to get an app together for installing any configs from git and/or editing these config files on the device.

Troubleshooting

If you’re trying to do this yourself and having difficulty troubleshooting what’s happening, keep an eye on adb logcat when you’re connecting your bluetooth device. A message should appear telling you which layout and character map files are being used. This helped me identify what was failing on my first attempt and I eventually tracked a problem to a duplicate definition for POUND in the character map.

A shout out to this post on the Thrive forums for getting me on the right track.

9 thoughts on “Nexus 7 Bluetooth Keyboard and UK Layout

  1. Just got my Touchpad bluetooth keyboard and was a bit miffed when it was mapped to US on my Nexus devices. Your guide got things all sorted for me, thanks for the info!

  2. For some reason this hasnt worked for my N7 running JOP40D, the files are there in /system/usr/* and with the right permissions. When I connect the keyboard, it shows it loading the layouts as named in the files but still @ for shift-2 🙁

    logcat gives me hope it should work, but if I reboot then reconnect and then draft a mail in gmail, still US keylayout for shift-2
    I’ve tried re-pairing and a couple of reboots but to no avail!

    Here’s the logcat bits, any ideas?

    W/bt-btif ( 1125): bta_hh_co_send_hid_info: vendor_id = 0x04f2, product_id = 0x1063, version=0x0505,ctry_code=0x00

    I/EventHub( 449): New device: id=7, fd=230, path=’/dev/input/event3′, name=’Broadcom Bluetooth HID’, classes=0x80000163, configuration=”, keyLayout=’/system/usr/keylayout/Vendor_04f2_Product_1063.kl’, keyCharacterMap=’/system/usr/keychars/Vendor_04f2_Product_1063.kcm’, builtinKeyboard=false,usingSuspendBlockIoctl=true, usingClockIoctl=false

    I/InputReader( 449): Device added: id=7, name=’Broadcom Bluetooth HID’, sources=0x01000711

    cheers

    1. Good timing! Literally caught me before I wiped the device (just got a replacement as the screen is acting up). Just tried plugging in my keyboard and my logcat is identical to yours (ignoring device id as I imagine that’s a connection order thing). Same rom version.

      If you go into Language & Input in settings and tap the Broadcom Bluetooth HID bit does it have anything other than “Default” listed?

  3. Hi, thinking of buying this keyboard for my first tablet which is on the way (Nexus 10). Thanks for the great blog post. One more general question, with this keyboard can I switch running apps on a nexus tablet using the keyboard, i.e. is there an equivalent to Alt+Tab in windows? If so what is that alternative? Thanks!

  4. thanks very much for this.
    i have a miniature keysonic ACK-3400U keyboard and my xperia z ultra completely lacks the ability to switch to a UK key layout.
    Your keyboard map worked very well except that the shift-2 key is still @ not “, and the aprostrophe/at key produces a double-quote when used with the shift key, so its very very close to perfect.

Leave a Reply

Your email address will not be published. Required fields are marked *