HHOOK WINAPI SetWindowsHookEx(
__in int idHook,
__in HOOKPROC lpfn,
__in HINSTANCE hMod,
__in DWORD dwThreadId
);
In this as the doc says lpfn is a pointer to the hook procedure. Let the hook procedure be :
keyboardProcessing(.....) {
}
How do i call this hook procedure ? Even if i call this hook procedure how am i going to receive the keystrokes ?
Please explain how does SetWindowsHookEx function and how does it call the actual programmer-defined hook method to handle keys ?
The SetWindowsHookEx function is used to register a custom handler for some Windows events. From MSDN:
It means that, for a selected hook type (keyboard events, for example) Windows will call the supplied procedure (
lpfn) to notify that an event of that type occured. You can use it, for example, to get all the events from the keyboard even when they’re not directed to your application window (imagine to write a macro recorder).You do not have to call that procedure, you’ll supply its address in the call to
SetWindowsHookExand then Windows will call it for you when needed.As said you do not have to call it, if you register a hook for
WH_KEYBOARDthen Windows will call that procedure for each keyboard event. Do not forget to call CallNextHookExinside your procedure, hook is a chain of procedures and each one is responsible to call the next procedure in the chain. Finally release the hook before you quit your application with UnhookWindowsHookEx.