Android : 输入设备键值映射流程

Posted sheldon_blogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android : 输入设备键值映射流程相关的知识,希望对你有一定的参考价值。

一、Android输入子系统简介:

  android输入事件的源头是位于/dev/input/下的设备节点,而输入系统的终点是由WMS管理的某个窗口。最初的输入事件为内核生成的原始事件,而最终交付给窗口的则是KeyEvent或MotionEvent对象。因此Android输入系统的主要工作是读取设备节点中的原始事件,将其加工封装,然后派发给一个特定的窗口以及窗口中的控件。这个过程由InputManagerService(以下简称IMS)系统服务为核心的多个参与者共同完成。

                        图1:输入系统的总体流程与参与者

 

二、键值映射关系:

    ①IR硬件扫描码在驱动里面被映射为 include/uapi/linux/input.h 里面定义的某个键值,但这个键值只在linux系统(内核)中使用。
  ②Android通过源码目录下的 device/xxx/xxx.kl(keylayout) 文件完成linux键值到Android系统要使用的键值映射。
 
  以HID设备为例,首先内核中的键值转换在drivers/hid/hid-input.c 中进行映射,键值通道也有多种类型,例如:keyboard、consumer 等等;
  //keyboard通道键值则是在如下数组添加修改:
static const unsigned char hid_keyboard[256] = {
      0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
     50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
      4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
     27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
     65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
    105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
     72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
    191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
    115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
    122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,
    unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
    unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
    unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
    unk,unk,unk,unk,unk,unk,unk,unk,111,unk,unk,unk,unk,unk,unk,unk,
     29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
    150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
};

......

//然后在以下代码部分使用:
    case HID_UP_KEYBOARD:
        set_bit(EV_REP, input->evbit);

        if ((usage->hid & HID_USAGE) < 256) {
            if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
            map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
        } else
            map_key(KEY_UNKNOWN);

        break;

 //consumer通道键值则是在如下添加修改:

