欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入我们的社群。

android添加遥控器按键处理

Android 迷途小书童 4年前 (2020-12-30) 3859次浏览 0个评论

软硬件环境

  • android 7.1.2
  • remote controller

前言

最近接到个需求,在现有 OTT 电视盒子上添加相应遥控器上的几个键值响应,默认情况下这几个按键是不响应的。研究了一番,最后通过编辑 kl(key layout) 文件就搞定了,不需要修改任何源码。本文就来分享 android 是如何来处理键值的。

确认linux是否接收遥控器按键

依次执行下面的命令,通过网络连接到盒子当中

# 通过ip连接
adb connect 192.168.1.100
adb shell
getevent

然后按下遥控器对应的按键,如果有键值出来,说明,底层的 driver 已经 OK,第三列数值就是 linux 对应的键值,这是16进制数,如我这里的上键键值是0254,也就是10进制中的596

android

kl文件

kl 文件的作用是把底层 linux 传上来的键值映射为 Android 键值,一般情况下,文件的格式如下

key 580   DPAD_LEFT

其中,第一列表示这行是普通的键值,key 是关键字,不需要修改;第二列是 linux 中的键值,十进制数;第三列是 android 对应键的名称,android 中的 framework 层会把这个键名称一直往上层传递。上例中的580代表的就是 linux 驱动中方向左键的值,而这个键在 android 中的名称是 DPAD_LEFT

android 源码中,kl 文件一般会有多个,各个厂家的做法不尽相同,接下来就要去寻找系统中使用的是哪个 kl 文件

adb shell
dumpsys input

可以看到控制器使用的 kl 文件是 zx_cir0.kl。当然这里,每个厂家是不一样的。看到这里,后续的操作其实就是去修改这个 kl 文件了

android

android

下面看看 frameworks/native/include/input/InputEventLabels.h 如何处理 DPAD_LEFT 的?

android

接着看看 DEFINE_KEYCODE 这个宏定义

#define DEFINE_KEYCODE(key) { #key, AKEYCODE_##key }

这里就会将 DPAD_LEFTAKEYCODE_DPAD_LEFT 联系起来了,而 AKEYCODE_DPAD_LEFT 会在文件 frameworks/native/include/android/keycodes.h 中使用到,它对应的 android 键值是21

/**
 * Key codes.
 */
