Sign in to follow this  

SetWindowsHookExA + WH_KEYBOARD_LL trouble, callback not called

Recommended Posts

Hello, I am experiencing trouble with a SetWindowsHookExA hook, specifically WM_KEYBOARD_LL.
I need to detect whether the keystrokes sent to a game window are injected or real.

But it turns out that the hook's callback doesn't even get called for the keystrokes sent to the game window, it is called for every other window.

/* ============== ! DLL CODE ! ============== */

HHOOK g_hHook;

LRESULT CALLBACK AC_LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )

Com_Printf( "CALLBACK! %03i ( %08X )\n", kd->scanCode, kd->flags );

return CallNextHookEx( g_hHook, nCode, wParam, lParam );

void AC_SetKeyboardHook( void ) {
if( !g_hHook ) {
g_hHook = SetWindowsHookExA( WH_KEYBOARD_LL, AC_LowLevelKeyboardProc, GetModuleHandle( NULL ), 0 );

void AC_DisableKeyboardHook( void ) {
if( g_hHook ) {
UnhookWindowsHookEx( g_hHook );

This code is put in DLL which is loaded by the main game executable (game is powered by OpenGL and DirectInput (mouse only)).

So, the callback function is never called when I press keys in the game window, it is called only for the other windows.

Surprisingly, if I hook from another EXE+DLL, it catches all keys, including the ones sent to the game. But I need to get it to work for the DLL game loads...

Kind regards :)

Share this post

Link to post
Share on other sites
or just NULL

GetModuleHandle(NULL) is wrong as it returns the hModule of the EXE
(which doesn't contain the HookProc)

and error checking is your friend in situations like this

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this