case HID_UP_CONSUMER:    /* USB HUT v1.12, pages 75-84 */
        switch (usage->hid & HID_USAGE) {
        case 0x000: goto ignore;
        case 0x030: map_key_clear(KEY_POWER);        break;
        case 0x031: map_key_clear(KEY_RESTART);        break;
        case 0x032: map_key_clear(KEY_SLEEP);        break;
        case 0x034: map_key_clear(KEY_SLEEP);        break;
        case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE);    break;
        case 0x036: map_key_clear(BTN_MISC);        break;
        case 0x040: map_key_clear(KEY_MENU);        break; /* Menu */
        case 0x041: map_key_clear(KEY_SELECT);        break; /* Menu Pick */
        case 0x042: map_key_clear(KEY_UP);        break; /* Menu Up */
        case 0x043: map_key_clear(KEY_DOWN);        break; /* Menu Down */
        case 0x044: map_key_clear(KEY_LEFT);        break; /* Menu Left */
        case 0x045: map_key_clear(KEY_RIGHT);        break; /* Menu Right */
        case 0x046: map_key_clear(KEY_ESC);        break; /* Menu Escape */
        case 0x047: map_key_clear(KEY_KPPLUS);        break; /* Menu Value Increase */
        case 0x048: map_key_clear(KEY_KPMINUS);        break; /* Menu Value Decrease */
        case 0x060: map_key_clear(KEY_INFO);        break; /* Data On Screen */
        case 0x061: map_key_clear(KEY_SUBTITLE);    break; /* Closed Caption */
        case 0x063: map_key_clear(KEY_VCR);        break; /* VCR/TV */
        case 0x065: map_key_clear(KEY_CAMERA);        break; /* Snapshot */
        case 0x069: map_key_clear(KEY_RED);        break;
        case 0x06a: map_key_clear(KEY_GREEN);        break;
        case 0x06b: map_key_clear(KEY_BLUE);        break;
        case 0x06c: map_key_clear(KEY_YELLOW);        break;
        case 0x06d: map_key_clear(KEY_ZOOM);        break;
        case 0x082: map_key_clear(KEY_VIDEO_NEXT);    break;
        case 0x083: map_key_clear(KEY_LAST);        break;
        case 0x084: map_key_clear(KEY_ENTER);        break;
        case 0x088: map_key_clear(KEY_PC);        break;
        case 0x089: map_key_clear(KEY_TV);        break;
        case 0x08a: map_key_clear(KEY_WWW);        break;
        case 0x08b: map_key_clear(KEY_DVD);        break;
        case 0x08c: map_key_clear(KEY_PHONE);        break;
        case 0x08d: map_key_clear(KEY_PROGRAM);        break;
        case 0x08e: map_key_clear(KEY_VIDEOPHONE);    break;
        case 0x08f: map_key_clear(KEY_GAMES);        break;
        case 0x090: map_key_clear(KEY_MEMO);        break;
        case 0x091: map_key_clear(KEY_CD);        break;
        case 0x092: map_key_clear(KEY_VCR);        break;
        case 0x093: map_key_clear(KEY_TUNER);        break;
        case 0x094: map_key_clear(KEY_EXIT);        break;
        case 0x095: map_key_clear(KEY_HELP);        break;
        case 0x096: map_key_clear(KEY_TAPE);        break;
        case 0x097: map_key_clear(KEY_TV2);        break;
        case 0x098: map_key_clear(KEY_SAT);        break;
        case 0x09a: map_key_clear(KEY_PVR);        break;
        case 0x09c: map_key_clear(KEY_CHANNELUP);    break;
        case 0x09d: map_key_clear(KEY_CHANNELDOWN);    break;
        case 0x0a0: map_key_clear(KEY_VCR2);        break;
        case 0x0b0: map_key_clear(KEY_PLAY);        break;
        case 0x0b1: map_key_clear(KEY_PAUSE);        break;
        case 0x0b2: map_key_clear(KEY_RECORD);        break;
        case 0x0b3: map_key_clear(KEY_FASTFORWARD);    break;
        case 0x0b4: map_key_clear(KEY_REWIND);        break;
        case 0x0b5: map_key_clear(KEY_NEXTSONG);    break;
        case 0x0b6: map_key_clear(KEY_PREVIOUSSONG);    break;
        case 0x0b7: map_key_clear(KEY_STOPCD);        break;
        case 0x0b8: map_key_clear(KEY_EJECTCD);        break;
        case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT);    break;
        case 0x0b9: map_key_clear(KEY_SHUFFLE);        break;
        case 0x0bf: map_key_clear(KEY_SLOW);        break;
        case 0x0cd: map_key_clear(KEY_PLAYPAUSE);    break;
        case 0x0e0: map_abs_clear(ABS_VOLUME);        break;
        case 0x0e2: map_key_clear(KEY_MUTE);        break;
        case 0x0e5: map_key_clear(KEY_BASSBOOST);    break;
        case 0x0e9: map_key_clear(KEY_VOLUMEUP);    break;
        case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);    break;
        case 0x0f5: map_key_clear(KEY_SLOW);        break;
        case 0x182: map_key_clear(KEY_BOOKMARKS);    break;
        case 0x183: map_key_clear(KEY_CONFIG);        break;
        case 0x184: map_key_clear(KEY_WORDPROCESSOR);    break;
        case 0x185: map_key_clear(KEY_EDITOR);        break;
        case 0x186: map_key_clear(KEY_SPREADSHEET);    break;
        case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);    break;
        case 0x188: map_key_clear(KEY_PRESENTATION);    break;
        case 0x189: map_key_clear(KEY_DATABASE);    break;
        case 0x18a: map_key_clear(KEY_MAIL);        break;
        case 0x18b: map_key_clear(KEY_NEWS);        break;
        case 0x18c: map_key_clear(KEY_VOICEMAIL);    break;
        case 0x18d: map_key_clear(KEY_ADDRESSBOOK);    break;
        case 0x18e: map_key_clear(KEY_CALENDAR);    break;
        case 0x191: map_key_clear(KEY_FINANCE);        break;
        case 0x192: map_key_clear(KEY_CALC);        break;
        case 0x193: map_key_clear(KEY_PLAYER);        break;
        case 0x194: map_key_clear(KEY_FILE);        break;
        case 0x196: map_key_clear(KEY_WWW);        break;
        case 0x199: map_key_clear(KEY_CHAT);        break;
        case 0x19c: map_key_clear(KEY_LOGOFF);        break;
        case 0x19e: map_key_clear(KEY_COFFEE);        break;
        case 0x1a6: map_key_clear(KEY_HELP);        break;
        case 0x1a7: map_key_clear(KEY_DOCUMENTS);    break;
        case 0x1ab: map_key_clear(KEY_SPELLCHECK);    break;
        case 0x1ae: map_key_clear(KEY_KEYBOARD);    break;
        case 0x1b6: map_key_clear(KEY_IMAGES);        break;
        case 0x1b7: map_key_clear(KEY_AUDIO);        break;
        case 0x1b8: map_key_clear(KEY_VIDEO);        break;
        case 0x1bc: map_key_clear(KEY_MESSENGER);    break;
        case 0x1bd: map_key_clear(KEY_INFO);        break;
        case 0x201: map_key_clear(KEY_NEW);        break;
        case 0x202: map_key_clear(KEY_OPEN);        break;
        case 0x203: map_key_clear(KEY_CLOSE);        break;
        case 0x204: map_key_clear(KEY_EXIT);        break;
        case 0x207: map_key_clear(KEY_SAVE);        break;
        case 0x208: map_key_clear(KEY_PRINT);        break;
        case 0x209: map_key_clear(KEY_PROPS);        break;
        case 0x21a: map_key_clear(KEY_UNDO);        break;
        case 0x21b: map_key_clear(KEY_COPY);        break;
        case 0x21c: map_key_clear(KEY_CUT);        break;
        case 0x21d: map_key_clear(KEY_PASTE);        break;
        case 0x21f: map_key_clear(KEY_FIND);        break;
        case 0x221: map_key_clear(KEY_SEARCH);        break;
        case 0x222: map_key_clear(KEY_GOTO);        break;
        case 0x223: map_key_clear(KEY_HOMEPAGE);    break;
        case 0x224: map_key_clear(KEY_BACK);        break;
        case 0x225: map_key_clear(KEY_FORWARD);        break;
        case 0x226: map_key_clear(KEY_STOP);        break;
        case 0x227: map_key_clear(KEY_REFRESH);        break;
        case 0x22a: map_key_clear(KEY_BOOKMARKS);    break;
        case 0x22d: map_key_clear(KEY_ZOOMIN);        break;
        case 0x22e: map_key_clear(KEY_ZOOMOUT);        break;
        case 0x22f: map_key_clear(KEY_ZOOMRESET);    break;
        case 0x233: map_key_clear(KEY_SCROLLUP);    break;
        case 0x234: map_key_clear(KEY_SCROLLDOWN);    break;
        case 0x238: map_rel(REL_HWHEEL);        break;
        case 0x23d: map_key_clear(KEY_EDIT);        break;
        case 0x25f: map_key_clear(KEY_CANCEL);        break;
        case 0x269: map_key_clear(KEY_INSERT);        break;
        case 0x26a: map_key_clear(KEY_DELETE);        break;
        case 0x279: map_key_clear(KEY_REDO);        break;
        case 0x289: map_key_clear(KEY_REPLY);        break;
        case 0x28b: map_key_clear(KEY_FORWARDMAIL);    break;
        case 0x28c: map_key_clear(KEY_SEND);        break;
        default:    goto ignore;
        }
        break;

