How to add new Key to Android from Kernel to Android framework and Android application test

本文详细阐述了如何从内核到框架再到应用层面,在Android系统中添加新的按键功能,包括矩阵键盘配置、代码修改、框架集成及测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


How to add new Key to Android from Kernel to Android framework and Android application test


Kernel Layer:

1:We use Kernel matrix-keypad to register 3*3 key


#ifdef CONFIG_KEYBOARD_MATRIX
/*GPIO Matrix Keyboard*/
static const uint32_t s3c64xx_matrix_keys[] = {
KEY(0, 0, KEY_P1),
KEY(0, 1, KEY_LEFT),
KEY(0, 2, KEY_UP),

KEY(1, 0, KEY_P2),
KEY(1, 1, KEY_ENTER),
KEY(1, 2, KEY_RIGHT),

KEY(2, 0, KEY_FRONTDOOR),
KEY(2, 1, KEY_DOWN),
KEY(2, 2, KEY_START),
};

const struct matrix_keymap_data s3c64xx_keymap_data = {
.keymap = s3c64xx_matrix_keys,
.keymap_size = ARRAY_SIZE(s3c64xx_matrix_keys),
};


/*ROW KEY
GPN0,GPN1,GPN2
INPUT-->Extern Interrupt
*/

static const unsigned int s3c64xx_keypad_row_gpios[] = {
144,145,146
};

/*COL KEY:GPN3,GPN6,GPN7*/
static const unsigned int s3c64xx_keypad_col_gpios[] = {
147,150,151
};

static struct matrix_keypad_platform_data s3c64xx_keypad_platform_data = {
.keymap_data = &s3c64xx_keymap_data,
.row_gpios = s3c64xx_keypad_row_gpios,
.num_row_gpios = ARRAY_SIZE(s3c64xx_keypad_row_gpios),
.col_gpios = s3c64xx_keypad_col_gpios,
.num_col_gpios = ARRAY_SIZE(s3c64xx_keypad_col_gpios),
.active_low = 1,

.debounce_ms = 20,
.col_scan_delay_us = 5,
};


static struct platform_device s3c64xx_matrix_keyboard = {
.name = "matrix-keypad",
.id = -1,
.dev = {
.platform_data = &s3c64xx_keypad_platform_data,
},
};


static struct platform_device *smdk6410_devices[] __initdata = {
#ifdef CONFIG_KEYBOARD_MATRIX
&s3c64xx_matrix_keyboard,
#endif
};


2:We add new defition of KEY_P1,KEY_P2,KEY_FRONTDOOR,KEY_START in linux/include/linux/input.h
/*Jiujin.hong 2011/1011
#define KEY_P1 247
#define KEY_P2 248
#define KEY_FRONTDOOR 249
#define KEY_START 250
/*END*/


3:We make menuconfig to support Matrix Keyboard arch to register into Kernel input subsystem



Android Framework Layer:

1:

+++ /project/sec_android/eclair/android2.1/smdk6410/android/frameworks/base/include/ui/KeycodeLabels.h 2011-07-04 11:23:23.326802179 +0800
@@ -114,12 +114,6 @@
{ "MEDIA_REWIND", 89 },
{ "MEDIA_FAST_FORWARD", 90 },
{ "MUTE", 91 },
-//Jiujin.hong 2011/10/12,3*3 Key new Key
- { "P1",92},
- { "P2",93},
- { "FRONTDOOR",94},
- { "START",95},
-//

// NOTE: If you add a new keycode here you must also add it to:
// (enum KeyCode, in this file)
@@ -224,13 +218,7 @@
kKeyCodePreviousSong = 88,
kKeyCodeRewind = 89,
kKeyCodeForward = 90,
- kKeyCodeMute = 91,
-//Jiujin.hong 2011/1012,3*3 Key
- kKeyCodeP1 =92,
- kKeyCodeP2 =93,
- kKeyCodeFRONTDOOR=94,
- kKeyCodeSTART=95
-//End
+ kKeyCodeMute = 91
} KeyCode;



2:

