Hook This thing is sometimes loveful, hook is used to intercept some of the information of some information, for example, we want the system whether it will take a CTL-B as much as possible
Perform NotePad, maybe you will use Form's KeyPreview, set to True, but press CTL-B in other process? That is useless, then you have to set it.
Keyboard Hook to intercept all Key IN keys; such as: Mousemove is valid only on the form or control, if you want to get outside the Form can also get
Know Mouse Move information, then use the mouse hook to the information of the mouse. Another example: You want to record all keyboard actions or MOSUE action,
To record the gather, use JournalRecordhook, if you want to stop all MOSUE keyboard actions, and put (execute) the gather, then use Journalplayback
Hook; hook, can be the REMOTE HOOK, ie other process's action you can also intercept, or Localhook, its interception
There is only Process itself. Remote Hook Hook Function To be in .dll, local hook is in .bas. How to set HOOK in VB? Use setWindowshookex ()
Declare function setWindowshookex lib "user32" alias "setwindowshookexa" (byval IDHOKEXA "(Byval LPFN AS"
Long, Byval HMOD AS Long, ByVal DWTHREADID AS Long AS Long
idHook representative of what Hook, there are several Public Const WH_CALLWNDPROC = 4Public Const WH_CALLWNDPROCRET = 12Public Const WH_CBT = 5Public Const WH_DEBUG = 9Public Const WH_FOREGROUNDIDLE = 11Public Const WH_GETMESSAGE = 3Public Const WH_HARDWARE = 8Public Const WH_JOURNALPLAYBACK = 1Public Const WH_JOURNALRECORD = 0Public Const WH_KEYBOARD = 2PUBLIC const wh_mouse = 7public const wh_msgfilter = (-1) public const wh_shell = 10public const wh_sysmsgfilter = 6
LPFN represents the address of Hook Function, this is a Callback FuCnction, when you hang an HOOK, we have to define a Function.
When a message is generated, it handles its function, this hook function has a certain parameter format.
Private function hookfunc (Byval Ncode As Long, Byval LParam as long) As long
Ncode represents the hook produced under the condition, and the possible value of different groups of possible wparam lparam transmission values vary with HOOK, different from the value of hook and NCODE.
Because this parameter is a Function address, we fix the Hook Function in .bas and incorporated with Addressof Hookfunc. As for hook
Function's name We can be given, not necessarily hookfunc
HMOD represents .dll's Hinstance, if it is Local Hook, this value can be null (VB can be transmitted 0), and if it is Remote
Hook, you can use the getModuleHandle (". DLL name") to pass.
DWTHREADID represents this hook's ThreadID, if not set is that thread is doing, then zero 0 (so in general, Remote Hook passed into 0), and
VB's local hook generally transmits app.threadID in
Valueback value If setWindowshookex () is successful, it will pass a value, representing the current Hook Handle, this value is to be recorded.
Because the A program can have a System Hook, such as the Keyboard Hook, and the B program also has a Remote's Keyboard Hook, then in the end.
KEYBOARD information Who is intercepted? The answer is that the last one is intercepted, that is, a first to do the keyboard hook, and then B is doing, the information is intercepted by B, that A
? Just look at the Hook Function of B. If b wants A's Hook Function to have this information, then B will call
CallNextHooKex () This information passes gives A, which generates a connection of the HOOK. If you don't want PASS this information to A, don't call callnexthookex ()
.
Declare function callnexthookex lib "user32" alias "callnexthookex" _ (byval hhook as long, _byval ncode as long, _lparam as any) As long
The hHOOK value is the return value of SetWindowsHooKex (), Ncode, WPARAM, LPARAM is three parameters in hook procedure.
Finally, remove this hook, please call unHookWindowHookex ()
Declare Function UnHookWindowshookex lib "user32" alias "unhookwindowshookex" _ (Byval HHOOK AS long) AS Long
HHOOK is the return value of SetWindowsHooKex (). At this time, in the above example, the B program ends hook, then change A can intercept information directly.
KEYBOARD HOOK Example
Hook Function three parameters
NCODE WPARAM LPARAM Passage HC_Action or hc_noremove table button Virtual Key with WM_KEYDOWN If information is to be processed 0 Conversion 1
Public HHOOK As Long
Public Sub UnHookKBD ()
IF HNEXTHOOKPROC <> 0 THEN
UnhookWindowshookex hHOOKHHOOK = 0
END IF
End Sub
Public function enablekbdhook ()
If HHOOK <> 0 Then EXIT FUNXHHOOK = SETWINDOWSHOKEX (wh_keyboard, addressof mykbhfunc, app.hinstance, app.threadID)
END FUNCTION
Public Function Mykbhfunc (Byval Icode As Long, Byval LParam as long) As long
Mykbhfunc = 0 'indicates that you want to handle this information.
IF wparam = vbkeysnapshot life 'detection Do not press the PrintScreen button
Mykbhfunc = 1 'Eat this information in this hook
End ifcall callnexthookex (hHOOK, ICODE, WPARAM, LPARAM) is passed to the next hook
END FUNCTION
As for other hook details and NCODE, WPARAM, LPARAM, please check Win32 Help