sharpchain
sharpchain

Reputation: 375

KeyboardProc Callback function not getting called?

So I am trying to make a message box appear when a user presses a button on his/her keyboard using Hooks.

The hook is getting installed correctly because there are no errors, but it seems like the KeyboardProc Callback function is not getting called because the message box that is supposed to show up when it is called never shows up.

There are no errors btw that show up and I am programming this in a desktop app.

Here is the code regarding the hook and the callback function:

LRESULT CALLBACK KeyboardProc(
int nCode, WPARAM keyState, LPARAM keyInfo) {
    LRESULT reValue = 0;
    MessageBox(hWnd, L"Testing", L"Test", MB_OK);//This is the msg box that isnt showing up
    if (nCode < 0) {
        reValue = CallNextHookEx(keyboardHook, nCode, keyState, keyInfo);
    }


    return reValue;
};

keyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hInstance, 0);

Upvotes: 0

Views: 785

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

You need to run a message loop to process the calls. From the remakrs on the KeyboardProc callback function description: "The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop."

Upvotes: 1

Related Questions