Android 增加按键

来源:互联网 发布:js大于0的整数 编辑:程序博客网 时间:2024/05/18 19:40

转自:http://www.2cto.com/kf/201312/265650.html

http://www.linuxidc.com/Linux/2012-01/50692.htm

http://www.educity.cn/wenda/173573.html

Android 增加按键:

ScanCode -> KeyCodeLabel -> KeyCode -> Keyevent

-----

1) 键扫描码ScanCode是由linux的Input驱动框架定义的整数类型,可参考input.h头文件(./external/kernel-headers/original/linux/input.h)。

2) 按键码(KeyCode),这个按键码是一个整数,在上层的JAVA程序中主要通过这个值来判断系统的实现。

----

实现过程:

1, ScanCode -> KeyCodeLabel。

键盘布局文件(*.kl) 把ScanCode转换为KeyCodeLabel。 .kl文件在源码的devices文件夹中或设备中的/system/usr/keylayout/.kl。

key 102 HOME

2,KeyCodeLabel -> KeyCode。

通过查找KEYCODES[]数组,得到KeyCodeLabel字符串对应的KeyCode值。KEYCODES[]在文件frameworks/base/include/ui/KeycodeLabels.h 中。

static const KeycodeLabel KEYCODES[] = {
...
{ "HOME", 3 },

在frameworks/base/native/include/android/keycodes.h中定义KeyCode的枚举值。

enum {
....
AKEYCODE_HOME = 3,

3,KeyCode -> Keyevent。

KeyEvent.java中的对应KeyCode:(文件路径:frameworks/base/core/java/android/view/KeyEvent.java)。如果改动了KeyEvent,影响到API则需要调用make update-api。

public static final int KEYCODE_HOME = 3;
....
names.append(KEYCODE_HOME, "KEYCODE_HOME");
....

4,在frameworks\base\core\res\res\values\attrs.xml中增加表示属性的资源文件,添加相应用name="keycode"的attr。

keyevent.java中的注释

// NOTE: If you add a new keycode here you must also add it to:

// isSystem()

// native/include/android/keycodes.h

// frameworks/base/include/ui/KeycodeLabels.h

// external/webkit/WebKit/android/plugins/ANPKeyCodes.h

// tools/puppet_master/PuppetMaster/nav_keys.py

// frameworks/base/core/res/res/values/attrs.xml

// commands/monkey/Monkey.java

// emulator?

//

// Also Android currently does not reserve code ranges for vendor-

// specific key codes. If you have new key codes to have, you

// MUST contribute a patch to the open source project to define

// those new codes. This is intended to maintain a consistent

// set of key code definitions across all Android devices.


0 0
原创粉丝点击