--- frameworks/base/core/java/android/view/KeyEvent.java 2011-10-14 16:29:55.480750044 +0800
+++ /project/sec_android/eclair/android2.1/smdk6410/android/frameworks/base/core/java/android/view/KeyEvent.java 2011-07-04 11:23:14.279382459 +0800
@@ -120,12 +120,6 @@
public static final int KEYCODE_MEDIA_REWIND = 89;
public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
public static final int KEYCODE_MUTE = 91;
-//Jiujin.hong 2011/10/12 3*3 NEW KEY Exhibiton
- public static final int KEYCODE_P1 = 92;
- public static final int KEYCODE_P2 = 93;
- public static final int KEYCODE_FRONTDOOR = 94;
- public static final int KEYCODE_START = 95;
-//

// NOTE: If you add a new keycode here you must also add it to:
// isSystem()
@@ -141,9 +135,7 @@
// those new codes. This is intended to maintain a consistent
// set of key code definitions across all Android devices.

-// private static final int LAST_KEYCODE = KEYCODE_MUTE;
-//Jiujin.hong 2011/10/12 3*3 KEY Exhibition
- private static final int LAST_KEYCODE = KEYCODE_START;
+ private static final int LAST_KEYCODE = KEYCODE_MUTE;

/**
* @deprecated There are now more than MAX_KEYCODE keycodes.
@@ -700,12 +692,6 @@
case KEYCODE_CAMERA:
case KEYCODE_FOCUS:
case KEYCODE_SEARCH:
-//Jiujin.hong 2011/10/12 3*3 KEY new Key
- case KEYCODE_P1:
- case KEYCODE_P2:
- case KEYCODE_FRONTDOOR:
- case KEYCODE_START:
-//END
return true;
default:
return false;


3:
--- frameworks/base/core/res/res/values/attrs.xml 2011-10-14 16:29:19.448748629 +0800
+++ /project/sec_android/eclair/android2.1_6410/smdk6410/android/frameworks/base/core/res/res/values/attrs.xml 2011-07-04 11:23:16.606738864 +0800
@@ -912,12 +912,6 @@
<enum name="KEYCODE_MEDIA_REWIND" value="89" />
<enum name="KEYCODE_MEDIA_FAST_FORWARD" value="90" />
<enum name="KEYCODE_MUTE" value="91" />
- <!-- Jiujin.hong 2011/1012 3*3 Key New Key -->
- <enum name="KEYCODE_P1" value="92" />
- <enum name="KEYCODE_P2" value="93" />
- <enum name="KEYCODE_FRONTDOOR" value="94" />
- <enum name="KEYCODE_START" value="95" />
- <!-- End -->
</attr>

<!-- ***************************************************************** -->

4:


qwerty.kl (不能遗缺)

key 247 P1
key 248 P2
key 249 FRONTDOOR
key 250 START





Android Application test:

1:Android has setFocusable /request Focus for View if you use onKeyDown,so we need dispatch event

//Since we register new keycode map in frameworks/base/core/res/res/values/attrs.xml
//So we use it to judge
//KEYCODE_P1 ---92
//KEYCODE_P2 ---93
//KEYCODE_FRONTDOOR ---94
//KEYCODE_START ---95
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
System.out.println("Keycode--->"+keyCode);
switch (keyCode) {

case 92:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
System.out.println("P1 DOWN");
}
if (action == KeyEvent.ACTION_UP) {
//TODO
System.out.println("P1 UP");
}
return true;

case 93:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
System.out.println("P2 DOWN");
}
if (action == KeyEvent.ACTION_UP) {
//TODO
System.out.println("P2 UP");
}
return true;

case 94:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
System.out.println("FRONTDOOR DOWN");
}
if (action == KeyEvent.ACTION_UP) {
//TODO
System.out.println("FRONTDOOR UP");
}
return true;
case 95:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
System.out.println("START DOWN");
}
if (action == KeyEvent.ACTION_UP) {
//TODO
System.out.println("START UP");
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}

// End




adb logcat:






I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 DOWN
I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 UP


I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 DOWN
I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 UP
I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 DOWN
I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 UP
I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 DOWN
I/System.out( 1687): Keycode--->92
I/System.out( 1687): P1 UP




I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 DOWN
I/System.out( 1687): Keycode--->93
I/System.out( 1687): P2 UP




I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR DOWN
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR UP
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR DOWN
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR UP
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR DOWN
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR UP
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR DOWN
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR UP
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR DOWN
I/System.out( 1687): Keycode--->94
I/System.out( 1687): FRONTDOOR UP



I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
D/dalvikvm( 1687): GC freed 9526 objects / 375728 bytes in 66ms
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START DOWN
I/System.out( 1687): Keycode--->95
I/System.out( 1687): START UP


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值