Android键盘输出增加按键码
jopen
13年前
<span style="font-family:'Microsoft YaHei';font-size:16px;">开发过程中免不得需要增加或减少按键码值,在android2.3系统事件处理部分,主要向上层提供统一的按键码(KeyCode),这个按键码是一个整数,在上层的JAVA程序中主要通过这个值来判断系统的实现。</span> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 这里主要讲android事件的处理过程,需要经过两个转换步骤:</span><span style="font-family:'Microsoft YaHei';white-space:pre;font-size:16px;"> </span></p> <p><span style="white-space:pre;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><img alt="Android键盘输出增加按键码 " src="https://simg.open-open.com/show/340a604a2895dded20950aa01cf2caee.bmp" width="443" height="382" /></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 1、键扫描码ScanCode是由linux的Input驱动框架定义的整数类型,可参考input.h头文件 </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 在步骤一中将其值转换成表示按键的字符串</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 键盘布局文件(*.kl)将完成第一步的转换,放在/system/usr/keylayout/下面</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 举例如下:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> input.h 中字母键定义:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> #define KEY_Q 16<br /> #define KEY_W 17<br /> #define KEY_E 18<br /> #define KEY_R 19<br /> #define KEY_T 20<br /> #define KEY_Y 21<br /> #define KEY_U 22<br /> #define KEY_I 23<br /> #define KEY_O 24<br /> #define KEY_P 25 </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 而qwert.kl中定义如下:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> ScanCode + 字符串值 </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> key 16 Q<br /> key 17 W<br /> key 18 E<br /> key 19 R<br /> key 20 T<br /> key 21 Y<br /> key 22 U<br /> key 23 I<br /> key 24 O<br /> key 25 P </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 2、通过查找KEYCODES数组,将literal字符串转换成value的整型值,即转换到KeyCode值了</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 片段如下,右边的值是android系统中使用的KeyCode值:(文件路径:frameworks/base/include/ui/KeycodeLabels.h)</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;">static const KeycodeLabel KEYCODES[] = {<br /> </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> ...</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> { "A", 29 },<br /> { "B", 30 },<br /> { "C", 31 },<br /> { "D", 32 },<br /> { "E", 33 },<br /> { "F", 34 },<br /> { "G", 35 },<br /> { "H", 36 },<br /> { "I", 37 },<br /> { "J", 38 },<br /> { "K", 39 },<br /> { "L", 40 },<br /> { "M", 41 },<br /> { "N", 42 },<br /> { "O", 43 },<br /> { "P", 44 },<br /> { "Q", 45 },<br /> { "R", 46 },<br /> { "S", 47 },<br /> { "T", 48 },<br /> { "U", 49 },<br /> { "V", 50 },<br /> { "W", 51 },<br /> { "X", 52 },<br /> { "Y", 53 },<br /> { "Z", 54 },<br /> </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> ...</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;">};</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 右边的值在android中定义如下:(文件路径:<span style="font-family:'Microsoft YaHei';font-size:16px;">frameworks/base/include/android/keycodes.h</span>)</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> /*<br /> * Key codes.<br /> */<br /> enum {</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> AKEYCODE_A = 29,<br /> AKEYCODE_B = 30,<br /> AKEYCODE_C = 31,<br /> AKEYCODE_D = 32,<br /> AKEYCODE_E = 33,<br /> AKEYCODE_F = 34,<br /> AKEYCODE_G = 35,<br /> AKEYCODE_H = 36,<br /> AKEYCODE_I = 37,<br /> AKEYCODE_J = 38,<br /> AKEYCODE_K = 39,<br /> AKEYCODE_L = 40,<br /> AKEYCODE_M = 41,<br /> AKEYCODE_N = 42,<br /> AKEYCODE_O = 43,<br /> AKEYCODE_P = 44,<br /> AKEYCODE_Q = 45,<br /> AKEYCODE_R = 46,<br /> AKEYCODE_S = 47,<br /> AKEYCODE_T = 48,<br /> AKEYCODE_U = 49,<br /> AKEYCODE_V = 50,<br /> AKEYCODE_W = 51,<br /> AKEYCODE_X = 52,<br /> AKEYCODE_Y = 53,<br /> AKEYCODE_Z = 54,</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> ..</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;">};</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> 这里的键值与KeyEvent.java中的值是对应的:(文件路径:frameworks/base/core/java/android/view/KeyEvent.java)</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> /** Key code constant: 'A' key. */<br /> public static final int KEYCODE_A = 29;<br /> /** Key code constant: 'B' key. */<br /> public static final int KEYCODE_B = 30;<br /> /** Key code constant: 'C' key. */<br /> public static final int KEYCODE_C = 31;<br /> /** Key code constant: 'D' key. */<br /> public static final int KEYCODE_D = 32;<br /> /** Key code constant: 'E' key. */<br /> public static final int KEYCODE_E = 33;<br /> /** Key code constant: 'F' key. */<br /> public static final int KEYCODE_F = 34;<br /> /** Key code constant: 'G' key. */<br /> public static final int KEYCODE_G = 35;<br /> /** Key code constant: 'H' key. */<br /> public static final int KEYCODE_H = 36;<br /> /** Key code constant: 'I' key. */<br /> public static final int KEYCODE_I = 37;<br /> /** Key code constant: 'J' key. */<br /> public static final int KEYCODE_J = 38;<br /> /** Key code constant: 'K' key. */<br /> public static final int KEYCODE_K = 39;<br /> /** Key code constant: 'L' key. */<br /> public static final int KEYCODE_L = 40;<br /> /** Key code constant: 'M' key. */<br /> public static final int KEYCODE_M = 41;<br /> /** Key code constant: 'N' key. */<br /> public static final int KEYCODE_N = 42;<br /> /** Key code constant: 'O' key. */<br /> public static final int KEYCODE_O = 43;<br /> /** Key code constant: 'P' key. */<br /> public static final int KEYCODE_P = 44;<br /> /** Key code constant: 'Q' key. */<br /> public static final int KEYCODE_Q = 45;<br /> /** Key code constant: 'R' key. */<br /> public static final int KEYCODE_R = 46;<br /> /** Key code constant: 'S' key. */<br /> public static final int KEYCODE_S = 47;<br /> /** Key code constant: 'T' key. */<br /> public static final int KEYCODE_T = 48;<br /> /** Key code constant: 'U' key. */<br /> public static final int KEYCODE_U = 49;<br /> /** Key code constant: 'V' key. */<br /> public static final int KEYCODE_V = 50;<br /> /** Key code constant: 'W' key. */<br /> public static final int KEYCODE_W = 51;<br /> /** Key code constant: 'X' key. */<br /> public static final int KEYCODE_X = 52;<br /> /** Key code constant: 'Y' key. */<br /> public static final int KEYCODE_Y = 53;<br /> /** Key code constant: 'Z' key. */<br /> public static final int KEYCODE_Z = 54;</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> ...</span></p> <p> <span style="font-family:Microsoft YaHei;font-size:16px;"> 如果改动了这里,影响到API则需要调用make update-api<br /> </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;">ok,理清楚了以上的转换关系,下面就说一下如何增加按键,以增加gamekey为例:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;">1 、键盘布局文件中增加键,一般是qwery.kl (一定是以*.kl结尾的文件):</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> key 304 BUTTON_A </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> <span style="font-family:'Microsoft YaHei';font-size:16px;">key 305 BUTTON_B</span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <span style="font-family:'Microsoft YaHei';font-size:16px;">key 306 BUTTON_C</span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> key 307 BUTTON_X</span><br /> </span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <span style="font-family:'Microsoft YaHei';font-size:16px;">key 308 BUTTON_Y</span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <span style="font-family:'Microsoft YaHei';font-size:16px;">key 309 BUTTON_Z</span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> key 315 BUTTON_START</span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> key 316 BUTTON_MODE</span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><br /> </span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;">2、在<span style="font-family:'Microsoft YaHei';font-size:16px;">frameworks/base/include/ui/KeycodeLabels.h中增加KeycodeLabel类型的Code数组</span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> { "BUTTON_A", 96 },<br /> { "BUTTON_B", 97 },<br /> { "BUTTON_C", 98 },<br /> { "BUTTON_X", 99 },<br /> { "BUTTON_Y", 100 },<br /> { "BUTTON_Z", 101 },</span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> { "BUTTON_START", 108 },</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> { "BUTTON_MODE", 110 }, </span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> 目前的2.3系统已经添加</span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><br /> </span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> 3、在<span style="font-family:'Microsoft YaHei';font-size:16px;">frameworks/base/include/android/keycodes.h中增加<span style="font-family:'Microsoft YaHei';font-size:16px;">KeyCode的枚举值</span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> AKEYCODE_BUTTON_A = 96,<br /> AKEYCODE_BUTTON_B = 97,<br /> AKEYCODE_BUTTON_C = 98,<br /> AKEYCODE_BUTTON_X = 99,<br /> AKEYCODE_BUTTON_Y = 100,<br /> AKEYCODE_BUTTON_Z = 101,</span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> AKEYCODE_BUTTON_START = 108,<br /> AKEYCODE_BUTTON_MODE = 110, </span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <span style="font-family:'Microsoft YaHei';font-size:16px;"> 目前的2.3系统已经添加</span></span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><br /> </span></span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;">4、JAVA层的keyEvent.java中增加用于java应用程序使用</span></span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> 略,<span style="font-family:'Microsoft YaHei';font-size:16px;">目前的2.3系统已经添加</span></span></span></span></span></span></span></span></span></span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><br /> </span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;">5、在frameworks\base\core\res\res\values\attrs.xml中增加表示属性的资源文件,添加相应用name="keycode"的attr</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="96" name="KEYCODE_BUTTON_A"></enum></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="97" name="KEYCODE_BUTTON_B"></enum></span><br /> </span></p> <p></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;font-size:16px;padding-top:2px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="98" name="KEYCODE_BUTTON_C"></enum></span><br /> </span></p> <div style="font-size:16px;"> <enum value="99" name="KEYCODE_BUTTON_X"> </enum> </div> <div style="font-size:16px;"> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="100" name="KEYCODE_BUTTON_Y"></enum></span><br /> </span></p> <div> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="101" name="KEYCODE_BUTTON_Z"></enum></span><br /> </span></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="108" name="KEYCODE_BUTTON_START"></enum></span></span></span></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"> <enum value="110" name="KEYCODE_BUTTON_MODE"></enum></span><br /> </span></span></span></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><span style="font-family:'Microsoft YaHei';font-size:16px;"><br /> </span></span></span></span></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">ok,完成以上步骤应该就可以使用了。</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">查找key是否有没有被过滤掉重点确认的地方:</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">1、EventHub.cpp 文件中的getEvent函数</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"> if (iev.type == EV_KEY) {<br /> status_t err = device->layoutMap->map(iev.code,<br /> & outEvent->keyCode, & outEvent->flags);<br /> LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",<br /> iev.code, outEvent->keyCode, outEvent->flags, err);<br /> if (err != 0) {<br /> outEvent->keyCode = AKEYCODE_UNKNOWN;<br /> outEvent->flags = 0;<br /> }</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"> }</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"> 确认这里转换是否ok了,如果没有则可能*.kl文件中没有添加进去这个键值 </p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">2、InputReader.cpp文件中的KeyboardInputMapper处理函数process</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"> void KeyboardInputMapper::process(const RawEvent* rawEvent) {<br /> switch (rawEvent->type) {<br /> case EV_KEY: {<br /> int32_t scanCode = rawEvent->scanCode;<br /> if (isKeyboardOrGamepadKey(scanCode)) {<br /> processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,<br /> rawEvent->flags);<br /> }<br /> break;<br /> }<br /> }<br /> }<br /> <br /> bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {<br /> return scanCode < BTN_MOUSE<br /> || scanCode >= KEY_OK<br /> || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);<br /> }</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">可以在processKey中加打印,确认已通过notifyKey上报键值给android系统了。</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;"> </p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">附加,如何编译生成qwerty.kcm.bin 文件:</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">android为了减少载入时间,并没有使用原始按键表文件,而是将其转换成二进制文件<br /> 转换的工具源代码在android源代码build/tools/kcm目录下,android在编译过程中会<br /> 首先编译转换工具,然后利用转换工具将qwerty.kcm文件转换成qwerty.kcm.bin</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">转换后的二进制文件复制到out/target/product/ <board_name> /system/usr/keychars <br /> 目录下,也就是目标平台的/system/usr/keychars目录中。 </board_name></p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">用法:</p> <p style="padding-bottom:2px;margin:4px 0px;padding-left:0px;padding-right:0px;padding-top:2px;">static int usage()<br /> {<br /> fprintf(stderr,<br /> "usage: kcm INPUT OUTPUT\n"<br /> "\n"<br /> "INPUT keycharmap file\n"<br /> "OUTPUT compiled keycharmap file\n"<br /> );<br /> return 1;<br /> }</p> </div> </div>