Android添加新键值实现
1. 为了方便调试,我打开了debug选项,将
KeyInputQueue.java,
WindowManagerService.java
PhoneWindowManager.java
的debug选项修改为true,最好把修改的地方注上名字,等调试完成后将其复原。
2. 在.kl文件中定义键值:idh.code\3rdparty\products\sp8805ga\sprd-keypad8805ga.kl
key 185 IM
注意:新加的键值不要与已有的重复。
3. 在键盘驱动中定义相应的键盘扫描码:kernel\drivers\input\keyboard\sc8800g-keys.c
static const unsigned int sprd_keymap[] = {
...
KEYVAL(5, 0, KEY_F15),//IM
...
}
4. 在数组keycodes 中添加 新定义的信息:frameworks/base/include/ui/KeycodeLabels.h
static const KeycodeLabel KEYCODES[] = {
...
{ "IM", 92 },
// NOTE: If you add a new keycode here you must also add it to:
// (enum KeyCode, in this file)
// frameworks/base/core/java/android/view/KeyEvent.java
// tools/puppet_master/PuppetMaster.nav_keys.py
// frameworks/base/core/res/res/values/attrs.xml
{ NULL, 0 }
}
注意:要在{NULL, 0}之前添加
static const KeycodeLabel KEYCODES[] = {
...
kKeyCodeIM = 92
}
5. frameworks/base/core/res/res/values/attrs.xml
...
6. frameworks/base/core/java/android/view/KeyEvent.java
public class KeyEvent implements Parcelable {
...
public static final int KEYCODE_IM
= 92;
// NOTE: If you add a new keycode here you must also add it to:
// isSystem()
// frameworks/base/include/ui/KeycodeLabels.h
// tools/puppet_master/PuppetMaster/nav_keys.py
// frameworks/base/core/res/res/values/attrs.xml
// commands/monkey/Monkey.java
// emulator?
//
// Also Android currently does not reserve code ranges for vendor-
// specific key codes. If you have new key codes to have, you
// MUST contribute a patch to the open source project to define
// those new codes. This is intended to maintain a consistent
// set of key code definitions across all Android devices.
private static final int LAST_KEYCODE = KEYCODE_IM;
}
public final boolean isSystem() {
//如果是系统按键
case KEYCODE_IM:
return true;
}
7. 新键值进行处理:frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down,
int repeatCount, int flags) {
...
// First we always handle the home key here, so applications
// can never break it, although if keyguard is on, we do let
// it handle it, because that gives us the correct 5 second
// timeout.
if (code == KeyEvent.KEYCODE_HOME) {
...
} else if (code == KeyEvent.KEYCODE_MENU) {
...
} else if (code == KeyEvent.KEYCODE_IM) {
TODO: do something
}
}
注意:
1. 在.kl文件中,是按行读的,所以如果不能找到对应键值,则抛出异常,导致后面的键值不能正确读出
如果.kl文件中存在“key 184 ADDRESSBOOK”,但是其他地方未实现,则会抛出下面异常
E/KeyLayoutMap( 1149): /system/usr/keylayout/sprd-keypad8805ga.kl:156: expected keycode, got 'ADDRESSBOOK'
2. 在.kl文件中,是区别大小写的,所以如果没注意添加了Key 184 ADDRESSBOO 或KEY 185 IM
则会导致报以下错误,并使Event不能正确映射。
E/KeyLayoutMap( 1207): /system/usr/keylayout/sprd-keypad8805ga.kl:159: expected flag, got 'KEY'