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測試,更詳細資料如下
http://www.linuxidc.com/Linux/2011-02/32079.htm