Capturing mouse click outside the window
-
I am trying to capture mouse click event even when the window lost focus. I want to record the coordinates of the points which the user clicked and knowing when the mouse is clicked is essential. I have been googling for the answer yet I couldn't find a working solution. I even tried looking at the MSDN.
The mousePressEvent() function in Qt only captures mouse clicks that are sent to the window only and so is the eventFilter() function. (Even worse, some how the eventFilter() processes a click for several times.)
If anyone know how to perform this task, please share the solution with me.
-
This is not a trivial thing and Qt will not help you with this.
You mentioned MSDN so I assume you're only interested in Windows.
To do this you will need a library(dll) that gets injected into every process.
In that library you'll need to export a function with "this signature":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644988.aspx
Once you have your dll you need to load it in your app and pass its handle to the "SetWindowsHookEx":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx method with the WH_MOUSE parameter. Refer to the docs in the first link to see how to parse the arguments of the hook function to get mouse position and buttons.There's also an "easy" path, but it will be far less accurate. Create a timer and poll periodically for mouse position and status of the buttons.
You can get current cursor position with QCursor::pos(). getting mouse buttons requires Windows API call to GetAsyncKeyState with whichever button you want to check eg. VK_LBUTTON. Qt has also QApplication::mouseButtons() method, but it will work only when you click inside your app. -
Hi,
Maybe grabmouse() can do the job except on MacOS.
Sincerely -
[quote author="Chris Kawa" date="1416850156"]
To do this you will need a library(dll) that gets injected into every process.
In that library you'll need to export a function with "this signature":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644988.aspx
Once you have your dll you need to load it in your app and pass its handle to the "SetWindowsHookEx":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx method with the WH_MOUSE parameter. Refer to the docs in the first link to see how to parse the arguments of the hook function to get mouse position and buttons.
[/quote]Thanks for the reply. Well, actually I am kind of a beginner and the documentation could be mind-twisting to me. Based on what I've read and understood, should I start by adding the below codes in the comment?
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);//HOOKPROC hkprcSysMsg; //static HINSTANCE hinstDLL; //static HHOOK hhookSysMsg; //hinstDLL = LoadLibrary(TEXT("c:\\myapp\\sysmsg.dll")); //hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc"); //hhookSysMsg = SetWindowsHookEx(WH_MOUSE, hkprcSysMsg, hinstDLL, 0);
}
@The codes are grabbed from their example and I haven't changed the DLL 'cause I don't know what DLL should it be in my case.
Also, how do I use MouseProc() afterwards?
-
[quote author="archqt" date="1416857650"]Hi,
Maybe grabmouse() can do the job except on MacOS.
Sincerely[/quote]Thanks for the suggestion. After the grabmouse() is called, the buttons inside the program became unable to receive the click. And for the recording, it made the program capable of recording the click that caused the window to lose focus, but still it wouldn't record clicks after the focus is lost.