通过 map_key_clear 宏将原始键值(usage->hid & HID_USAGE)转换成内核的定义,映射函数的具体实现可看内核源码,

以上键值的定义在 include/uapi/linux/input-event-codes.h (内核代码,较新版本定义整合进了input.h),对应到Android系统层的头文件则是 bionic/libc/kernel/uapi/linux/input-event-codes.h:

#define KEY_RESERVED  0
#define KEY_ESC   1
#define KEY_1   2
#define KEY_2   3
#define KEY_3   4
#define KEY_4   5
#define KEY_5   6
#define KEY_6   7
#define KEY_7   8
#define KEY_8   9
#define KEY_9   10
#define KEY_0   11
#define KEY_MINUS  12
#define KEY_EQUAL  13
#define KEY_BACKSPACE  14
#define KEY_TAB   15
#define KEY_Q   16
#define KEY_W   17
#define KEY_E   18
#define KEY_R   19
#define KEY_T   20
#define KEY_Y   21
#define KEY_U   22
#define KEY_I   23
#define KEY_O   24
#define KEY_P   25
#define KEY_LEFTBRACE  26
#define KEY_RIGHTBRACE  27
#define KEY_ENTER  28
#define KEY_LEFTCTRL  29
#define KEY_A   30
#define KEY_S   31
#define KEY_D   32
#define KEY_F   33
#define KEY_G   34
#define KEY_H   35
#define KEY_J   36
#define KEY_K   37
#define KEY_L   38
#define KEY_SEMICOLON  39
#define KEY_APOSTROPHE  40
#define KEY_GRAVE  41
#define KEY_LEFTSHIFT  42
#define KEY_BACKSLASH  43
#define KEY_Z   44
#define KEY_X   45
#define KEY_C   46
#define KEY_V   47
#define KEY_B   48
#define KEY_N   49
#define KEY_M   50
#define KEY_COMMA  51
#define KEY_DOT   52
#define KEY_SLASH  53
#define KEY_RIGHTSHIFT  54
#define KEY_KPASTERISK  55
#define KEY_LEFTALT  56
#define KEY_SPACE  57
#define KEY_CAPSLOCK  58
#define KEY_F1   59
#define KEY_F2   60
#define KEY_F3   61
#define KEY_F4   62
#define KEY_F5   63
#define KEY_F6   64
#define KEY_F7   65
#define KEY_F8   66
#define KEY_F9   67
#define KEY_F10   68
#define KEY_NUMLOCK  69
#define KEY_SCROLLLOCK  70
#define KEY_KP7   71
#define KEY_KP8   72
#define KEY_KP9   73
#define KEY_KPMINUS  74
#define KEY_KP4   75
#define KEY_KP5   76
#define KEY_KP6   77
#define KEY_KPPLUS  78
#define KEY_KP1   79
#define KEY_KP2   80
#define KEY_KP3   81
#define KEY_KP0   82
#define KEY_KPDOT  83

