linux应用读取input按键驱动上报键值案例代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <termios.h>
#include <errno.h>
#include <linux/input.h>
#include "log.h"
#define TOUCHKEY_TAG "TouchkeyDemo "
#define TOUCHKEY_FILE"/dev/input/event4"
int main(int argc, char **argv)
{
int nFd;
int nRet;
struct input_event *sInputEvent;
PL_LOGI(TOUCHKEY_TAG, " --> Make time: %s %s %s <-- ", __DATE__, __TIME__, "Tony.Huang");
sInputEvent = malloc(sizeof(struct input_event));
nFd = open(TOUCHKEY_FILE, O_RDWR);
if(nFd < 0)
{
PL_LOGE(TOUCHKEY_TAG, "open file %s error.", TOUCHKEY_FILE);
return -1;
}
else
{
PL_LOGI(TOUCHKEY_TAG, "open file %s success.", TOUCHKEY_FILE);
}
memset(sInputEvent, 0, sizeof(struct input_event));
nRet = read(nFd, sInputEvent, sizeof(struct input_event));
if(nRet < 0)
{
PL_LOGE(TOUCHKEY_TAG, "read file %s error.", TOUCHKEY_FILE);
return nRet;
}
PL_LOGI(TOUCHKEY_TAG, "type: 0x%x ", sInputEvent->type);
PL_LOGI(TOUCHKEY_TAG, "code: 0x%x ", sInputEvent->code);
PL_LOGI(TOUCHKEY_TAG, "value: 0x%x", sInputEvent->value);
free(sInputEvent);
close(nFd);
return 0;
}
Makefile编译文件:
CUR_TARGET=touchkeydemo
INCLUDES += -I./
CUR_SRCS_THREAD:= touchkeydemo.c
touchkeydemo:
$(CC) $(CUR_SRCS_THREAD) -o $(CUR_TARGET) -lpthread -ldl -lm
clean:
rm -fv $(CUR_TARGET)