Hi, I'm like doing certain events with my mouse. Now what I want is to return the value of the instance but I don't know how: cpp code:
#include "mouselogger.h"
#include <QDebug>
MouseLogger &MouseLogger::instance()
{
static MouseLogger _instance;
return _instance;
}
MouseLogger::MouseLogger(QObject *parent) : QObject(parent)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
// Set hook
mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
// Check hook is correctly
if (mouseHook == NULL) {
qWarning() << "Mouse Hook failed";
}
}
LRESULT CALLBACK MouseLogger::mouseProc(int Code, WPARAM wParam, LPARAM lParam)
{
Q_UNUSED(Code);
// Having an event hook, we nned to cast argument lParam
// to the structure of the mouse is the hook.
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
// Next, we check to see what kind of event occurred,
// if the structure is not a pointer to nullptr
if(pMouseStruct != nullptr) {
switch (wParam) {
case WM_MOUSEMOVE:
qDebug() << "WM_MOUSEMOVE";
break;
case WM_LBUTTONDOWN:
qDebug() << "WM_LBUTTONDOWN";
break;
case WM_LBUTTONUP:
qDebug() << "WM_LBUTTONUP";
break;
case WM_RBUTTONDOWN:
qDebug() << "WM_RBUTTONDOWN";
break;
case WM_RBUTTONUP:
qDebug() << "WM_RBUTTONUP";
break;
case WM_MBUTTONDOWN:
qDebug() << "WM_MBUTTONDOWN";
break;
case WM_MBUTTONUP:
qDebug() << "WM_MBUTTONUP";
break;
case WM_MOUSEWHEEL:
qDebug() << "WM_MOUSEWHEEL";
break;
default:
break;
}
emit instance().mouseEvent();
}
// After that you need to return back to the chain hook event handlers
return CallNextHookEx(NULL, Code, wParam, lParam);
}
main() code:
//#include <QCoreApplication>
//int main(int argc, char *argv[])
//{
// QCoreApplication a(argc, argv);
//return a.exec();
//}
#include <QCoreApplication>
#include <QDebug>
#include "mouselogger.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
[](){
//retornar el valor
qDebug() << s;
});
return a.exec();
}
I need this for a job, thank you.
Assuming you intend to fetch
s
, the bad news is that you won't be able to do it with a lambda. However, nothing prevents you from using a class to manage the information:To know the value of
s
it is enough to call (from any part of the code)SignalReceiver::Instance().GetS()
.It's not a particularly elegant design, but it gets the job done. Of course, each event that enters will crush the value of
s
. If you do not provide more information it is difficult to give a more accurate answer.All the best.