PDA

View Full Version : Mouse position on screen



Nippler
7th August 2008, 11:42
In my application I have to do like a drawing/painting of a custom polygon. For this purpose as long as the mouse button is pressed and the mouse is moving, the points of the mouse should be saved. After releasing the button the polygon will be painted.
The problem is that the mouse can go over the applications window. This points should be stored as well in order to shrink the whole polygon afterwards to fit in the window.

Now my question:
How can I get the mouse position on the whole screen? And how can I find out wheter the mouse button was released outside the applications window?
Or is there something like a global event filter?

jpn
7th August 2008, 11:47
Experiment with this test app:


// main.cpp
#include <QtGui>

class Widget : public QWidget
{
protected:
void mousePressEvent(QMouseEvent* event)
{
qDebug() << event << event->pos();
}

void mouseMoveEvent(QMouseEvent* event)
{
qDebug() << event << event->pos();
}

void mouseReleaseEvent(QMouseEvent* event)
{
qDebug() << event << event->pos();
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget window;
window.show();
return app.exec();
}

luf
7th August 2008, 14:37
Depending on platform you can create filters for the native messages. e.g. winEvent on windows. I know you can register to find keypresses in the win32 api, regardless of what application or what has focus, so I suspect you can do the same with mouse events.

Although that makes you a bit platform-dependent if you don't write routines for all platforms.

somebody
7th August 2008, 15:22
Hello,

I've a similar problem.

I know this is not that much Qt-related but in the end a Qt-Application will need that stuff.

I'm searching for a way to get the absolute mouse coordinates of every ButtonPress on the desktop. With 'every' I mean that a click can also occur on the desktop itself or on another application/window.

I was told that's not possible with Qt so I played around with xlib (since it's for unix/linux). With xlib I found a way to get the absolute coordinates if a ButtonPress occurs, but only if it occurs in the self made window:



#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;
}


Any hint how to accomplish this on x11?

And let's say that there is a way to do this...how to integrate this x11 stuff into a Qt-Application (since Qt uses xlib itself)?!