Problema com Funçao que Captura o evento de click do Mouse do Sistema Operacional
-
A funcao esta funcionando perfeitamente, o problema é quando eu do um Click seguro e arrasto o cursor... ele identifica milhoes de click e eu quero que identifique uma vez só.
seghe o codigo:
@#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>HHOOK MouseHook= NULL;
using namespace std;
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int LBUTTON=0, RBUTTON=0;
if (nCode < 0) // do not process the message
return CallNextHookEx(0, nCode,wParam, lParam);LBUTTON=(int)GetKeyState(VK_LBUTTON); RBUTTON=(int)GetKeyState(VK_RBUTTON); if(LBUTTON<0||RBUTTON<0) qDebug()<<"CLICK"; return CallNextHookEx(0, nCode, wParam,lParam);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);
if(MouseHook == NULL)
{
qDebug() << "Hook failed";
}
return a.exec();
}
@Alguem arrisca uma soluçao?
-
Resolvi o problema da seguinte maneira:
@#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>HHOOK MouseHook= NULL;
int flagLBUTTON=0,flagRBUTTON=0;using namespace std;
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int LBUTTON=0, RBUTTON=0;
if (nCode < 0) // do not process the message
return CallNextHookEx(0, nCode,wParam, lParam);LBUTTON=(int)GetKeyState(VK_LBUTTON); RBUTTON=(int)GetKeyState(VK_RBUTTON); if(LBUTTON<0) flagLBUTTON=1; else if(LBUTTON>=0&&flagLBUTTON==1) flagLBUTTON=-1; if(RBUTTON<0) flagRBUTTON=1; else if(RBUTTON>=0&&flagRBUTTON==1) flagRBUTTON=-1; if(flagLBUTTON!=0&&flagLBUTTON==-1) { qDebug()<<"LBUTTON"; flagLBUTTON=0; } if(flagRBUTTON!=0&&flagRBUTTON==-1) { qDebug()<<"RBUTTON"; flagRBUTTON=0; } return CallNextHookEx(0, nCode, wParam,lParam);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);
if(MouseHook == NULL)
{
qDebug() << "Hook failed";
}
return a.exec();
}
@alguem conhece alguma soluçao que nao utilize essa gambiarra?
-
Achei uma soluçao melhor...
segue:
@#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>HHOOK MouseHook= NULL;
using namespace std;
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int LBUTTON,RBUTTON;
if (nCode < 0) // do not process the message
return CallNextHookEx(0, nCode,wParam, lParam);LBUTTON = GetAsyncKeyState(VK_LBUTTON); if(LBUTTON==-32767) qDebug()<<"Click1"; RBUTTON = GetAsyncKeyState(VK_RBUTTON); if(RBUTTON==-32767) qDebug()<<"Click2"; return CallNextHookEx(0, nCode, wParam,lParam);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);
if(MouseHook == NULL)
{
qDebug() << "Hook failed";
}
return a.exec();
}@