-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.cpp
125 lines (110 loc) · 2.85 KB
/
keyboard.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "keyboard.h"
#include <stdio.h>
#include <iostream>
// Keyboard info:
// idVendor 0x05d5
// idProduct 0x0689
Keyboard::Keyboard(QObject *parent) : QObject(parent)
{
this->init();
}
void Keyboard::init(){
// if(handle != NULL){
// hid_set_nonblocking(handle, 1);
// hid_close(handle); // no idea why this shit throws device to garbage
// printf("device closed\n");
// }
this->handle = hid_open(0x05d5, 0x0689, NULL);
if (!this->handle) {
printf("unable to open usb device\n");
}
memset(buf,0,sizeof(buf));
//hid_set_nonblocking(this->handle, 0);
hid_read(this->handle, buf, sizeof(buf));
}
char Keyboard::getChar(int buffer){
switch(buffer){
case 0x62:
return '0';
break;
case 0x59:
return '1';
break;
case 0x5A:
return '2';
break;
case 0x5B:
return '3';
break;
case 0x5C:
return '4';
break;
case 0x5D:
return '5';
break;
case 0x5E:
return '6';
break;
case 0x5F:
return '7';
break;
case 0x60:
return '8';
break;
case 0x61:
return '9';
break;
case 0x58:
return 'E'; // ENTER
break;
case 0x2A:
return 'D'; // DELETE
break;
case 0x63:
return 'D'; // . or DEL
break;
}
return NULL;
}
void Keyboard::manufacturer(){
wstr[0] = 0x0000;
res = hid_get_manufacturer_string(this->handle, wstr, MAX_STR);
if (res < 0)
printf("Unable to read manufacturer string\n");
printf("Manufacturer: %ls\n", wstr);
}
void Keyboard::productStrng(){
wstr[0] = 0x0000;
res = hid_get_product_string(this->handle, wstr, MAX_STR);
if (res < 0)
printf("Unable to read product string\n");
printf("Product: %ls\n", wstr);
}
QString Keyboard::getInput(){
memset(buf,0,sizeof(buf));
input_grab = "";
get_enter = false;
while(!get_enter){
res = 0;
while (res == 0) {
res = hid_read(this->handle, buf, sizeof(buf));
if (res < 0)
printf("Unable to read()\n");
}
//for (int i = 0; i < res; i++)
// printf("buf[%d]: %d\n", i, buf[i]);
what_get = this->getChar(buf[2]);
if(what_get != NULL){
if(what_get == 'E'){
get_enter = true;
}else{
if(what_get == 'D'){
input_grab.truncate(input_grab.length()-1);
}else{
input_grab += this->what_get;
}
}
}
};
return input_grab;
}