Hook programming (Hook) installation in-process keyboard hooks (1)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.