Customize external keyboard layout for Android

Hello Fellows 🙂

Using an external keyboard with an Android device sometimes can be tricky because not all keyboards feature all of the basic Android buttons. For example I use a Bluetooth keyboard that lacks the context menu key which acts like a menu button for Android. Luckily if you have root access to your device you can customize the key mappings of your keyboard.

The first thing you need to do is to check the vendor and the product identification codes of your keyboard. You can find them if you do:

cat /proc/bus/input/devices

and explore the output in the terminal of your Android device (the keyboard has to be connected).

Check the names of the devices that are listed and you should easily identify your keyboard.

I: Bus=0005 Vendor=0a5c Product=8502 Version=011b

N: Name="Broadcom Bluetooth HID"

My Bluetooth keyboard is from vendor with id 0a5c and the product id is 8502. Now that you know the vendor and the product ids of your keyboard you can create a copy of the file /system/usr/keylayout/Generic.kl in the same location, give it a proper name and start customizing it. The name of the file should follow the pattern:

Vendor_<vendor_id>_Product_<product_id>.kl

For example Vendor_0a5c_Product_8502.kl is the name of the file for my Bluetooth keyboard. The next time you connect your keyboard it should load it’s key mappings from this file instead of the generic one.

What I did to get the functionality of the menu button was to change these lines in my custom .kl file

key 126 META_RIGHT
key 127 MENU

 to

key 126 MENU
key 127 META_RIGHT

This way I turned the right meta key (windows logo) into a menu button. The right meta key has a scan code of 126 and the context menu key has a scan code of 127 and swapping the actions that are mapped to them in the file did the job for me.

One thing that you have to be careful about is not to assign different actions to the same scan code which will cause you trouble. Each scan code must be present only once in the file.

If you want to find out the scan code of a key on your keyboard you can use the application “KeyEvent Display” which is free in the Play Store.

I hope this will be of some help.