C++ SetWindowsHookEx WH_KEYBOARD_LL 正确设置

发布于 2024-08-20 08:06:28 字数 1235 浏览 15 评论 0原文

我正在创建一个控制台应用程序,我想在其中记录按键操作(如向上键)。我创建了一个低级键盘挂钩,它应该捕获任何线程中的所有按键并调用我的回调函数,但它不起作用。当我按下某个键时,程序会暂停一会儿,但不会调用回调。我检查了文档但没有发现任何内容。我不知道我是否错误地使用了 SetWindowsHookEx() (据我所知,它成功创建了挂钩)或者我的回调函数不正确!我不确定出了什么问题!预先感谢您的帮助。

#include "Windows.h"
#include <iostream>
using namespace std;

HHOOK hookHandle;

LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[]) {

 hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, NULL, 0);

 if(hookHandle == NULL) {
  cout << "ERROR CREATING HOOK: ";
  cout << GetLastError() << endl;
  getchar();
  return 0;
 }

 MSG message;

 while(GetMessage(&message, NULL, 0, 0) != 0) {
  TranslateMessage( &message );
  DispatchMessage( &message );
 }

 cout << "Press any key to quit...";
 getchar();

 UnhookWindowsHookEx(hookHandle);

 return 0;
}


LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam) {
 cout << "Hello!" << endl;

 // Checks whether params contain action about keystroke
 if(nCode == HC_ACTION) {
  cout << ((KBDLLHOOKSTRUCT *) lParam)->vkCode << endl;
 }

 return CallNextHookEx(hookHandle, nCode, 
            wParam, lParam);
}

I'm creating a console application in which I'd like to record key presses (like the UP ARROW). I've created a Low Level Keyboard Hook that is supposed to capture all Key Presses in any thread and invoke my callback function, but it isn't working. The program stalls for a bit when I hit a key, but never invokes the callback. I've checked the documentation but haven't found anything. I don't know whether I'm using SetWindowsHookEx() incorrectly (to my knowledge it successfully creates the hook) or my callback function is incorrect! I'm not sure whats wrong! Thanks in advance for the help.

#include "Windows.h"
#include <iostream>
using namespace std;

HHOOK hookHandle;

LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[]) {

 hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, NULL, 0);

 if(hookHandle == NULL) {
  cout << "ERROR CREATING HOOK: ";
  cout << GetLastError() << endl;
  getchar();
  return 0;
 }

 MSG message;

 while(GetMessage(&message, NULL, 0, 0) != 0) {
  TranslateMessage( &message );
  DispatchMessage( &message );
 }

 cout << "Press any key to quit...";
 getchar();

 UnhookWindowsHookEx(hookHandle);

 return 0;
}


LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam) {
 cout << "Hello!" << endl;

 // Checks whether params contain action about keystroke
 if(nCode == HC_ACTION) {
  cout << ((KBDLLHOOKSTRUCT *) lParam)->vkCode << endl;
 }

 return CallNextHookEx(hookHandle, nCode, 
            wParam, lParam);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-08-27 08:06:28

您不能阻止系统调用(getchar),您必须在调用钩子之前运行窗口循环并处理消息。

You can't block on a syscall (the getchar), you have to be running a window loop and processing messages before your hook gets called.

灯角 2024-08-27 08:06:28

在 Windows XP 上,您需要将 hInstance(来自 WinMain)作为第三个参数传递给 SetWindowsHookEx。例如:

int WINAPI WinMain
( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPTSTR lpCmdLine, int nCmdShow ) {

  hookHandle = SetWindowsHookEx ( WH_KEYBOARD_LL, keyHandler, hInstance, 0 );

// ...

On Windows XP, you need, you need to pass hInstance (from WinMain) as the third argument to SetWindowsHookEx. For example:

int WINAPI WinMain
( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPTSTR lpCmdLine, int nCmdShow ) {

  hookHandle = SetWindowsHookEx ( WH_KEYBOARD_LL, keyHandler, hInstance, 0 );

// ...
故事未完 2024-08-27 08:06:28

我建议先简单;

// VB:检索应用程序实例
HINSTANCE appInstance = GetModuleHandle(NULL);

进而:
hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, appInstance, 0);

// ...,但是稍后还有另一个错误

I suggest simle first;

// VB: Retrieve the applications instance
HINSTANCE appInstance = GetModuleHandle(NULL);

and then:
hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, appInstance, 0);

// ..., but there are another errors later, too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文