Hello,
I need to write code which detects whether keystrokes are injected by applications (with SendInput, keybd_event) or are genuinely coming from the keyboard.

I came across a possible solution: creating a WH_KEYBOARD_LL hook and checking the INJECTED flag.

So I've made this so far:

HHOOK	g_hHook;

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
{
	KBDLLHOOKSTRUCT	*kd = (KBDLLHOOKSTRUCT *)lParam;

	cg.ei.Com_Printf( "LowLevelKeyboardProc: flags = %08X\n", kd->flags );

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

void InitHooks( void ) {
	g_hHook = SetWindowsHookExA( WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle( NULL ), 0 );
}

This code is placed inside a DLL and the DLL is loaded by an EXE, an OpenGL powered game engine.

It catches all keystrokes which are sent to windows, with the exception of my game window, and that is the problem. I need it to catch the keystrokes sent to the game window as well.

Could you please help me with this?
Kind regards

NT

Member Avatar for MonsieurPointer

g_hHook = SetWindowsHookExA( WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle( NULL ), 0 );

I believe it has to do with two last parameters you pass into SetWindowsHookExA. From MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx):

hMod [in]

Type: HINSTANCE

A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.

dwThreadId [in]

Type: DWORD

[The identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.