Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I try to do local hook to writing window on NotePad, my code is:
#include <windows.h>

HHOOK keyboardHook;

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (wParam == WM_KEYDOWN && nCode == HC_ACTION)
    {
        Beep(440, 440);
        return 0;
    }
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

int main()
{
    HWND hParent = FindWindow(L"Notepad", L"Untitled - Notepad");
    HWND hChild = FindWindowEx(hParent, NULL, L"Edit", L"");
    DWORD processId;
    DWORD threadId = GetWindowThreadProcessId(hChild, &processId);

    keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, threadId); // Why not work???

    MSG msg{ 0 };
    while (GetMessage(&msg, NULL, 0, 0) != 0);

    UnhookWindowsHookEx(keyboardHook);

    return 0;
}


"Global hook" works well when I put the last parameter of SetWindowsHookEx function is zero. But local hook with this parameter threadId (not equal to zero) is not working ! I don't know why is that ! Help me, please. Thanks.

What I have tried:

I tried to find this problem from internet, but there is no !
Posted
Updated 29-Aug-21 21:47pm

1 solution

I don't have an answer for you but there many articles on the topic of hooks at this site. Hopefully one of them will be of use to you. Are are links to a few of them :

API hooking revealed[^]
Mouse and KeyBoard Hooking utility with VC++[^]
PolyHook 2: C++17 x86/x64 Hooking Library[^]
Change to Global Keyboard and Mouse Hooks behavior in Windows 7[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900