include iostream include fstream include linux input include fcntl inc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <linux/input.h>
#include <fcntl.h>
#include "scancodes.cpp"
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
int main()
{
map<int,string> keys;
map<int,int> pressed;
keys = get_keys();
int fd;
if ((fd = open("/dev/input/event1", O_RDONLY)) < 0)
{
perror("evdev open");
return 1;
}
struct input_event ev1,ev2,ev3;
while(1)
{
read(fd, &ev1, sizeof(struct input_event));
read(fd, &ev2, sizeof(struct input_event));
read(fd, &ev3, sizeof(struct input_event));
//printf("value %d, type %d, code %d %s\n",ev1.value,ev1.type,
//ev1.code, keys[ev1.code].c_str());
int holding = time(NULL);
if (ev2.value==1)
{
//cout << time(NULL) << endl;
pressed[ev1.value]=1;
holding = time(NULL);
//cout << "pressed " << keys[ev1.value] << endl;// << endl;
cout << keys[ev1.value];
cin.sync();
}
else if (ev2.value==0)
{
//cout << "released " << keys[ev1.value] << endl;
//cout << holding << " " << time(NULL) << endl;
//int t = (int)time(NULL) - (int)holding;
pressed[ev1.value]=0;
//printf("holded %d, %d\n",holding, time(NULL));
}
/*if (ev.type==1)
{
if (!pressed[ev.code])
{
pressed[ev.code]=1;
cout << keys[ev.code];
cin.sync();
}
}
else if(ev.type==0)
{
pressed[ev.code]=0;
}*/
/*cout << keys[ev.code].c_str();
cin.sync();*/
//printf("%s",keys[ev.code].c_str());
/*int a,b,c;
a=ev.value;
b=ev.type;
c=ev.code;
printf("%d %d %d %s", a, b, c, keys[c].c_str());*/
}
}