forked from zrafa/onscreenkeyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userland_keystrokes.c
83 lines (69 loc) · 1.93 KB
/
userland_keystrokes.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
/*
* Example of keystrokes sent from userland. Rafael Zurita 2014
* Useful to work with on screen keyboard
* The fb on screen keyboard would send keystrokes using uinput driver
* There is already a kernel driver, so we use it in this example
* (the kernel driver is uinput)
*
* This example sends KEY_D events to the system from a user program.
* During 20 seconds
*
* Based on http://thiemonge.org/getting-started-with-uinput
*
* Usage:
* gcc -o userland_keystrokes. userland_keystrokes.c
* modprobe uinput
* ./userland_keystrokes
*/
#define UINPUT_MAX_NAME_SIZE 80
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/uinput.h>
int main(int argc, char *argv[]) {
int fd;
/* Set up a fake keyboard device */
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); // or /dev/input/uinput
ioctl(fd, UI_SET_EVBIT, EV_KEY);
/* Send an event */
int ret;
ret = ioctl(fd, UI_SET_EVBIT, EV_KEY);
ret = ioctl(fd, UI_SET_EVBIT, EV_SYN);
ret = ioctl(fd, UI_SET_KEYBIT, KEY_D);
struct uinput_user_dev {
char name[UINPUT_MAX_NAME_SIZE];
struct input_id id;
int ff_effects_max;
int absmax[ABS_MAX + 1];
int absmin[ABS_MAX + 1];
int absfuzz[ABS_MAX + 1];
int absflat[ABS_MAX + 1];
};
struct uinput_user_dev uidev;
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x1234;
uidev.id.product = 0xfedc;
uidev.id.version = 1;
ret = write(fd, &uidev, sizeof(uidev));
ret = ioctl(fd, UI_DEV_CREATE);
struct input_event ev;
memset(&ev, 0, sizeof(ev));
int i;
for (i=0;i<20;i++) {
ev.type = EV_KEY;
ev.code = KEY_D;
ev.value = 1;
ret = write(fd, &ev, sizeof(ev));
sleep(1);
ev.value = 0;
ret = write(fd, &ev, sizeof(ev));
}
/* Clean up */
ioctl(fd, UI_DEV_DESTROY);
close(fd);
}