Android 10 新增物理按键

1.当驱动层添加完成配置后,通过getevent命令查看底层驱动配置的键值

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 00b7 00000001
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0001 00b7 00000000
/dev/input/event1: 0000 0000 000000002

———————————————————————————————————————————

2.添加KL 映射  

按键布局文件由  供应商、产品(可能还包括版本)ID 或输入设备名称来确定位置。系统会按顺序查阅以下路径,在以下任一kl文件下添加映射即可:

如果所有文件都没有,系统会默认是去读取/data/system/devices/keylayout/Generic.kl配置

  • /odm/usr/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
  • /vendor/usr/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
  • /system/usr/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
  • /data/system/devices/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
  • /odm/usr/keylayout/Vendor_XXXX_Product_XXXX.kl
  • /vendor/usr/keylayout/Vendor_XXXX_Product_XXXX.kl
  • /system/usr/keylayout/Vendor_XXXX_Product_XXXX.kl
  • /data/system/devices/keylayout/Vendor_XXXX_Product_XXXX.kl
  • /odm/usr/keylayout/DEVICE_NAME.kl
  • /vendor/usr/keylayout/DEVICE_NAME.kl
  • /system/usr/keylayout/DEVICE_NAME.kl
  • /data/system/devices/keylayout/DEVICE_NAME.kl
  • /odm/usr/keylayout/Generic.kl
  • /vendor/usr/keylayout/Generic.kl
  • /system/usr/keylayout/Generic.kl
  • /data/system/devices/keylayout/Generic.kl

由第一步getevent查询 可知此按键是00f9,10进制249,名称可以自定义,这里我命名为 CUSTIOM_KEY,首先需要在/data/system/devices/keylayout/Generic.kl添加红色部分:

....
key 165   MEDIA_PREVIOUS
key 168   MEDIA_REWIND
key 159   MEDIA_FAST_FORWARD
key 183   CUSTIOM_KEY   

....

———————————————————————————————————————————

3. 在目录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_CUSTIOM_KEY  = 289

———————————————————————————————————————————

4. 在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(CUSTIOM_KEY),
    { nullptr, 0 }
};

———————————————————————————————————————————

5.在KeyEvent.java里面最后依次添加,并把LAST_KEYCODE改为才添加的   

红色标记部分

    /**
     * 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_CUSTIOM_KEY = 289;
     @TestApi
    public static final int LAST_KEYCODE = KEYCODE_CUSTIOM_KEY;

———————————————————————————————————————————

6.更新api文档

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; 
  }

———————————————————————————————————————————

7.最后一步在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_CUSTIOM_KEY" value="289" />
    </attr>
———————————————————————————————————————————

验证:以上步骤完成 

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

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

注:文中红色标记部分为修改或者添加部分

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


 

上一篇:靠直播电商源码实现热门电商平台搜索栏样式


下一篇:Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法