Summary: hooks can monitor various event messages in a system or process. Intercepts messages destined for the target form and processes them. This allows us to install our own defined hooks in the system to monitor the occurrence of specific events in the system. Completion of specific functions, such as the interception of keyboard, mouse input. screen fetching words, log monitoring and so on.
The following shows how to install an in-process keyboard hook,
Step 1: Open VC6.0. Create an MFC application that is based on a dialog box.
Step 2: Write for example the following code on the bool Chookdlg::oninitdialog () function: (Note that it is written above.) Not in OnInitDialog ()
Hhook G_hkeyboard = NULL; LRESULT CALLBACK keyboardproc (int ncode, WPARAM WPARAM, LPARAM LPARAM) {return 1;}
Step 3: Install the keyboard hooks in the OnInitDialog () function. Copy and paste such as the following code,
G_hkeyboard = SetWindowsHookEx (Wh_keyboard,keyboardproc,null,getcurrentthreadid ());
Step 4: Compile-by-link, execute: This time you find that the keyboard event is no longer responding ~ ~ ~
Of course. Suppose you just want to block out the SPACEBAR, you can write that,
LRESULT CALLBACK keyboardproc (int ncode, WPARAM WPARAM, LPARAM LPARAM) {if (Vk_space = = WPARAM) return 1;elsereturn Callnext Hookex (G_hkeyboard, Ncode, WParam, LParam);}
Let's say the SPACEBAR is pressed to return directly to the system. The message is already running.
Otherwise, pass the message to the next hook ~ ~ ~
Another is the need to block some key combinations, for example: Alt+f4. Can be implemented in the following ways, for example,
LRESULT CALLBACK keyboardproc (int ncode, WPARAM WPARAM, LPARAM LPARAM) {if (Vk_f4 = = WPARAM | | (1 = = (lparam>>29 & 1))) Return 1;elsereturn CallNextHookEx (G_hkeyboard, Ncode, WParam, LParam);}
lparam The meanings of the members are as follows:
Want to know a lot about the in-process hooks. Be able to read this article "Hook programming" mouse hooks in the installation process
Hook programming (Hook) installation in-process keyboard hooks (1)