欢迎访问本站,希望站内信息可以帮助到您! 博主个人微信:xituxiaoshutong100

android添加遥控器按键处理

Android 迷途小书童 0评论

软硬件环境

前言

最近接到个需求,在现有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)
发表我的评论
取消评论

表情