1

I am using Qt framework and I would like to generate mouse events outside my application window.

So far I managed to move the mouse pointer using:

QGuiApplication::overrideCursor()->setPos(x,y);

How can I also generate left mouse button click, middle button click, right button click and mouse wheel movement?

Yiannis Mpourkelis
  • 1,366
  • 1
  • 15
  • 34

3 Answers3

3

There's no way to do that with Qt. Use APIs specific for your target platform.

Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
2

A few years ago i wrote a drivers for GUI testing (for mouse and keyboard). Drivers are developed for Windows, Linux and MacOS X. You can look at here. There is a OS depended MouseDriver implementation for Windows. Also you can see other implementations. This is a open source project, you can use it code for free.

Kastaneda
  • 739
  • 1
  • 8
  • 15
  • Thank you Kastaneda. My project is also open source so I think I will use your code and I will mention you in the license. How well is your code tested? Are there any known bugs in this code? – Yiannis Mpourkelis Oct 09 '14 at 18:56
  • This code is using about 3 years. It is used to write a GUI tests. I think all obvious bugs are fixed already. You should find a latest version of code in code repository for reduce to a minimum a likely presence of bugs. – Kastaneda Oct 10 '14 at 06:51
1

Addition to first answer. For example in Windows:

Click:

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP,x,y,0,0);

Wheel:

mouse_event(MOUSEEVENTF_WHEEL, 0, 0, WHEEL_DELTA, NULL);

More information: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx

Use also qt macros:

#ifdef Q_WS_X11
//Linux
#endif
#ifdef Q_WS_WIN
//Windows
#endif
#ifdef Q_WS_MACX
//Mac
#endif

More about macros: http://qt-project.org/doc/qt-5/qtglobal.html

QSysInfo: http://qt-project.org/doc/qt-5/qsysinfo.html

Jablonski
  • 18,083
  • 2
  • 46
  • 47