PDA

View Full Version : Simulate left and right clicks?



hakermania
27th May 2011, 11:46
Hello.
I want to know if it is possible to grab somehow the mouse and detect the right, the left clicks and where in the screen they happened and store these values in an integer table like
int a [ 1, 123, 456 ]
for example this will mean that it was a right click (1) and happened at 123,456 (x,y)

After having stored those values I want to repeat these actions, something like a macro.

I know how to move the mouse to x,y, what I don't know is how actually to
1) grab the mouse for left and right clicks
2) Save the data from the "grabbing" in different variables every time and in the end, after the grabbing ends, how do I loop among them so as to repeat the action depending on the data stored?(actually C++ question, not Qt)


I don't want code ready to execute, I want a bit of guidance here, but sample code for better understanding is welcome.

Thx in advance for any answer :)

high_flyer
27th May 2011, 11:48
Do you want to grab the mouse in your own application or in windows outside your application?

stampede
27th May 2011, 12:17
As for 2), you can have a list of your own structures, for example:


typedef enum{
CLICK_LEFT = 0,
CLICK_RIGHT = 1
} MyClickType;

typedef struct{
ClickType type;
QPoint pos;
} MyMouseClick;

typedef QList<MyMouseClick> MyMouseClickList;

On each grabbed click, push back new structure filled with correct data to the list:


// somehow you've grabbed the click
QPoint pos = clickPos;
MyClickType type = CLICK_RIGHT;
//... store it in list
MyMouseClickList clickList;
//...
MyMouseClick c;
c.type = type;
c.pos = clickPos;
clickList.push_back( c );

Clicks will be in the correct order this way (first grabbed click will be first in list).
Later you can use for example foreach() macro to loop through the list:


foreach( const MyMouseClick& c, clickList ){
doSomething(c);
}

You can introduce a class instead of POD-like struct, but it's up to your preferences.

DanH
27th May 2011, 13:13
Probably you want to intercept the mouse events. Several ways to do it, depending ... Probably setEventFilter (http://doc.qt.nokia.com/4.7/qcoreapplication.html#setEventFilter) is the best combo of ease and generality.

You can also intercept mouse events a the widget level (including the main screen widget) with installEventFilter (http://doc.qt.nokia.com/4.7/qobject.html#installEventFilter), or you can subclass the appropriate object and override the various MouseEvents (http://doc.qt.nokia.com/4.7/qmouseevent.html).

hakermania
8th June 2011, 12:31
Thx for the replies, but I'd like to capture the mouse clicks not only IN my application, but between all the open windows, desktop, everywhere :)

high_flyer
8th June 2011, 12:44
This is not trivial, and not portable, and beyond the scope of Qt.
You will have to google it for the platforms on which you want to implement it.
I found this for windows, but I didn't test it:
http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx?display=Print