Sign in

Android keymaps

2010-08-22 15:32:41 -

I recently bought a Bluetooth keyboard to use on Android; unfortunately some keys weren't recognised at all and others weren't quite mapped as I'd like. This is fairly easy to fix as an end user, as long as you have a rooted device (adb remount must succeed). Here's a quick howto, since I couldn't find one.

It shouldn't need saying, but you do this at your own risk. You can easily break your device's response to keys, and possibly make your device unbootable too if the file is invalid.

It's pretty much as described under Keymaps and Keyboard Input in the porting documentation. What we're going to do is edit one file: /system/usr/keylayout/qwerty.kl. This file maps scancodes to Android keycodes. For example: key 30 A means scancode 30 (decimal) maps to KEYCODE_A (29), the A key. As the porting docs say, a separate Key Character Map file deals with what characters, if any (e.g. "a", "A", "#"), should be generated given the keycode and the state of the modifiers (Shift, Fn/Sym, etc). A different KCM can be loaded for Bluetooth keyboards depending on their device name, but at least by default, the same key layout file (including your changes) is used for Bluetooth and the built-in keyboard (if any).

So, first we need to fetch your current key layout, and it's a good idea to keep backups (both on the device and locally).

1 adb pull /system/usr/keylayout/qwerty.kl qwerty.kl
2 cp qwerty.kl qwerty-orig.kl
3 adb shell cp /system/usr/keylayout/qwerty.kl /sdcard/qwerty-orig.kl

Now, edit qwerty.kl as you like. It should look initially look similar to this Nexus One example. You might find it helpful at this point to see what scancodes your keyboard is sending. There really ought to be a tool for this in Dev Tools or Spare Parts, but it seems there isn't. I've made a dirt-simple app to show you KeyEvent.toString(), which was the work of 5 minutes and could certainly be improved.

My particular keyboard is a white roll-up 87-key variety, that you can still find on eBay. It has F-keys doubling as media keys: if you get this model, be aware that the Fn key is really F-lock (toggles the state rather than acting as a modifier). Here is my layout file in case it's helpful. This layout mostly follows the principle of least surprise, except that the button with "Home" printed on it is Call, to mirror "End" being Hangup. F1 with F-lock on (Home symbol) is the Android "Home" keycode (goes to the home screen).

Now copy the layout back onto the device:

1 adb remount
2 adb push qwerty.kl /system/usr/keylayout/qwerty.kl

(If the remount fails, you probably don't have a rooted device.) It seems you need to reboot for the changes to take effect, so go ahead and do that, and your keys should then behave as you've specified. :-)

 
2010-12-25 08:20:11
Julien Bourdoiseau

very useful - works fine HTC desire + bluetooth keyboard AZERTY
Thanks

 
2011-08-10 04:09:02, updated at 2011-08-10 09:37:46
Rafael Machado

Hi, nice tutorial, but, i'm using a portuguese (brazilian) keyboard, and my tablet doesn't have a layout for this keyboard, i tried use your tutorial, but when i start the Key test, i push the "ç" letter, and appear key 74, but when i change the key in qwerty.kl some keys stops work, and the "ç" doesn't work too, so can you help me?

 
2011-08-10 09:48:56

Have you perhaps put something like "key 74 ç" in qwerty.kl? That won't work (and would probably stop it reading the rest of the file), because ç is a character, but you need a keycode, something like CCEDILLA, but that isn't on the list... I'm looking at the list of Android keycodes and in fact I don't see any for accented characters, so I don't know how that would work. :-(

I would recommend you try asking on android.stackexchange.com, about keycodes for accented characters. I hope I've missed something obvious and that it is possible somehow, I mean, they're already selling Android devices in plenty of countries that would require such keys on the phone itself, aren't they?

 
2011-08-10 18:09:26
Rafael Machado

Thanks for the help, I already asked in the forum who you posted

 
2011-08-14 00:44:05
Rafael Machado

Hey, I think i have a solution, I had a motorola cliq, which has a physical keyboard, so i thought, it has to be a ROM with the layout, so i found, but wasn't configurated to a external keyboard, but opening the keychar, i searched for Ç, and i found, and I rename to qwert.kcm, but now the problem is, how can I configure the qwerty.kl to work with Ç?

 
2011-08-16 08:38:23

If you look at your kcm file (it's plain text), look for the lines with the Ç, you can see at the start of the line what keycode is mapped to that. Use that keycode in qwerty.kl. A problem you might encounter is that the kcm expects an ordinary C and a modifier, which is seemingly not what you have. If it does that, I don't know how to proceed from here, and can only suggest looking at relevant parts of the platform source.

 
2012-01-23 00:56:16

Hi, sorry i'm checking for an app or similar for change the funtions of the keys <<,>>,>,Pause, etc of the normal radiocd with a2dp, when it's in a normal bluetooth sync between an android phone..
i try to change this funtions to make diferent thinks on the phone when i press this keys on the radiocd.. it´s possible  to make this??

 
2012-01-23 14:25:01

Yes, I would guess you should be able to catch the buttons in the same way that media players like last.fm do it. Looks like you want to catch Intent.ACTION_MEDIA_BUTTON. Here is last.fm's implementation, search for MEDIA_BUTTON in both cases:

https://github.com/c99koder/lastfm-android/blob/master/app/src/fm/last/android/LastFMMediaButtonHandler.java

https://github.com/c99koder/lastfm-android/blob/master/app/AndroidManifest.xml