forked from Vantskruv/wejoy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CKeyboard.cc
64 lines (53 loc) · 1.2 KB
/
CKeyboard.cc
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
#include "CKeyboard.h"
//#include <libudev.h>
//#include <stdio.h>
#include <string.h>
#include <unistd.h> //usleep
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
CKeyboard::CKeyboard(std::string _eventPath)
{
eventPath = _eventPath;
fd = open(eventPath.c_str(), O_RDONLY);
if(fd == -1)
{
fprintf(stderr, "KEYBOARD: Cannot open %s: %s.\n", eventPath.c_str(), strerror(errno));
}
}
CKeyboard::~CKeyboard()
{
if(fd>-1) close(fd);
}
std::string CKeyboard::getEventPath()
{
return eventPath;
}
bool CKeyboard::isOpen()
{
if(fd>-1) return true;
return false;
}
bool CKeyboard::readEvent(CKeyboardEvent* _keyEvent)
{
/*
static const char *const evval[3] = {
"RELEASED",
"PRESSED ",
"REPEATED"
};
*/
if(fd<0) return false;
struct input_event ev;
ssize_t n;
n = read(fd, &ev, sizeof ev);
if(n == ((ssize_t) - 1) || n!= sizeof(ev)) return false;
if(ev.type == EV_KEY && ev.value>=0 && ev.value<=2 && ev.value!=2)
{
_keyEvent->isPressed = ev.value;
_keyEvent->code = ev.code;
//printf("%s 0x%04x (%d) %d\n", evval[ev.value], (int)ev.code, (int)ev.code, (ev.code == KEY_Q ? 1 : 0));
return true;
}
return false;
}