当用户点击一个键显示消息 [英] displaying a message when the user taps a key
问题描述
下面的代码片断是为了显示当用户键入一个重要的消息。即使在重点不是应用程序。但似乎与下列code中的问题。它不会调用钩链与Windows注册的功能。我想这个问题是 HINSTANCE HINST
。我应该如何修改低于code,这样我能看到该邮件为用户点击一个键。
The following snippet is meant to display the message when the user types a key. Even when the focus is not on the application. But there seems to be a problem with the following code. It doesn't call the function registered in the hook-chain with the windows. I guess the problem is with HINSTANCE hInst
. How should I modify the below code so that I am able to see the message as the user taps a key.
// Global Variables
static HHOOK handleKeyboardHook = NULL;
HINSTANCE hInst = NULL;
void TestKeys_setWinHook // i call this function to activate the keyboard hook
(...) {
hInst = GetModuleHandle(NULL);
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInst, 0); // LowLevelKeyboardProc should be put in the hook chain by the windows,but till now it doesn't do so.
printf("Inside function setWinHook !");
}
// the following function should be called when the user taps a key.
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
printf("You pressed a key !\n");
return CallNextHookEx(handleKeyboardHook, nCode, wParam, lParam);
}
但Windows不会调用函数 LowLevelKeyboardProc
。我不明白其中的道理,但我相信这个问题是与 HINST
中的钩子函数。我该怎么办需要初始化呢?
But the windows doesn't call the function LowLevelKeyboardProc
. I don't understand the reason but I am sure that the problem is with hInst
in the hook function. How do i need to initialize it ?
到现在为止,输出我看到的是在功能setWinHook!
Till now, output that I see is Inside function setWinHook !
推荐答案
下面是一个LowLevelKeyboardProc的例子。
Here's an example of a LowLevelKeyboardProc.
HHOOK hHook;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
{
printf("You pressed a key!\n");
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
这篇关于当用户点击一个键显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!