Generate mouse events using Qt framework

2019-08-08 14:33发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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



标签: c++ qt qt5.3