PDA

View Full Version : deactivate hook with qobject



SirJonas
24th October 2016, 13:05
Hi i'm connecting to my external class with qobject to check mouse event's . But when i try to deactivate the hook with emit instance().event(); doesnt seem's to work the problem seem this is not call signal. Obviously when i do this:

mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
UnhookWindowsHookEx(mouseHook);

But my problem is when i do qobject::connect(..instance..signal). The signal never is called?
Code:
main.cpp:


#include <QCoreApplication>
#include <QDebug>
#include "mouselogger.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
[](){

});

return a.exec();
}


mouse.cpp:


#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);
}

mouse.h:


#ifndef MOUSELOGGER_H
#define MOUSELOGGER_H

#include <QObject>
#include <windows.h>

class MouseLogger : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(MouseLogger)
public:
static MouseLogger &instance();
explicit MouseLogger(QObject *parent = nullptr);
virtual ~MouseLogger(){}

// Static method that will act as a callback-function
static LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam);
signals:
// The signal, which will report the occurrence of an event
void mouseEvent();
void Eventos();

public slots:


private:
// hook handler
HHOOK mouseHook;
};

#endif // MOUSELOGGER_H

anda_skoa
24th October 2016, 13:09
So, what exactly is the problem you are facing?

Can't be the processing of the signal as your lambda is empty.

What is your reason for the native event handling code?

Cheers,
_

SirJonas
24th October 2016, 13:24
ok solved one question qobject it's only for connect two objects but i can't call my signal with qobject::connect...? sorry again!

anda_skoa
24th October 2016, 14:40
ok solved one question

Since you haven't even asked a question you are now at -1 open questions.



qobject it's only for connect two objects[quote]
You can connect any number of QObject with signal/slot connections, two is one of the many cases.

[QUOTE=SirJonas;294805]i can't call my signal with qobject::connect...? sorry again!
Connections have nothing to do with emitting a signal.

Cheers,
_