Keyboard


android header






First, you have to have a rooted device to be able to modify the key layout file.  You should additionally have installed a terminal app like term.apk and a text editor like textedit.  And you will need to have some familiarity with Linux commands.

Mapping from the keyboard to what the tablet sees is controlled by the file /system/usr/keylayout/qwerty.kl.  Each line  in this file represents a mapping from the input scan code (in the second column, after the word key) to a key code in the next column (a character or word, like K or BACK).  There is also a key character map file, /system/usr/keychars/qwerty.kcm.bin, whose purpose is to communicate what should happen when a key is pressed while a modifier key (SHIFT, CTRL, ALT) is down, but we don't have to deal with that.  All we need to do is to edit
/system/usr/keylayout/qwerty.kl.

I've written an Android app, Keycode View, that will spit out the scan code when a key is pressed, and also a number for the key code.  If there is no associated key code, the number is 0.  This can be used to find out what each key does.

As for the keycode, in Android, each character keycode (K or BACK) is defined as a fixed constant (KEYCODE_K or KEYCODE_BACK) which has a fixed value (39 and 4, in these examples).  But this information isn't needed to reassign keys in the key layout file.

To add confusion to this, the scancode we're talking about here is not the original scancode that gets sent from the keyboard (presumably USB or Bluetooth).  The original keyboard scancode is converted by the Linux evdev driver to our scancode.

The whole topic of keyboard output is complex and confusing, as you can see.  If a PS/2 keyboard is involved - luckily, no Android device ever has to deal with one - an entire new level of complexity is reached, as there are three possible sets of scancodes, any one of which can be used by the keyboard at the request of the operating system, and those need to be translated by hardware into the primitive scancode that is dealt with by the evdev driver.

My keyboard has a blank key right where I use my left thumb on the space bar - I am left-handed - and in order to avoid retraining myself to space with my right thumb, I assigned that key to the space function.  The tilde/grave (`/~) key on the keyboard in its original configuration produces no keycode (or rather 0); I wanted to fix that.  And I wanted to add the left mouse button function to the Control key, which isn't used by Android, so that I could use a key instead of a screen tap to represent a mouse button press.

I modified the qwerty.kl file as follows:


KEY
scan code
orig key code
new key code
blank
86
0
SPACE
`
41
0
GRAVE
CTRL
29
0
DPAD_CTR


Note that textedit cannot directly edit files in the /system directory.  In the terminal, you can invoke su, then copy the layout file to a directory in you own area where you can edit it with textedit.  When it's done, just move (mv) or copy (cp) it back to its original directory, after having removed (rm) or renamed (mv) the original file.


These links give more information:

http://developer.android.com/reference/android/view/KeyEvent.html

http://www.kandroid.org/online-pdk/guide/keymaps_keyboard_input.html