0

I'm using SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0); to set a global hook for capturing the keystrokes, but the result is strange.
The callback function can be executed when I press the "special" keys such as "Enter", "Tab", "Shift", "Ctrl" and other keys having a Virtual Key Code, while it fails to capture the keystrokes when I press the regular letters and digits.
I am confused about it and could anyone tell me the reason?

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

HHOOK keyboardHook = 0;

LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam)
{
    KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam;
    cout<< "[TEST] " << ks->vkCode << endl;

    return CallNextHookEx(0, code, wParam, lParam);
}

int main()
{
    keyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0);
    if (keyboardHook == 0)
    {
        cout << "failed" << endl;
        return -1;
    }
    cout << "ok" << endl;

    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    UnhookWindowsHookEx(keyboardHook);


    return 0;
}
Asesh
  • 3,186
  • 2
  • 21
  • 31
Sean
  • 1
  • 1
  • Have you tried WH_KEYBOARD than WH_KEYBOARD_LL? – user743414 May 27 '19 at 08:34
  • 2
    I don't understand what you mean by not capturing regular letters and digits. I can use your code to capture every keystroke. – Strive Sun May 27 '19 at 09:45
  • @StriveSun-MSFT Maybe it's my system's problem, the callback function was not called when I pressed a,b,c,1,2,3... on my keyboard. – Sean May 27 '19 at 10:08
  • @Sean I can do it on my computer. You try to recompile the program. – Strive Sun May 27 '19 at 10:13
  • @user743414 Not yet, it seems WH_KEYBOARD_LL is more recommended than WH_KEYBOARD. I am not sure whether DLL programming is needed(seems very difficult) when using WH_KEYBOARD and I'm trying it now. – Sean May 27 '19 at 10:42
  • You can only get *virtual* keys from this hook. Virtual keys are the same anywhere in the world. What character they produce depends on the keyboard layout that is active. A big hairy problem is that the active layout is a per-process property and there is no api to obtain that info for another process. This is the core reason why there are two hooks, if you need this to be accurate then you do need WH_KEYBOARD so the code runs in the context of the active process. Much, much harder to make that work. – Hans Passant May 27 '19 at 11:11
  • 1
    Haha, thanks for your concern! I have known the reason which is not about the code... I find my anti-virus software will silently stop the routing entering the callback function when I press letters and digits(maybe for some reasons of safety). So all we should do is to **close** the anti-virus software or add the program to the **white-list**, then the code can work perfectly. Thanks again for your help! – Sean May 27 '19 at 14:28
  • If you do not get callbacks for certain keyboard input, then there is some software running in your system that installs a low-level keyboard hook, that doesn't call `CallNextHookEx` under certain conditions. – IInspectable May 27 '19 at 17:26

0 Answers0