我想使用SetWindowsHookEx()来按下用户的键并单击鼠标。 我已经定义了一个全局键盘/鼠标钩子并使用SetWindowsHookEx安装它。 钩子适用于几乎应用,但它不能用于特定应用(例如Cain& Abel)。
当我第一次点击Cain&阿贝尔,我可以获得点击信息。然后它才能工作,直到我点击另一个应用程序或控制台。
以下是我的代码的一部分:
#include <QtCore/QCoreApplication>
#include <QtDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <Windows.h>
#pragma comment(lib, "user32.lib")
HHOOK hHook = NULL;
HHOOK hMouseHook = NULL;
主:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
HINSTANCE hInstance = GetModuleHandle(NULL);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowerLevelKeyBoardProc, NULL, 0);
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MyLowerLevelMouseProc, hInstance, 0);
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
UnhookWindowsHookEx(hHook);
UnhookWindowsHookEx(hMouseHook);
if (hHook == NULL || hMouseHook == NULL)
{
qDebug() << "Hook failed";
}
return a.exec();
}
回调函数:MyLowerLevelMouseProc
LRESULT CALLBACK MyLowerLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam){
MOUSEHOOKSTRUCT *pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL){
POINT screenpoint;
HWND hwndFoundWindow = NULL;
switch(wParam){
case WM_LBUTTONDOWN:
GetCursorPos (&screenpoint);
hwndFoundWindow = WindowFromPoint (screenpoint);
qDebug() << "1. Mouse click! (Left), x= " << screenpoint.x << ", y= " << screenpoint.y;
qDebug() << "2. hwndFoundWindow=" << hwndFoundWindow
break;
case WM_RBUTTONDOWN:
GetCursorPos (&screenpoint);
hwndFoundWindow = WindowFromPoint (screenpoint);
qDebug() << "Mouse click! (Right)" ;
break;
}
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
如果有任何想法和解决方案,请帮助我。 非常感谢。