PDA

View Full Version : Mouse Right or Left click ???



anjanu
26th August 2011, 21:39
Dear Friends,

How do I perform mouse Right or Left click through code ???
Been scratching my head from the last two days but cannot seem to get through this:confused:

all I am trying to do is when a button is clicked then the mouse Right Click should happen:



void Widget::on_pushButton_6_clicked()
{
QPoint point(cur.pos().x(), cur.pos().y());

QApplication::postEvent(QApplication::desktop()->window(), new QMouseEvent(QMouseEvent::MouseButtonPress,point,Qt ::RightButton, Qt::RightButton,Qt::NoModifier));

QApplication::postEvent(QApplication::desktop()->window(), new QMouseEvent(QMouseEvent::MouseButtonRelease,point, Qt::RightButton, Qt::RightButton,Qt::NoModifier));

}

First I get the current cursor position and then I am trying to perform a Right Click event...but nothing happens:confused:

Lykurg
26th August 2011, 23:13
first, how deletes the event in your code? and is window() returning a valid pointer and is the point value correct for window()?

anjanu
27th August 2011, 08:41
Thanks Lykurg

Now I have changed my code a bit to this:



void Widget::on_pushButton_6_clicked()
{
QTimer::singleShot(4000, this, SLOT(clickAction()));
}

void Widget::clickAction()
{
QObject *desk = QApplication::activeWindow();
QPoint point(cur.pos().x(), cur.pos().y());
ui->plainTextEdit->appendPlainText("X:" + QString::number(cur.pos().x()) + " Y:" + QString::number(cur.pos().y()));
QApplication::postEvent(desk, new QMouseEvent(QMouseEvent::MouseButtonPress,point,Qt ::RightButton, Qt::RightButton,Qt::NoModifier));
QApplication::postEvent(desk, new QMouseEvent(QMouseEvent::MouseButtonRelease,point, Qt::RightButton, Qt::RightButton,Qt::NoModifier));
}


as you can see that the Window() is the current active window.
When I click the button which creates the timer for 5 sec, I quickly point and click the curson on the Desktop to make it the current window and after the timer timeouts I expect an Right Click event...but I get the correct co-ordinates (X, Y) value but still not Right Click happens...pls help:(

wysota
27th August 2011, 10:56
Why do you want to mimic mouse events? Sending events to widgets outside your application won't work this way.

anjanu
27th August 2011, 11:11
hi Wysota,
Actually I am trying to control the mouse cursor remotely...I can easily move the mouse cursor but cannot simulate click events...all this was very easy in java using Robot API.

and friend do you mean sending events outside the application won't work...I mean is't it possible in Qt ???
can you recommend me reading some this so that I can have better understanding...

wysota
27th August 2011, 11:14
It's possible but not using Qt's event API. You have to do that using native API, Qt events can't be translated to system events.

anjanu
27th August 2011, 11:17
do you mean the native OS(MS Windows and Linux) APIs...

if yes how can I begin with them ???

wysota
27th August 2011, 11:49
do you mean the native OS(MS Windows and Linux) APIs...
Yes. But X11 rather than Linux.


if yes how can I begin with them ???
By getting familiar with their docs, probably :)

anjanu
27th August 2011, 18:54
I dont get this...do I need to use Visual C++ APIs for windows ???
win32API, windows.h...what do I need to do ???

Added after 52 minutes:

friends I finally got it working...here's what I did:

include the header file "windows.h"
and use mouse_event(5 param);

here's what I did for Right Click



mouse_event(MOUSEEVENTF_RIGHTDOWN,100, 200, 0,0);
mouse_event(MOUSEEVENTF_RIGHTUP, 100, 200, 0,0);

wysota
27th August 2011, 19:41
I dont get this...do I need to use Visual C++ APIs for windows ???
There is no such thing as "Visual C++ API". There is only WinAPI and it is C-based.

Congrats that you made it work.