Realization keyboard monitoring Ⅱ-- easy to misunderstand the function CallNextHookEx

Original link: http://www.cnblogs.com/grenet/archive/2010/12/08/1900169.html

  "Above - Keyboard Hook API keyboard monitoring function achieved Ⅰ introduced Hook API function keyboard" in the.

  Focus on the key message processing function

  Private Function KeyboardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
      Dim MyKeyboardHookStruct As KeyboardHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(KeyboardHookStruct)), KeyboardHookStruct)

  

  Some code of its own process, for example: records, shielding, mapping

 

  Return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam)
  End Function

  

  Take a look at CallNextHookEx function, from the literal understanding is to call back a hook function. If the back has no hook function? Many people will mistakenly believe will pass the message to Window message processing function. They believe that the process flow of a message as shown below: Suppose there are four hook function, namely the hook A, the hook B, C hooks, hooks D

  Physical keystrokes

   ↓

  A hook

   ↓

  Hook B

   ↓

  Hook C

   ↓

  D hook

   ↓

  Window message handler

  They believe that as long as there are four hook function returns a 1 (non-zero), it will abort message delivery. Not even call CallNextHookEx function hook function will also prevent the delivery of messages. Even believe that modify the function parameters can change CallNextHookEx deliver key messages.

  Unfortunately, this idea is wrong.

  You can call the delete function in CallNextHookEx hook function, you will find the keys Window still got the message. You can also try to modify CallNextHookEx function parameters and see what will be the effect. So I try to direct error (even inexplicable exit).

  And then go back to the CallNextHookEx function and found it just calls the next hook function, but pass information between the hook function.

  The correct message processing procedure should be as follows: or in the story above example.

  Physical keystrokes

   ↓

  Hook hook management function A ← → ← → B ← → hook hook hooks D C ← →

   ↓

  Window message handler

 

  A function of the hook, if the call CallNextHookEx functions, the key message to the hook B; if not CallNextHookEx function call, the message hook button B will not be, in other words, the hook B fails, of course, at this time the hook C and D hook also ineffective. In order to live in peace between the hook, or should add a call to a function in the hook function CallNextHookEx inside.

 

  Say that the issue of the return value of the hook function. In the above case, the return value of the hook decision button A message is discarded. Return Value 0, tells the system, the message passed on to the message handler Window; message returns the value 1 (non-zero), tells the system, the message will be discarded, not Window message processing function key.

 

  Thus, the information only if the key statistics

  Finally hook function is called directly

  Return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam)

  By the following hook function to determine whether the message is discarded. (We live in peace)

 

  If a key mask

  After it is determined in the hook function, directly meet the requirements

  CallNextHookEx(hKeyboardHook, nCode, wParam, lParam)  

  Return 1

  Tell the system, the message is discarded. Of course, out of courtesy, before calling CallNextHookEx or function, so that other hook function processes the message

 

  As modifier keys (key mapping), to modify the parameters, CallNextHookEx function call is of no use. Because the original message simply do not change, you just change the message is passed to the other hook function. But also very easy to make mistakes.

  On how to modify the keys, it will be introduced later.
  

  

Reproduced in: https: //www.cnblogs.com/grenet/archive/2010/12/08/1900169.html

Guess you like

Origin blog.csdn.net/weixin_30194507/article/details/94784174