#define KEY_ZENKAKUHANKAKU 85
#define KEY_102ND  86
#define KEY_F11   87
#define KEY_F12   88
#define KEY_RO   89
#define KEY_KATAKANA  90
#define KEY_HIRAGANA  91
#define KEY_HENKAN  92
#define KEY_KATAKANAHIRAGANA 93
#define KEY_MUHENKAN  94
#define KEY_KPJPCOMMA  95
#define KEY_KPENTER  96
#define KEY_RIGHTCTRL  97
#define KEY_KPSLASH  98
#define KEY_SYSRQ  99
#define KEY_RIGHTALT  100
#define KEY_LINEFEED  101
#define KEY_HOME  102
#define KEY_UP   103
#define KEY_PAGEUP  104
#define KEY_LEFT  105
#define KEY_RIGHT  106
#define KEY_END   107
#define KEY_DOWN  108
#define KEY_PAGEDOWN  109
#define KEY_INSERT  110
#define KEY_DELETE  111
#define KEY_MACRO  112
#define KEY_MUTE  113
#define KEY_VOLUMEDOWN  114
#define KEY_VOLUMEUP  115
#define KEY_POWER  116 /* SC System Power Down */
#define KEY_KPEQUAL  117
#define KEY_KPPLUSMINUS  118
#define KEY_PAUSE  119
#define KEY_SCALE  120 /* AL Compiz Scale (Expose) */

#define KEY_KPCOMMA  121
#define KEY_HANGEUL  122
#define KEY_HANGUEL  KEY_HANGEUL
#define KEY_HANJA  123
#define KEY_YEN   124
#define KEY_LEFTMETA  125
#define KEY_RIGHTMETA  126
#define KEY_COMPOSE  127

