以新增一个新按键F15为例
前提是bsp部分配置好GPIO相关部分,并在mtk-kpd.kl文件中配置新增按键,与framework的qwerty.kl相对应
a. 添加与BSP配置对应关系
frameworks/base/data/keyboards/qwerty.kl
### 新增F15
key 185 F15
b. 修改KeyEvent
frameworks/base/core/java/android/view/KeyEvent.java
// 新增
public static final int KEYCODE_F15 = 290;
// 修改 LAST_KEYCODE
public static final int LAST_KEYCODE = KEYCODE_F15;
c. 修改attrs配置档
frameworks/base/core/res/res/values/attrs.xml
<attr name="keycode">
<enum name="KEYCODE_F15" value="290" />
</attr>
d. InputEventLabels.h
frameworks/native/include/input/InputEventLabels.h
static const InputEventLabel KEYCODES[] = {
DEFINE_KEYCODE(F15), //Added
}
e. keycodes.h
frameworks/native/include/android/keycodes.h
enum {
AKEYCODE_F15 = 290, //添加
};
f. 最后在PhoneWindowManager.java中修改interceptKeyBeforeQueueing方法
frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
//。。。。。
case KeyEvent.KEYCODE_F15:
{
//do somethings
break;
}
//。。。。。
}
g. 编译
由于在framework中新增了一个API,所以在编辑之间先执行
make update-api <model>
来更新framework的公共API文件并提交到代码库。不执行上面的指令编译会报错
******************************
You have tried to change the API from what has been previously approved.
To make these errors go away, you have two choices:
1) You can add "@hide" javadoc comments to the methods, etc. listed in the
errors above.
2) You can update current.txt by executing the following command:
make update-api
To submit the revised current.txt to the main Android repository,
you will need approval.
******************************
后记
在和BSP进行联调时,发现键值上层一直无法监听到底层抛上来的键值,最后发现是BSP没有为新增键值使用专属的kl,那么系统就会使用默认的那个Generic.kl文件。而第a步的修改则不起作用。
android/frameworks/base/data/keyboards/Generic.kl
key 185 F15
补充知识:
android kl(key layout)文件是一个映射文件,是标准linux与anroid的键值映射文件,kl文件可以有很多个,但是它有一个使用优先级:
/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
調試命令:
#獲取當前點擊的event事件詳情
adb shell getevent -l
#獲取系統輸入輸出節點的詳情
adb shell dumpsys input
#模擬按鍵輸入
adb shell input keyevent (int)keycode
擴展知識參考:
https://code84.com/778343.htmlhttps://code84.com/778343.html