#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* This program prints the absolute coordinates of the
* mouse if you press a mouse button.
*
* This works only if you click IN THE WINDOW. I want to
* have it independent of the window. That is, if there
* is anywhere (other windows) a button press, I want the
* mouse coordinates, too.
*
* Any hint?
*/
int main() {
Display *display;
int screen;
Window window;
XEvent event;
Window QueryRoot, QueryChild;
int AbsoluteX, AbsoluteY;
int RelativeX, RelativeY;
unsigned int ModKeyMask;
/* open connection with the server */
display = XOpenDisplay(NULL);
if (display == NULL) {
printf("Cannot open display\n");
exit(1);
}
screen = DefaultScreen(display);
/* create window */
window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 200, 200, 1,
BlackPixel(display, screen), WhitePixel(display, screen));
/* select kind of events we are interested in */
XSelectInput(display, window, ExposureMask | ButtonPressMask);
/*XSelectInput(display, RootWindow(display, screen), ExposureMask | ButtonPressMask);*/
/* map (show) the window */
XMapWindow(display, window);
/* event loop */
while (1) {
XNextEvent(display, &event);
if (event.type == ButtonPress) {
printf("Button was pressed!\n");
XQueryPointer(display, window, &QueryRoot, &QueryChild, &AbsoluteX,
&AbsoluteY, &RelativeX, &RelativeY, &ModKeyMask);
printf("AbsoluteX: %d\n", AbsoluteX);
printf("AbsoluteY: %d\n", AbsoluteY);
}
}
/* close connection to server */
XCloseDisplay(display);
return 0;
}
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* This program prints the absolute coordinates of the
* mouse if you press a mouse button.
*
* This works only if you click IN THE WINDOW. I want to
* have it independent of the window. That is, if there
* is anywhere (other windows) a button press, I want the
* mouse coordinates, too.
*
* Any hint?
*/
int main() {
Display *display;
int screen;
Window window;
XEvent event;
Window QueryRoot, QueryChild;
int AbsoluteX, AbsoluteY;
int RelativeX, RelativeY;
unsigned int ModKeyMask;
/* open connection with the server */
display = XOpenDisplay(NULL);
if (display == NULL) {
printf("Cannot open display\n");
exit(1);
}
screen = DefaultScreen(display);
/* create window */
window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 200, 200, 1,
BlackPixel(display, screen), WhitePixel(display, screen));
/* select kind of events we are interested in */
XSelectInput(display, window, ExposureMask | ButtonPressMask);
/*XSelectInput(display, RootWindow(display, screen), ExposureMask | ButtonPressMask);*/
/* map (show) the window */
XMapWindow(display, window);
/* event loop */
while (1) {
XNextEvent(display, &event);
if (event.type == ButtonPress) {
printf("Button was pressed!\n");
XQueryPointer(display, window, &QueryRoot, &QueryChild, &AbsoluteX,
&AbsoluteY, &RelativeX, &RelativeY, &ModKeyMask);
printf("AbsoluteX: %d\n", AbsoluteX);
printf("AbsoluteY: %d\n", AbsoluteY);
}
}
/* close connection to server */
XCloseDisplay(display);
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks