Click listener

Hello, I would like advice on how to modify this code, so that the resulting program counted pressing the mouse on the background. When I run the program, the screen freezes :(. Thank you for your answer.

cc mouse.c -o mouse -lX11

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
#include <stdio.h>
#include <X11/Xlib.h>

char *key_name[] = {
"first",
"second (or middle)",
"third",
"fourth",
"fivth"
};

int main(int argc, char **argv)
{
Display *display;
XEvent xevent;
Window window;
int i;
if( (display = XOpenDisplay(NULL)) == NULL )
return -1;

window = DefaultRootWindow(display);
XAllowEvents(display, AsyncBoth, CurrentTime);

XGrabPointer(display,
window,
1,
ButtonPressMask,
GrabModeAsync,
GrabModeAsync,
None,
None,
CurrentTime);

for(i = 0; i < 10; i++){
XNextEvent(display, &xevent);

switch (xevent.type) {

case ButtonPress:
printf("Button pressed : %s\n", key_name[xevent.xbutton.button - 1]);
break;
}
}

return 0;
}
Last edited on
Topic archived. No new replies allowed.