Hardware Reference
In-Depth Information
Posting EV_KEY Events
The following code snippet shows how to post a key down event, followed by a key up
event:
1 static void
2 uinput_postkey(int fd,unsigned key) {
3 struct input_event ev;
4 int rc;
5
6 memset(&ev,0,sizeof(ev));
7 ev.type = EV_KEY;
8 ev.code = key;
9 ev.value = 1;
10
11 rc = write(fd,&ev,sizeof(ev));
12 assert(rc == sizeof(ev));
13
14 ev.value = 0;
15 rc = write(fd,&ev,sizeof(ev));
16 assert(rc == sizeof(ev));
17 }
From this example, you see that each event is posted by writing a suitably initialized
input_event
structure. The example illustrates that the member named
type
was set to
EV_KEY
,
code
was set to the key code, and a keypress was indicated by setting the member
value
to 1 (line 9).
To inject a key up event,
value
is reset to 0 (line 14) and the structure is written again.
Mouse button events work the same way, except that you supply mouse button codes
for the
code
member. For example:
memset(&ev,0,sizeof(ev));
ev.type = EV_KEY;
ev.code = BTN_RIGHT; /
Right click
/
∗
∗
ev.value = 1;
Posting EV_REL Events
To post a relative mouse movement, we populate the
input_event
as a type
EV_REL
. The
member
code
is set to the type of event (
REL_X
or
REL_Y
in this example), with the value
for the relative movement established in the member
value
:
static void
uinput_movement(int fd,int x,inty) {
struct input_event ev;
int rc;