program-o-steve
program-o-steve

Reputation: 2678

calling the method pointed to by SetWindowsHookEx

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 ?

Upvotes: 0

Views: 395

Answers (1)

Adriano Repetti
Adriano Repetti

Reputation: 67080

The SetWindowsHookEx function is used to register a custom handler for some Windows events. From MSDN:

Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.

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).

How do i call this hook procedure?

You do not have to call that procedure, you'll supply its address in the call to SetWindowsHookEx and then Windows will call it for you when needed.

Even if i call this hook procedure how am i going to receive the keystrokes?

As said you do not have to call it, if you register a hook for WH_KEYBOARD then Windows will call that procedure for each keyboard event. Do not forget to call CallNextHookEx inside 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.

Upvotes: 1

Related Questions