enum {
    /** Unknown key code. */
    AKEYCODE_UNKNOWN         = 0,
    /** Soft Left key.
     * Usually situated below the display on phones and used as a multi-function
     * feature key for selecting a software defined function shown on the bottom left
     * of the display. */
    AKEYCODE_SOFT_LEFT       = 1,
    /** Soft Right key.
     * Usually situated below the display on phones and used as a multi-function
     * feature key for selecting a software defined function shown on the bottom right
     * of the display. */
    AKEYCODE_SOFT_RIGHT      = 2,
    /** Home key.
     * This key is handled by the framework and is never delivered to applications. */
    AKEYCODE_HOME            = 3,
    /** Back key. */
    AKEYCODE_BACK            = 4,
    /** Call key. */
    AKEYCODE_CALL            = 5,
    /** End Call key. */
    AKEYCODE_ENDCALL         = 6,
    /** '0' key. */
    AKEYCODE_0               = 7,
    /** '1' key. */
    AKEYCODE_1               = 8,
    /** '2' key. */
    AKEYCODE_2               = 9,
    /** '3' key. */
    AKEYCODE_3               = 10,
    /** '4' key. */
    AKEYCODE_4               = 11,
    /** '5' key. */
    AKEYCODE_5               = 12,
    /** '6' key. */
    AKEYCODE_6               = 13,
    /** '7' key. */
    AKEYCODE_7               = 14,
    /** '8' key. */
    AKEYCODE_8               = 15,
    /** '9' key. */
    AKEYCODE_9               = 16,
    /** '*' key. */
    AKEYCODE_STAR            = 17,
    /** '#' key. */
    AKEYCODE_POUND           = 18,
    /** Directional Pad Up key.
     * May also be synthesized from trackball motions. */
    AKEYCODE_DPAD_UP         = 19,
    /** Directional Pad Down key.
     * May also be synthesized from trackball motions. */
    AKEYCODE_DPAD_DOWN       = 20,
    /** Directional Pad Left key.
     * May also be synthesized from trackball motions. */
    AKEYCODE_DPAD_LEFT       = 21,
    /** Directional Pad Right key.
     * May also be synthesized from trackball motions. */
    AKEYCODE_DPAD_RIGHT      = 22,
    /** Directional Pad Center key.
     * May also be synthesized from trackball motions. */
    AKEYCODE_DPAD_CENTER     = 23,

最后看看文件 frameworks/base/core/java/android/view/KeyEvent.java,它负责将 C 端的键值传递到 java 端,AKEYCODE_DPAD_LEFTKEYCODE_DPAD_LEFT,变量名最前面少了个 A

public class KeyEvent extends InputEvent implements Parcelable {
    /** Key code constant: Unknown key code. */
    public static final int KEYCODE_UNKNOWN         = 0;
    /** Key code constant: Soft Left key.
     * Usually situated below the display on phones and used as a multi-function
     * feature key for selecting a software defined function shown on the bottom left
     * of the display. */
    public static final int KEYCODE_SOFT_LEFT       = 1;
    /** Key code constant: Soft Right key.
     * Usually situated below the display on phones and used as a multi-function
     * feature key for selecting a software defined function shown on the bottom right
     * of the display. */
    public static final int KEYCODE_SOFT_RIGHT      = 2;
    /** Key code constant: Home key.
     * This key is handled by the framework and is never delivered to applications. */
    public static final int KEYCODE_HOME            = 3;
    /** Key code constant: Back key. */
    public static final int KEYCODE_BACK            = 4;
    /** Key code constant: Call key. */
    public static final int KEYCODE_CALL            = 5;
    /** Key code constant: End Call key. */
    public static final int KEYCODE_ENDCALL         = 6;
    /** Key code constant: '0' key. */
    public static final int KEYCODE_0               = 7;
    /** Key code constant: '1' key. */
    public static final int KEYCODE_1               = 8;
    /** Key code constant: '2' key. */
    public static final int KEYCODE_2               = 9;
    /** Key code constant: '3' key. */
    public static final int KEYCODE_3               = 10;
    /** Key code constant: '4' key. */
    public static final int KEYCODE_4               = 11;
    /** Key code constant: '5' key. */
    public static final int KEYCODE_5               = 12;
    /** Key code constant: '6' key. */
    public static final int KEYCODE_6               = 13;
    /** Key code constant: '7' key. */
    public static final int KEYCODE_7               = 14;
    /** Key code constant: '8' key. */
    public static final int KEYCODE_8               = 15;
    /** Key code constant: '9' key. */
    public static final int KEYCODE_9               = 16;
    /** Key code constant: '*' key. */
    public static final int KEYCODE_STAR            = 17;
    /** Key code constant: '#' key. */
    public static final int KEYCODE_POUND           = 18;
    /** Key code constant: Directional Pad Up key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_UP         = 19;
    /** Key code constant: Directional Pad Down key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_DOWN       = 20;
    /** Key code constant: Directional Pad Left key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_LEFT       = 21;
    /** Key code constant: Directional Pad Right key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_RIGHT      = 22;
    /** Key code constant: Directional Pad Center key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_CENTER     = 23;

键值测试

想测试键值对应的效果,如果碰巧手头上没有遥控器的话,可以通过命令 input keyevent $键值 来模拟测试,比如输入

adb shell
input keyevent 19

就表示按下方向上键,这里的键值与 Android 上的键值一致,也就是源码文件 frameworks/base/core/java/android/view/KeyEvent.java 里的键值

android

喜欢 (0)

您必须 登录 才能发表评论!