2
votes

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.

1
Have you redirected stdout? What if you do a printf after your AllocConsole?tibur
Yep. Printf() is working as it should, all of my messages are shown except the test message in the callbackthaCURSEDpie
Does your keyboard hook code work when used outside of an injected dll, like for example a standalone program?Null Set
Do you want a global hotkey or only one that works if the current thread receives input?CodesInChaos
@Null Set Good question, I haven't tried that yet. Will give it a try. @CodeInChaos Either way will do. The user will be in-game when he uses my mod, so it wouldn't really matter.thaCURSEDpie

1 Answers

0
votes

Here's how I detect key down and key up in my NoQuake ID Tech games minimizer. Hope it helps.