Monday, February 25, 2013

Bind Media Keys on the RC11 Air Mouse within Android

I recently bought a Measy RC 11 air mouse to complement the Rockchip MK808  and one thing I noticed right away was that the media buttons did not function. I guessed it had something to do with key mapping and learned that Android maintains mapping information within key layout files in the  path. My goal would be to edit the appropriate files to map the media buttons to the key codes (scancodes).

I essentially followed the instructions provided here while making slight adjustments to suit my own needs. The only prerequisite being that the device needs to be rooted.

The key layout files themselves can be stored in various locations and Android refers to them in the following hierarchy:
/system/usr/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
/system/usr/keylayout/Vendor_XXXX_Product_XXXX.kl
/system/usr/keylayout/DEVICE_NAME.kl
/data/system/devices/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
/data/system/devices/keylayout/Vendor_XXXX_Product_XXXX.kl
/data/system/devices/keylayout/DEVICE_NAME.kl
/system/usr/keylayout/Generic.kl/data/system/devices/keylayout/Generic.kl
As Generic.kl file acts as the default file used in the case a mapping was not found, the documentation strongly recommends that we do not modify it.

The first step in the process was to make myself a copy of the Generic.kl using ES File Explorer File Manager. Temporarily rename the copy to Vendor_[VendorID]_Product_[ProductID].kl. The [VendorID] and [ProductID] will be replaced with the proper IDs once identified.  This will be our new mapping file.

Next, we figure out the the scan codes to the media buttons using the the keytest app written by Chris Boyle. Fortunately, the scancodes generated from the RC11 didn’t differ from the generic mapping. I updated it with my preferred mapping:
key 62    HOME
key 63    ENVELOPE
key 64    MUSIC
key 65    MEDIA_REWIND
key 66    MEDIA_PLAY_PAUSE
key 67    MEDIA_FAST_FORWARD
key 68    VOLUME_MUTE
key 87    VOLUME_DOWN
key 88    VOLUME_UP

Now we identify the keyboard vendor id and product id codes. To do this I installed a copy of Android Terminal Server Emulator and used the cat /proc/bus/input/devices command to list the details about the devices attached attached. This contains the vendor as well as the product Id that we require.

The output should look similar to the one below:
I: Bus=0003 Vendor=0c45 Product=7403 Version=0100
N: Name="SONiX USB Device" 

P: Phys=usb-usb20_host-1.1.1/input0
S: Sysfs=/devices/platform/usb20_host/usb2/2-1/2-1.1/2-1.1.1/2-1.1.1:1.0/input/input9
U: Uniq=
H: Handlers=sysrq kbd event1 keychord 

I: Bus=0003 Vendor=0c45 Product=7403 Version=0100
N: Name="SONiX USB Device"
P: Phys=usb-usb20_host-1.1.1/input1
S: Sysfs=/devices/platform/usb20_host/usb2/2-1/2-1.1/2-1.1.1/2-1.1.1:1.1/input/input10
U: Uniq=
H: Handlers=kbd event2 keychord
B: PROP=0
Identify your device from the list and rename your previously created file to include the vendor and product Ids. Mine is now called Vendor_0c45_Product_7403.kl.

Now all that remains is to deploy the changes to the /system/usr/keylayout/ path. You would require root privileges to write to this folder. Once file has been copied over make sure that Group and Other entities have read permission on the file, I spent quite a bit of time trying to figuring this one out.

Rebooting the device or re-plugging in the keyboard receiver will cause the new mapping to take effect.

While my experience is specific to the RC11, this process can be adopted to meet your own requirements. I’ve attached my key layout file for your convenience.


As usual, bad things can happen when modifying system files, so please make sure to take all the appropriate precautions as well as backups. I am not responsible for anything you might break.

This has been a very fun learning experience for me. Look forward to more Android related posts from in the future.

References