Article ID: 134655
Article Last Modified on 3/1/2005
// Trap keyboard messages
__declspec(dllexport) LRESULT CALLBACK HookFunction(
int code,
WPARAM wParam,
LPARAM lParam)
{
char szVCode[50];
//display the virtual key code trapped
sprintf(szVCode, "Virtual Key code: %lx", wParam);
MessageBox(NULL, szVCode,"Key stroke", MB_OK);
:
:
}
The associated .def file for this DLL might resemble this:
LIBRARY HOOK
EXPORTS
HookFunction
// add system-wide hook
hHookDll = LoadLibrary("hook");
hHookProc = (HOOKPROC) GetProcAddress(hHookDll, "HookFunction");
// Install keyboard hook to trap all keyboard messages
hSystemHook = SetWindowsHookEx(WH_KEYBOARD,hHookProc,hHookDll,0);
Once the application has finished with the system-wide hook, be sure to
undo the hooking process as follows:
// Remove the hook and unload the DLL used for the hooking process
UnhookWindowsHookEx(hSystemHook);
FreeLibrary(hHookDll);
Additional query words: 4.00
Keywords: kbregistry kbkernbase kbcode KB134655