There is a third-party process with gui, in which several threads are running. You need to install the keyboard hook on the gui thread of this process. I do as follows:

processID = 8888; //pid HWND hWnd = GetForegroundWindow(); DWORD threadID = GetWindowThreadProcessId(hWnd, &processID); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)&KeyboardProc, GetModuleHandle(NULL), threadID); 

processId manually from the Task Manager (for debugging, then it will be returned by the script). If you replace the threadID parameter in the last line with 0, then the hook works, but it works everywhere - even in the browser. What am I doing wrong?

    1 answer 1

    In my task it was very important that the hook installation method and the KeyboardProc method were in the same code area (.cpp), and not in different dll. I found the answer:

    1) Making the processID global variable

    2) in the hook installation method

     processID = GetCurrentProcessId(); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)&KeyboardProc, NULL, 0); 

    3) In the KeyboardProc method:

     DWORD actProcess; HWND hwnd = GetForegroundWindow(); GetWindowThreadProcessId(hwnd, &actProcess); if (nCode == HC_ACTION) { if (processID == actProcess) { //do something } }