【发布时间】:2015-02-19 07:53:58
【问题描述】:
我已经编写了这个基本的 C++ 应用程序来理解 Windows 全局挂钩,因为我是新手。不幸的是,这在 Qt Creator 上很完美,但在 Visual Studio 中却不是那么好。事实上,它在 VS2013 中什么也没做。谁能详细说明为什么?真的很有帮助!
#include <iostream>
#include <fstream>
#include <Windows.h>
#pragma comment(lib, "user32.lib")
HHOOK hHook{ NULL };
LRESULT CALLBACK MyLowLevelKeyBoardProc(const int nCode, const WPARAM wParam, const LPARAM lParam)
{
std::cout << "Key Pressed!";
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
int main(int argc, char* argv[])
{
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL, 0);
if (hHook == NULL) {
std::cout << "Hook failed!" << std::endl;
}
return 0;
}
我已按照教程given here 进行操作。我也尝试过查阅许多在线文档,但我无法修复它,可能是因为我通常使用 C# 而不是 C++。
更新:这是 Qt 应用程序的外观。它几乎相同,只是主函数有点不同,std::cout 被 QDebug() 替换。还有一些额外的#include。
#include<QtCore/QCoreApplicaton>
#include<QDebug>
#include<QTime>
#include<QChar>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL, 0);
if (hHook == NULL) {
QDebug() << "Hook failed!";
}
return a.exec();
}
【问题讨论】:
-
当前程序会设置钩子并立即终止。你的 Qt 程序是什么样子的?
-
@trenki 完全正确!那就是问题所在。 Qt 程序看起来有点相似,我已经更新了帖子。
标签: c++ qt setwindowshookex