Android增加键值详解:
一.首先修改kernel/include/linux/input.h,加入自己定义的键值。
KEY_MYHALL为我加入的键值:
#define KEY_WIMAX 246
#define KEY_RFKILL 247 /* Key that controls all radios */
#define KEY_MICMUTE 248 /* Mute / unmute the microphone */
#define KEY_Find_My_Phone 249 /*justin add key for Find My Phone feature*/
#define KEY_MYHALL 250
/* Code 255 is reserved for special needs of AT keyboard driver */
二.接下来修改mediatek/custom/ckt89_we_jb2/kernel/dct/dct/cust_kpd.h
mediatek/custom/out/ckt89_we_jb2/kernel/dct/cust_kpd.h
加入如下代码:
#define KPD_INIT_KEYMAP() \
{ \
[0] = KEY_VOLUMEDOWN, \
[1] = KEY_VOLUMEUP, \
[2] = KEY_HOME, \
[3] = KEY_MENU, \
[4] = KEY_BACK, \
[5] = KEY_MYHALL, \
}
#define KEYS_PWRKEY_MAP { KEY_POWER, "Power" }
#define KEYS_MYHALL_MAP { KEY_MYHALL, "MyHall" }
#define DEFINE_KEYS_KEYMAP(x) \
struct key x[] = { \
KEYS_PWRKEY_MAP, \
{ KEY_VOLUMEDOWN, "VLDown" }, \
{ KEY_VOLUMEUP, "VLUp" }, \
{KEY_HOME, "home" }, \
{KEY_MENU, "menu" }, \
{KEY_BACK, "back" }, \
KEYS_MYHALL_MAP, \
}
#define CUST_KEY_MYHALL KEY_MYHALL
两个文件是一样的。
三.在mediatek/platform/xxx/kernel/drivers/keypad/kpd.c中input报告:
取到cust_kpd.h定义内容,并加入input报告中。
static u16 kpd_keymap[KPD_NUM_KEYS] = KPD_INIT_KEYMAP();
for (i = 0; i < KPD_NUM_KEYS; i++) {
if (kpd_keymap[i] != 0)
__set_bit(kpd_keymap[i], kpd_input_dev->keybit);
}
然后在该文件合适位置加上
input_report_key(kpd_input_dev, KEY_MYHALL, 1);
input_sync(kpd_input_dev);
udelay(10);
input_report_key(kpd_input_dev, KEY_MYHALL, 0);
input_sync(kpd_input_dev);
特别注意:因为是报告键值是虚拟,所以必须报1,0两次,如果只报1,会被认为KEY_MYHALL一直按下没有放开,导致input只上报一次键值。
选择/dev/input/eventX,不知道自己的input对应哪个event时,可以用命令cat /proc/bus/input/devices查看
测试程序(网上代码修改)如下:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <linux/input.h>
#define KEY_DEV "/dev/input/event1"
static int ts_fd = -1;
static struct input_event data;
static int init_device(char *TS_DEV)
{
if((ts_fd = open(TS_DEV, O_RDONLY)) < 0)
{
printf("Error open %s\n\n", TS_DEV);
return -1;
}
return ts_fd;
}
static int test_key()
{
if(init_device(KEY_DEV) < 0)
return -1;
while(1)
{
read(ts_fd, &data, sizeof(data));
if (data.type == EV_KEY)
printf("type: EV_KEY, event = %d, value = %d\n",data.code, data.value);
}
return 0;
}
int main()
{
test_key();
return 0;
}
关于input测试,更详细资料如下