1. 确认按键驱动是否有值
adb shell getevent
/dev/input/event2: 0001 008b 00000001
/dev/input/event2: 0000 0000 00000000
/dev/input/event2: 0001 008b 00000000
/dev/input/event2: 0000 0000 00000000
其中008b就是驱动上报的值,两次的1,0是指按下和弹起的动作
2. 转换驱动传的值为10进制,并映射
frameworks/base/data/keyboards/Generic.kl
key 137 CUT
# key 138 "KEY_HELP"
key 139 MENU
key 140 CALCULATOR
# key 141 "KEY_SETUP"
008b转换成10进制就是139,映射的就是MENU
3. framework native 中定义键值
frameworks/native/include/android/keycodes.h
AKEYCODE_FOCUS = 80,
/** '+' key. */
AKEYCODE_PLUS = 81,
/** Menu key. */
AKEYCODE_MENU = 142,// Menu key call out the multi-task function
/** Notification key. */
AKEYCODE_NOTIFICATION = 83,
!!!这里定义的值才是app中获取的键值
frameworks/native/include/input/InputEventLabels.h
static const InputEventLabel KEYCODES[] = {
// NOTE: If you add a new keycode here you must also add it to several other files.
// Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
DEFINE_KEYCODE(UNKNOWN),
DEFINE_KEYCODE(SOFT_LEFT),
......
DEFINE_KEYCODE(PROFILE_SWITCH),
DEFINE_KEYCODE(MENU),// Menu key call out the multi-task function
{ nullptr, 0 }
};
如果是添加新键值,还得在这里声明。这里是MENU用的F12的键值,两者都不是新增
4. framework base 中使用键值
frameworks/base/core/java/android/view/KeyEvent.java
/** Key code constant: F11 key. */
public static final int KEYCODE_F11 = 141;
/** Key code constant: F12 key. */
public static final int KEYCODE_F12 = 142;// Menu key call out the multi-task function
/** Key code constant: Num Lock key.
* This is the Num Lock key; it is different from {@link #KEYCODE_NUM}.
* This key alters the behavior of other keys on the numeric keypad. */
public static final int KEYCODE_NUM_LOCK = 143;
frameworks/base/core/res/res/values/attrs.xml
<enum name="KEYCODE_F11" value="141" />
<enum name="KEYCODE_F12" value="142" />
<enum name="KEYCODE_NUM_LOCK" value="143" />
<enum name="KEYCODE_NUMPAD_0" value="144" />
根据native定义好的值,声明一下。然后在app中就可以识别到声明的按键
5. 更新 api
make update-api
//如果是新增按键,需要在编译之前执行