-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_emu.c
200 lines (161 loc) · 6.04 KB
/
keyboard_emu.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int singlekeypress(int fd_key_emulator, int key, int mili) {
// struct member for input events
struct input_event key_input_event;
memset(&key_input_event, 0, sizeof(input_event));
// key press event for 'a'
key_input_event.type = EV_KEY;
key_input_event.code = key;
key_input_event.value = 1;
// now write to the file descriptor
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// EV_SYN for key press event
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.value = 0;
// now write to the file descriptor
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// key release event for 'a'
key_input_event.type = EV_KEY;
key_input_event.code = key;
key_input_event.value = 0;
usleep(mili * 1000);
// now write to the file descriptor
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// EV_SYN for key press event
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.value = 0;
// now write to the file descriptor
write(fd_key_emulator, &key_input_event, sizeof(input_event));
return 0;
}
int pressandhold2(int fd_key_emulator, int key1, int key2, int mili) {
// struct member for input events
struct input_event key_input_event;
memset(&key_input_event, 0, sizeof(input_event));
// key press event for 'a'
key_input_event.type = EV_KEY;
key_input_event.code = key1;
key_input_event.value = 1;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// EV_SYN for key press event
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.value = 0;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// key press event for 'a'
key_input_event.type = EV_KEY;
key_input_event.code = key2;
key_input_event.value = 1;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// EV_SYN for key press event
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.value = 0;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
usleep(mili * 1000);
// key release event for 'a'
key_input_event.type = EV_KEY;
key_input_event.code = key1;
key_input_event.value = 0;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// EV_SYN for key press event
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.value = 0;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
// key release event for 'a'
key_input_event.type = EV_KEY;
key_input_event.code = key2;
key_input_event.value = 0;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
memset(&key_input_event, 0, sizeof(input_event));
// EV_SYN for key press event
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.value = 0;
write(fd_key_emulator, &key_input_event, sizeof(input_event));
return 0;
}
/*
*
*/
int main(int argc, char** argv) {
// create uinput file descriptor
int fd_key_emulator;
// open file descriptor
fd_key_emulator = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
// uinput_user_dev struct for fake keyboard
struct uinput_user_dev dev_fake_keyboard;
memset(&dev_fake_keyboard, 0, sizeof(uinput_user_dev));
snprintf(dev_fake_keyboard.name, UINPUT_MAX_NAME_SIZE, "kb-emulator");
dev_fake_keyboard.id.bustype = BUS_USB;
dev_fake_keyboard.id.vendor = 0x01;
dev_fake_keyboard.id.product = 0x01;
dev_fake_keyboard.id.version = 1;
/**configure the input device to send type of events, inform to subsystem which
* type of input events we are using via ioctl calls.
* UI_SET_EVBIT ioctl request is used to applied on uinput descriptor to enable a type of event.
**/
// enable key press/release event
ioctl(fd_key_emulator, UI_SET_EVBIT, EV_KEY);
// enable set of KEY events here
for (int i=0; i < 256; i++) {
ioctl(fd_key_emulator, UI_SET_KEYBIT, i);
}
// enable synchronization event
ioctl(fd_key_emulator, UI_SET_EVBIT, EV_SYN);
// now write the uinput_user_dev structure into uinput file descriptor
write(fd_key_emulator, &dev_fake_keyboard, sizeof(uinput_user_dev));
// create the device via an IOCTL call
ioctl(fd_key_emulator, UI_DEV_CREATE);
// now fd_key_emulator represents the end-point file descriptor of the new input device.
// add 1 second sleep
usleep(3000*1000);
printf("ALT+ENTER\n");
pressandhold2(fd_key_emulator, KEY_LEFTALT, KEY_ENTER, 25); usleep(5000*1000);
printf("WIN+X\n");
pressandhold2(fd_key_emulator, KEY_LEFTMETA, KEY_X, 25); usleep(1000*1000);
printf("I\n");
singlekeypress(fd_key_emulator, KEY_I, 25); usleep(1000*1000);
printf("C");
singlekeypress(fd_key_emulator, KEY_C, 25); usleep(1000*1000);
printf("A");
singlekeypress(fd_key_emulator, KEY_A, 25); usleep(1000*1000);
printf("L");
singlekeypress(fd_key_emulator, KEY_L, 25); usleep(1000*1000);
printf("C");
singlekeypress(fd_key_emulator, KEY_C, 25); usleep(1000*1000);
printf(".");
singlekeypress(fd_key_emulator, KEY_DOT, 25); usleep(1000*1000);
printf("E");
singlekeypress(fd_key_emulator, KEY_E, 25); usleep(1000*1000);
printf("X");
singlekeypress(fd_key_emulator, KEY_X, 25); usleep(1000*1000);
printf("E");
singlekeypress(fd_key_emulator, KEY_E, 25); usleep(1000*1000);
printf("\nEnter\n");
singlekeypress(fd_key_emulator, KEY_ENTER, 25); usleep(1000*1000);
return 0;
}