#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
void handler (int sig)
{
printf ("nexiting...(%d)n", sig);
exit (0);
}
void perror_exit (char *error)
{
perror (error);
handler (9);
}
int main (int argc, char *argv[])
{
struct input_event ev[64];
int fd, rd, value, size = sizeof (struct input_event);
char name[256] = "Unknown";
char *device = NULL;
//Setup check
if (argv[1] == NULL){
printf("Please specify (on the command line) the path to the dev event interface device\n");
exit (0);
}
if ((getuid ()) != 0)
printf ("You are not root! This may not work...\n");
if (argc > 1)
device = argv[1];
//Open Device
if ((fd = open (device, O_RDONLY)) == -1)
printf ("%s is not a vaild device.\n", device);
//Print Device Name
ioctl (fd, EVIOCGNAME (sizeof (name)), name);
printf ("Reading From : %s (%s)\n", device, name);
while (1){
if ((rd = read (fd, ev, size * 64)) < size)
perror_exit ("read()\n");
value = ev[0].value;
if (value != ' ' && ev[1].value == 1 && ev[1].type == 1){ // Only read the key press event
printf ("Code[%d]\n", (ev[1].code));
}
}
return 0;
}
yantai:/home/shell.albert # ./a.out /dev/input/event0
Reading From : /dev/input/event0 (AT Translated Set 2 keyboard)Code[30]
aCode[48]
bCode[46]
cdCode[28]
Code[18]
eCode[33]
fCode[34]
gCode[35]
hCode[23]
iCode[36]
jCode[37]
kCode[38]
lCode[50]
mCode[49]
nCode[24]
oCode[25]
pCode[16]
qCode[19]
rCode[31]
sCode[20]
tCode[22]
uCode[47]
vCode[17]
wCode[44]
zCode[45]
xCode[21]
yCode[44]
zCode[29]
Code[46]
^C
yantai:/home/shell.albert #
by zhangshaoyan at May 20,2015.
We get the codes are same as defined in linux/input.h.