新增物理按键处理流程

最近项目新增了一个物理按键,当system添加好scancode之后,记录一下上层的处理流程。

djtang:/data1/localProject/Tokyo_TF$adb shell
Seoul_TF:/ $ 
Seoul_TF:/ $ getevent
add device 1: /dev/input/event2
  name:     "mtk-tpd"
add device 2: /dev/input/event0
  name:     "ACCDET"
add device 3: /dev/input/event1
  name:     "mtk-kpd"
/dev/input/event1: 0001 00f9 00000001
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0001 00f9 00000000
/dev/input/event1: 0000 0000 00000000

此按键scancode是00f9,10进制249,名称AI,首先需要在/system/usr/keylayout/mtk-kpd.kl添加:

key 164   MEDIA_PLAY_PAUSE
key 128   MEDIA_STOP
key 163   MEDIA_NEXT
key 165   MEDIA_PREVIOUS
key 168   MEDIA_REWIND
key 159   MEDIA_FAST_FORWARD
key 249   AI

在keycodes.h里面按顺序添加到最后 frameworks/native/include/android/keycodes.h

......
AKEYCODE_THUMBS_UP = 286,
/** Thumbs down key. Apps can use this to let user downvote content. */
AKEYCODE_THUMBS_DOWN = 287,
/** Used to switch current account that is consuming content.
 * May be consumed by system to switch current viewer profile. */
AKEYCODE_PROFILE_SWITCH = 288,
/* AI Key */
AKEYCODE_AI = 289

在InputEventLabels.h里面也按顺序添加在最后面 frameworks/native/include/input/InputEventLabels.h

    ......
    DEFINE_KEYCODE(ALL_APPS),
    DEFINE_KEYCODE(REFRESH),
    DEFINE_KEYCODE(THUMBS_UP),
    DEFINE_KEYCODE(THUMBS_DOWN),
    DEFINE_KEYCODE(PROFILE_SWITCH),
    DEFINE_KEYCODE(AI),
    { nullptr, 0 }
};
在KeyEvent.java里面最后依次添加,并把LAST_KEYCODE改为才添加的KEYCODE_AI

    public static final int KEYCODE_THUMBS_DOWN = 287;
    /**
     * Key code constant: Used to switch current {@link android.accounts.Account} that is
     * consuming content. May be consumed by system to set account globally.
     */
    public static final int KEYCODE_PROFILE_SWITCH = 288;
    /**
     * @hide
     */
    public static final int KEYCODE_AI = 289;
     @TestApi
    public static final int LAST_KEYCODE = KEYCODE_AI;

test-current.txt这个API文档也需要同步修改LAST_KEYCODE frameworks/base/api/test-current.txt

  public class KeyEvent extends android.view.InputEvent implements android.os.Parcelable {
    method public static String actionToString(int);
    method public final void setDisplayId(int);
    field public static final int LAST_KEYCODE = 289; // 0x121
  }

attrs.xml最后面同步添加 frameworks/base/core/res/res/values/attrs.xml

    <enum name="KEYCODE_REFRESH" value="285" />
    <enum name="KEYCODE_THUMBS_UP" value="286" />
    <enum name="KEYCODE_THUMBS_DOWN" value="287" />
    <enum name="KEYCODE_PROFILE_SWITCH" value="288" />
    <enum name="KEYCODE_AI" value="289" />
</attr>

都添加好了PhoneWindowManager.java里就能收到此按键点击事件了,对事件做对应处理即可

@Override
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
.......
 // Handle special keys.
    switch (keyCode) {
        case KeyEvent.KEYCODE_AI:{
            .......
            break;
        }
        case KeyEvent.KEYCODE_BACK:{
        }
.......
}

添加新的物理按键,要上层收到对应事件,只需要按部就班参照系统按键实现即可,system提供scancode,上层转换为keycode使用。

评论