#define KEY_STOP  128 /* AC Stop */
#define KEY_AGAIN  129
#define KEY_PROPS  130 /* AC Properties */
#define KEY_UNDO  131 /* AC Undo */
#define KEY_FRONT  132
#define KEY_COPY  133 /* AC Copy */
#define KEY_OPEN  134 /* AC Open */
#define KEY_PASTE  135 /* AC Paste */
#define KEY_FIND  136 /* AC Search */
#define KEY_CUT   137 /* AC Cut */
#define KEY_HELP  138 /* AL Integrated Help Center */
#define KEY_MENU  139 /* Menu (show menu) */
#define KEY_CALC  140 /* AL Calculator */
#define KEY_SETUP  141
#define KEY_SLEEP  142 /* SC System Sleep */
#define KEY_WAKEUP  143 /* System Wake Up */
#define KEY_FILE  144 /* AL Local Machine Browser */
#define KEY_SENDFILE  145
#define KEY_DELETEFILE  146
#define KEY_XFER  147
#define KEY_PROG1  148
#define KEY_PROG2  149
#define KEY_WWW   150 /* AL Internet Browser */
#define KEY_MSDOS  151
#define KEY_COFFEE  152 /* AL Terminal Lock/Screensaver */
#define KEY_SCREENLOCK  KEY_COFFEE
#define KEY_DIRECTION  153
#define KEY_CYCLEWINDOWS 154
#define KEY_MAIL  155
#define KEY_BOOKMARKS  156 /* AC Bookmarks */
#define KEY_COMPUTER  157
#define KEY_BACK  158 /* AC Back */
#define KEY_FORWARD  159 /* AC Forward */
#define KEY_CLOSECD  160
#define KEY_EJECTCD  161
#define KEY_EJECTCLOSECD 162
#define KEY_NEXTSONG  163
#define KEY_PLAYPAUSE  164
#define KEY_PREVIOUSSONG 165
#define KEY_STOPCD  166
#define KEY_RECORD  167
#define KEY_REWIND  168
#define KEY_PHONE  169 /* Media Select Telephone */
#define KEY_ISO   170
#define KEY_CONFIG  171 /* AL Consumer Control Configuration */
#define KEY_HOMEPAGE  172 /* AC Home */
#define KEY_REFRESH  173 /* AC Refresh */
#define KEY_EXIT  174 /* AC Exit */
#define KEY_MOVE  175
#define KEY_EDIT  176
#define KEY_SCROLLUP  177
#define KEY_SCROLLDOWN  178
#define KEY_KPLEFTPAREN  179
#define KEY_KPRIGHTPAREN 180
#define KEY_NEW   181 /* AC New */
#define KEY_REDO  182 /* AC Redo/Repeat */

#define KEY_F13   183
#define KEY_F14   184
#define KEY_F15   185
#define KEY_F16   186
#define KEY_F17   187
#define KEY_F18   188
#define KEY_F19   189
#define KEY_F20   190
#define KEY_F21   191
#define KEY_F22   192
#define KEY_F23   193
#define KEY_F24   194

#define KEY_PLAYCD  200
#define KEY_PAUSECD  201
#define KEY_PROG3  202
#define KEY_PROG4  203
#define KEY_DASHBOARD  204 /* AL Dashboard */
#define KEY_SUSPEND  205
#define KEY_CLOSE  206 /* AC Close */
#define KEY_PLAY  207
#define KEY_FASTFORWARD  208
#define KEY_BASSBOOST  209
#define KEY_PRINT  210 /* AC Print */
#define KEY_HP   211
#define KEY_CAMERA  212
#define KEY_SOUND  213
#define KEY_QUESTION  214
#define KEY_EMAIL  215
#define KEY_CHAT  216
#define KEY_SEARCH  217
#define KEY_CONNECT  218
#define KEY_FINANCE  219 /* AL Checkbook/Finance */
#define KEY_SPORT  220
#define<

以上是关于Android : 输入设备键值映射流程的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向ART 脱壳 ( DexClassLoader 脱壳 | DexClassLoader 构造函数 | 参考 Dalvik 的 DexClassLoader 类加载流程 )(代码片段

Android 逆向整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmOptimizeDexFile() 方法分析 | /bin/dexopt 源码分析 )(代码片段

android遥控器的映射

使用 ActionBar 旋转 Android 的双片段

什么是MapReduce?

Android带扫码枪的手持终端,怎样截获扫描内容,扫描内容是通过键值上报的?