Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hardware and software Environment

  • Android 7.1.2
  • remote controller

preface

I recently received a request to add several key value responses on the corresponding remote control on an existing OTT TV box, which are not responsive by default. After some research, we finally edited the KL (key layout) file and did not need to modify any source code. This article will share how Android handles key values.

Verify that Linux accepts remote control buttons

Execute the following commands in sequence to connect to the box over the network

Adb connect 192.168.1.100 ADB shell geteventCopy the code

Then press the key corresponding to the remote control. If there is a key value, it indicates that the underlying driver is OK. The third column is the key value corresponding to Linux, which is a hexadecimal number, for example, the top key value here is 0254, which is 596 in the base 10

Kl file

The kl file is used to map the underlying Linux keys to Android keys. Generally, the file format is as follows

key 580   DPAD_LEFT
Copy the code

Where, the first column indicates that this row is a normal key value, and key is a keyword that does not need to be modified. The second column is the key value in Linux, the decimal number; The third column is the name of the corresponding android key, which the Framework layer in Android passes all the way up. In the example above, 580 represents the value of the left direction key in the Linux driver, and the key name in Android is DPAD_LEFT

In android source code, kl files will generally have more than one, each manufacturer’s approach is not the same, the next to go to find the system used in which KL file

adb shell
dumpsys input
Copy the code

You can see that the kl file used by the controller is zx_cir0.kl. Of course, here, each manufacturer is different. The next step is to modify the KL file

See below frameworks/native/include/input/InputEventLabels h how to handle DPAD_LEFT?

Then look at the macro definition DEFINE_KEYCODE

#define DEFINE_KEYCODE(key) { #key, AKEYCODE_##key }
Copy the code

Here will be DPAD_LEFT linked with AKEYCODE_DPAD_LEFT, and AKEYCODE_DPAD_LEFT frameworks in the file/native/include/android/keycodes. Used to h, Its corresponding Android key value is 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,
Copy the code

Finally see the file frameworks/base/core/Java/android/view/KeyEvent. Java, it is responsible for the C key value passed to the Java side, AKEYCODE_DPAD_LEFT KEYCODE_DPAD_LEFT, The variable name is missing an 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;Copy the code

The key value test

To test the effect of the keys, if you don’t have a remote control at hand, you can simulate the test by using the input keyevent $command, such as input

adb shell
input keyevent 19
Copy the code

Means according to the up button below, the key value here are consistent with key values on the Android, namely source file frameworks/base/core/Java/Android/view/KeyEvent. Key values in Java