Using the hook function [4]

Source: Internet
Author: User

The first parameter of the SetWindowsHookEx function represents the hook type, there are 14 choices, and we've used two of them before:

Wh_keyboard, Wh_mouse.

The system creates a table for each type of hook (that is, 14 tables), such as when an application starts the keyboard hook, and our own program starts the keyboard hook, and the keyboard hooks go into the same table. This table (perhaps more than one, may also have a mouse hook, etc.) is the legendary "hook chain."

If a hook-chain comes in with three hooks (for example: Hook A, hook B, Hook c), the last "Hook C" will be executed first.

is not advanced after out? I think it should be said: LIFO first! Does that have any difference? Yes! Because the advanced is not necessarily come out.

The last hook will be executed first, and the previously incoming hook (hook A, hook b) can be executed. It has to be said that the "Hook C" is in the running.

If the function of "Hook C" contains the CallNextHookEx statement, then "Hook A, Hook B" may be the light of the light; Or we'll just have to wait for the corresponding

UnhookWindowsHookEx to take them away (I think of Zhao Benshan's sketch ...).

At this time you may think: this is too good, I will not add CallNextHookEx, just let their hooks "rampant"; But what if your hook goes in advance?

So Windows recommends that the hook function call CallNextHookEx and take its return value as the hook function's own return value.

CallNextHookEx should also pass arguments to the next (or perhaps called previous) hook in the hook chain (for example, which button is pressed in the keyboard message). is a keyboard hook the same as the mouse hook parameter? Certainly not the same, so they are not in a "chain" AH; The type of hook in the same chain must be the same.

I'll talk to you again. The return value of the hook function:

Before that, the return value of the hook function, which we all followed Windows ' convention, returned the CallNextHookEx return value.

If CallNextHookEx succeeds, it returns the return value of the next hook, which is a serial sleeve;

If the CallNextHookEx fails, it returns 0, so the hook chain is broken, and only the current hook is still performing the task.

The return values of different types of hooks are different, and if you return a value other than 0 to the keyboard hook, it means that the message is eliminated after it has been processed.

Other words:

If the hook function to the keyboard result: = 0; Note that the message was intercepted and processed by the hook to "put";

If the hook function to the keyboard result: = 1; Note that the message was intercepted by the hook and processed and then to "kill".

In the following example, we simply do not use CallNextHookEx (anyway, just a hook for me), directly to the return value!

This is the next example of the demo animation:

In the animation, I entered the letter A for Memo in three different states.

When the hook is not started, the Memo can be entered normally;

When the hook function returns 0, after the hook, the function of the hook function (the return key value) is executed first, and the letter A can be entered successfully;

When the hook function returns a value other than 0 (for example, 1), the hook function (the return key value) is only performed on the hooks, and poor Memo doesn't know what happened.

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.