Generate mouse events using Qt framework

2019-08-08 14:40发布

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?

标签: c++ qt qt5.3
3条回答
神经病院院长
2楼-- · 2019-08-08 14:53

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.

查看更多
forever°为你锁心
3楼-- · 2019-08-08 14:54

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

查看更多
唯我独甜
4楼-- · 2019-08-08 14:59

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

查看更多
登录 后发表回答