注:下文中的qwerty.kl不再有效,应使用Generic.kl或者Vendor_<vendorID>_Product_<productID>.kl,推荐使用Vendor_<vendorID>_Product_<productID>.kl,这样不会因为改错造成Generic.kl的混乱从而影响到其他外接键盘的使用。
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 toKEYCODE_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).
adb pull /system/usr/keylayout/qwerty.kl qwerty.kl cp qwerty.kl qwerty-orig.kl 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:
adb remount 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. :-)
附:bluetooth-keyboard-layout.txt内容如下
key 1 BACK
key 41 GRAVE
# lower left
key 86 BACKSLASH
# print screen
key 99 CAMERA WAKE
# home upper right
key 102 CALL WAKE_DROPPED
# menu lower left
key 125 MENU
# menu upper right
key 126 MENU
# menu icon upper right corner
key 127 DPAD_CENTER
# fn F1
key 172 HOME WAKE
# fn F4
key 156 STAR
# fn F5
key 164 MEDIA_PLAY_PAUSE WAKE
# fn F6
key 163 MEDIA_NEXT WAKE
# fn F7
key 165 MEDIA_PREVIOUS WAKE
# fn F8
key 166 MEDIA_STOP
# fn F11
key 113 MUTE
# fn F12 media
key 171 FOCUS
# pause break
key 119 NOTIFICATION
# ins lower right
key 110 DPAD_CENTER
# del lower right
key 111 DEL
key 399 GRAVE
key 2 1
key 3 2
key 4 3
key 5 4
key 6 5
key 7 6
key 8 7
key 9 8
key 10 9
key 11 0
key 158 BACK WAKE_DROPPED
key 230 SOFT_RIGHT WAKE
key 60 SOFT_RIGHT WAKE
key 107 ENDCALL WAKE_DROPPED
key 62 ENDCALL WAKE_DROPPED
key 229 MENU WAKE_DROPPED
key 139 MENU WAKE_DROPPED
key 59 MENU WAKE_DROPPED
#key 127 SEARCH WAKE_DROPPED
key 217 SEARCH WAKE_DROPPED
key 228 POUND
key 227 STAR
key 231 CALL WAKE_DROPPED
key 61 CALL WAKE_DROPPED
key 232 DPAD_CENTER WAKE_DROPPED
key 108 DPAD_DOWN WAKE_DROPPED
key 103 DPAD_UP WAKE_DROPPED
#key 102 HOME WAKE
key 105 DPAD_LEFT WAKE_DROPPED
key 106 DPAD_RIGHT WAKE_DROPPED
key 115 VOLUME_UP
key 114 VOLUME_DOWN
key 116 POWER WAKE
key 212 CAMERA
key 16 Q
key 17 W
key 18 E
key 19 R
key 20 T
key 21 Y
key 22 U
key 23 I
key 24 O
key 25 P
key 26 LEFT_BRACKET
key 27 RIGHT_BRACKET
key 43 BACKSLASH
key 30 A
key 31 S
key 32 D
key 33 F
key 34 G
key 35 H
key 36 J
key 37 K
key 38 L
key 39 SEMICOLON
key 40 APOSTROPHE
key 14 DEL
key 44 Z
key 45 X
key 46 C
key 47 V
key 48 B
key 49 N
key 50 M
key 51 COMMA
key 52 PERIOD
key 53 SLASH
key 28 ENTER
key 56 ALT_LEFT
key 100 ALT_RIGHT
key 42 SHIFT_LEFT
key 54 SHIFT_RIGHT
key 15 TAB
key 57 SPACE
key 150 EXPLORER
key 155 ENVELOPE
key 12 MINUS
key 13 EQUALS
key 215 AT
From: http://chris.boyle.name/blog/2010/08/android-keymaps/