本文转载自:http://developer.t-firefly.com/thread-251-1-1.html
网上存在一些关于Android系统添加自定义按键的文章,但大多针对Android2.3和4.0系统的,许多文件都已经变动位置了,这两天我总结了一些,写出来欢迎大家交流与指正:
Android系统通过*.kl文件将Linux按键传给上层,最新的Android4.4已经不再默认qwerty.kl文件了,代之的是frameworks/base/data/keyboards/Generic.kl文件,当然如果定义板级键值文件,还是以rk29-keypad.kl文件优先。
在
3288/device/rockchip/rksdk
目录下的
rk29-keypad.kl
文件中有相关键值的定义:
- key 59 MENU
- key 102 HOME
- key 114 VOLUME_DOWN
- key 115 VOLUME_UP
- key 116 POWE WAKE
- key 143 NOTIFICATION WAKE
- key 158 BACK
- key 212 CAMERA
- key 217 SEARCH
说明:
WAKE: 当设备睡眠时按下此键,设备将被唤醒,按键事件将会被发送到应用程序。
WAKE_DROPPED: 当设备睡眠时按下此键,设备将被唤醒,而按键事件不会被发送到应用程序。
瑞星微的按键分为GPIO和ADC两种,定义在/kernel/arch/arm/boot/dts目录下的firefly-rk3288.dts文件:
- &adc {
- status = "okay";
- key {
- compatible = "rockchip, key";
- io-channels = <&adc 1>;
- vol-up-key {
- linux, code = <115>;
- label = "volume up";
- rockchip, adc_value = <1>;
- };
- vol-down-key {
- linux, code = <114>;
- label = "volume down";
- rockchip, adc_value = <170>;
- };
- power-key {
- gpios = <&gpio0 GPIO_A4 GPIO_ACTIVE_LOW>;
- linux, code = <116>;
- label = "power";
- gpio-key, wakeup; // 具有唤醒功能
- };
- menu-key {
- linux, code = <139>;
- label = "menu";
- rockchip, adc_value = <355>;
- };
- home-key {
- linux, code = <102>;
- label = "home";
- rockchip, adc_value = <746>;
- };
- back-key {
- linux, code = <158>;
- label = "back";
- rockchip, adc_value = <560>;
- };
- camera-key {
- linux, code = <212>;
- label = "camera";
- rockchip, adc_value = <450>;
- };
- };
先在linux内核中添加新的按键值(当然不用宏定义直接写数值也可以),网上一般写在/include/linux/input/input.h文件中添加,实际3.10内核在/include/uapi/ linux/input.h文件中:
#defineKEY_RESERVED 0
#define KEY_ESC 1
#define KEY_1 2
...
#defineKEY_MY 250
对于Android层次,在rk29-keypad.kl文件中增加按键值后,还需要修改一些文件,系统才能处理,首先修
改KeycodeLabels.h文件,4.4版本将其移到了frameworks/native/include/input目录下,对KEYCODES数组添加按键码:
static const KeycodeLabelKEYCODES[] = {
...
{ " MY_KEYS" ,250},
改KeycodeLabels.h文件,4.4版本将其移到了frameworks/native/include/input目录下,对KEYCODES数组添加按键码:
static const KeycodeLabelKEYCODES[] = {
...
{ " MY_KEYS" ,250},
{ NULL,0 }
最后一定要以NULL结尾,其中的TV_KEYMOUSE_LEFT按键就是瑞星微添加的,再去frameworks/native/include/android/keycode.h文件中添加:enum {
AKEYCODE_UNKNOWN = 0,
....
....
添加
AKEYCODE_MY_KEYS =250,
最后一定要以NULL结尾,其中的TV_KEYMOUSE_LEFT按键就是瑞星微添加的,再去frameworks/native/include/android/keycode.h文件中添加:enum {
AKEYCODE_UNKNOWN = 0,
....
....
添加
AKEYCODE_MY_KEYS =250,
同样AKEYCODE_TV_KEYMOUSE_LEFT宏也是瑞星微添加的,如果要添加系统按键值,还需要修改
frameworks/base/libs/ui/input.java(android4.4已变为frameworks/native/libs/input/input.cpp)文件:
boolKeyEvent::hasDefaultAction(int32_t keyCode)
{
switch (keyCode) {
case AKEYCODE_HOME:
case AKEYCODE_BACK:
...
case
AKEYCODE_MY_KEYS:
return true;
还有下面的:
boolKeyEvent::isSystemKey(int32_t keyCode) {
switch (keyCode) {
case AKEYCODE_MENU:
...
case
AKEYCODE_MY_KEYS:
return true;
}
return false;
}
还要修改frameworks/base/core/java/android/view/KeyEvent.java文件:
public staticfinal int KEYCODE_TV_MEDIA_PAUSE = 249;
public staticfinal int KEYCODE_TV_MEDIA_PAUSE = 249;
public staticfinal int
KEYCODE_MY_KEYS = 250;
private staticfinal int LAST_KEYCODE = KEYCODE_MY_KEYS;
注意一定要修改LAST_KEYCODE,下面的populateKeycodeSymbolicNames数组也需要添加:
names.append(
KEYCODE_MY_KEYS, " KEYCODE_MY_KEYS ");
修改frameworks/base/core/res/res/values/attr.xml文件:
...
对于网上说的/external/webkit/Source/WebKit/android/plugins/ANPKeyCodes.h文件已经移除。
以上文件都改完之后frameworks/base/api/current.txt也要随之更新,如果未更新,可运行 makeupdate-api 进行更新
如果对新键值进行处理,可以通过获取相应的keycode,对它进行处理。对于按键事件的处理一般如下文件中:
frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@Override
public intinterceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn)
{
...
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
...
case KeyEvent.
KEYCODE_MY_KEYS: // 添加处理代码