2

Note: I am working in plain C. Not C++, not C#.

I am working on a mod. I've already written a working DLL-injector, as well as the DLL to be injected. Everything is going well, apart from the userinput.

I want to be able to use hotkeys, so I tried to setup a keyboardhook using SetWindowsHookEx. The following is my callback function:

LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    printf("key touched\n");
    if (wParam == VK_F5)
    {
        keyEvent = VK_F5;
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

And this is how I set it up:

HHOOK kbHookHandle = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)keyboardHook, NULL, GetCurrentThreadId());

if (kbHookHandle != NULL)
{
    printf("keyboard hook successful!\n");
}
else
{
    printf("keyboard hook failed!\n");
}

As far as I can tell, the hook gets setup well (I used to have a problem with an invalid parameter, but fixed that by using GetCurrentThreadID). It returns a handle which is not NULL.

But whenever I press a key, there is no output.

To further clarify: The code above is from the injected DLL. So it effectively 'belongs' to the game process. I have allocated a console using AllocConsole, to print debug messages to.

What am I doing wrong?

EDIT: To clarify (even more): the listed code is from the injected DLL. It is not the approach I use to inject the DLL - I wrote a seperate (working!) program to do just that.

It surprises some that I use printf(), since that wouldn't show up, considering I call it from inside the host process. Yes, I do call it from inside the host process, but that is not an issue, because I have already allocated a working console. I used an approach very similar to the one mentioned here

EDIT2: I am not asking why printf() isn't working (because it is), I am asking why this keyboardhook isn't working.

4

1 回答 1

0

以下是我在NoQuake ID Tech 游戏最小化器中检测按键按下和按键向上的方法。希望能帮助到你。

于 2011-06-08T11:54:25.870 回答