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

知识的价值不在于占有,而在于使用。这篇文章主要讲述Android : 输入设备键值映射流程相关的知识,希望能为你提供帮助。
一、Android输入子系统简介:
android输入事件的源头是位于/dev/input/下的设备节点,而输入系统的终点是由WMS管理的某个窗口。最初的输入事件为内核生成的原始事件,而最终交付给窗口的则是KeyEvent或MotionEvent对象。因此Android输入系统的主要工作是读取设备节点中的原始事件,将其加工封装,然后派发给一个特定的窗口以及窗口中的控件。这个过程由InputManagerService(以下简称IMS)系统服务为核心的多个参与者共同完成。

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

文章图片

图1:输入系统的总体流程与参与者
 
【Android : 输入设备键值映射流程】二、键值映射关系:
    ①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_RESERVED0 #define KEY_ESC1 #define KEY_12 #define KEY_23 #define KEY_34 #define KEY_45 #define KEY_56 #define KEY_67 #define KEY_78 #define KEY_89 #define KEY_910 #define KEY_011 #define KEY_MINUS12 #define KEY_EQUAL13 #define KEY_BACKSPACE14 #define KEY_TAB15 #define KEY_Q16 #define KEY_W17 #define KEY_E18 #define KEY_R19 #define KEY_T20 #define KEY_Y21 #define KEY_U22 #define KEY_I23 #define KEY_O24 #define KEY_P25 #define KEY_LEFTBRACE26 #define KEY_RIGHTBRACE27 #define KEY_ENTER28 #define KEY_LEFTCTRL29 #define KEY_A30 #define KEY_S31 #define KEY_D32 #define KEY_F33 #define KEY_G34 #define KEY_H35 #define KEY_J36 #define KEY_K37 #define KEY_L38 #define KEY_SEMICOLON39 #define KEY_APOSTROPHE40 #define KEY_GRAVE41 #define KEY_LEFTSHIFT42 #define KEY_BACKSLASH43 #define KEY_Z44 #define KEY_X45 #define KEY_C46 #define KEY_V47 #define KEY_B48 #define KEY_N49 #define KEY_M50 #define KEY_COMMA51 #define KEY_DOT52 #define KEY_SLASH53 #define KEY_RIGHTSHIFT54 #define KEY_KPASTERISK55 #define KEY_LEFTALT56 #define KEY_SPACE57 #define KEY_CAPSLOCK58 #define KEY_F159 #define KEY_F260 #define KEY_F361 #define KEY_F462 #define KEY_F563 #define KEY_F664 #define KEY_F765 #define KEY_F866 #define KEY_F967 #define KEY_F1068 #define KEY_NUMLOCK69 #define KEY_SCROLLLOCK70 #define KEY_KP771 #define KEY_KP872 #define KEY_KP973 #define KEY_KPMINUS74 #define KEY_KP475 #define KEY_KP576 #define KEY_KP677 #define KEY_KPPLUS78 #define KEY_KP179 #define KEY_KP280 #define KEY_KP381 #define KEY_KP082 #define KEY_KPDOT83#define KEY_ZENKAKUHANKAKU 85 #define KEY_102ND86 #define KEY_F1187 #define KEY_F1288 #define KEY_RO89 #define KEY_KATAKANA90 #define KEY_HIRAGANA91 #define KEY_HENKAN92 #define KEY_KATAKANAHIRAGANA 93 #define KEY_MUHENKAN94 #define KEY_KPJPCOMMA95 #define KEY_KPENTER96 #define KEY_RIGHTCTRL97 #define KEY_KPSLASH98 #define KEY_SYSRQ99 #define KEY_RIGHTALT100 #define KEY_LINEFEED101 #define KEY_HOME102 #define KEY_UP103 #define KEY_PAGEUP104 #define KEY_LEFT105 #define KEY_RIGHT106 #define KEY_END107 #define KEY_DOWN108 #define KEY_PAGEDOWN109 #define KEY_INSERT110 #define KEY_DELETE111 #define KEY_MACRO112 #define KEY_MUTE113 #define KEY_VOLUMEDOWN114 #define KEY_VOLUMEUP115 #define KEY_POWER116 /* SC System Power Down */ #define KEY_KPEQUAL117 #define KEY_KPPLUSMINUS118 #define KEY_PAUSE119 #define KEY_SCALE120 /* AL Compiz Scale (Expose) */#define KEY_KPCOMMA121 #define KEY_HANGEUL122 #define KEY_HANGUELKEY_HANGEUL #define KEY_HANJA123 #define KEY_YEN124 #define KEY_LEFTMETA125 #define KEY_RIGHTMETA126 #define KEY_COMPOSE127#define KEY_STOP128 /* AC Stop */ #define KEY_AGAIN129 #define KEY_PROPS130 /* AC Properties */ #define KEY_UNDO131 /* AC Undo */ #define KEY_FRONT132 #define KEY_COPY133 /* AC Copy */ #define KEY_OPEN134 /* AC Open */ #define KEY_PASTE135 /* AC Paste */ #define KEY_FIND136 /* AC Search */ #define KEY_CUT137 /* AC Cut */ #define KEY_HELP138 /* AL Integrated Help Center */ #define KEY_MENU139 /* Menu (show menu) */ #define KEY_CALC140 /* AL Calculator */ #define KEY_SETUP141 #define KEY_SLEEP142 /* SC System Sleep */ #define KEY_WAKEUP143 /* System Wake Up */ #define KEY_FILE144 /* AL Local Machine Browser */ #define KEY_SENDFILE145 #define KEY_DELETEFILE146 #define KEY_XFER147 #define KEY_PROG1148 #define KEY_PROG2149 #define KEY_WWW150 /* AL Internet Browser */ #define KEY_MSDOS151 #define KEY_COFFEE152 /* AL Terminal Lock/Screensaver */ #define KEY_SCREENLOCKKEY_COFFEE #define KEY_DIRECTION153 #define KEY_CYCLEWINDOWS 154 #define KEY_MAIL155 #define KEY_BOOKMARKS156 /* AC Bookmarks */ #define KEY_COMPUTER157 #define KEY_BACK158 /* AC Back */ #define KEY_FORWARD159 /* AC Forward */ #define KEY_CLOSECD160 #define KEY_EJECTCD161 #define KEY_EJECTCLOSECD 162 #define KEY_NEXTSONG163 #define KEY_PLAYPAUSE164 #define KEY_PREVIOUSSONG 165 #define KEY_STOPCD166 #define KEY_RECORD167 #define KEY_REWIND168 #define KEY_PHONE169 /* Media Select Telephone */ #define KEY_ISO170 #define KEY_CONFIG171 /* AL Consumer Control Configuration */ #define KEY_HOMEPAGE172 /* AC Home */ #define KEY_REFRESH173 /* AC Refresh */ #define KEY_EXIT174 /* AC Exit */ #define KEY_MOVE175 #define KEY_EDIT176 #define KEY_SCROLLUP177 #define KEY_SCROLLDOWN178 #define KEY_KPLEFTPAREN179 #define KEY_KPRIGHTPAREN 180 #define KEY_NEW181 /* AC New */ #define KEY_REDO182 /* AC Redo/Repeat */#define KEY_F13183 #define KEY_F14184 #define KEY_F15185 #define KEY_F16186 #define KEY_F17187 #define KEY_F18188 #define KEY_F19189 #define KEY_F20190 #define KEY_F21191 #define KEY_F22192 #define KEY_F23193 #define KEY_F24194#define KEY_PLAYCD200 #define KEY_PAUSECD201 #define KEY_PROG3202 #define KEY_PROG4203 #define KEY_DASHBOARD204 /* AL Dashboard */ #define KEY_SUSPEND205 #define KEY_CLOSE206 /* AC Close */ #define KEY_PLAY207 #define KEY_FASTFORWARD208 #define KEY_BASSBOOST209 #define KEY_PRINT210 /* AC Print */ #define KEY_HP211 #define KEY_CAMERA212 #define KEY_SOUND213 #define KEY_QUESTION214 #define KEY_EMAIL215 #define KEY_CHAT216 #define KEY_SEARCH217 #define KEY_CONNECT218 #define KEY_FINANCE219 /* AL Checkbook/Finance */ #define KEY_SPORT220 #define<

    推荐阅读