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)



阅读更多
版权声明:版权归版主所有,转载需要声明出处 https://blog.csdn.net/fightingTony/article/details/51898096
个人分类: C/C++开发笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