This article is not an article on the hook principle, just a discussion of how to invoke the hook function in vb.net, and the change in API usage in vb.net. due to the variety of hooks, the most commonly used keyboard hooks are analyzed in this paper. first, a look at the VB is how to achieve. The section is the space bar. The goal of performance is: A form, which has a textbox, regardless of whether the focus falls in the textbox, press the SPACEBAR, will not enter a space in the textbox, but into a textbox to write a sentence: "Hook success!" " 1. Write the following code in a module: Declaration of the invoked API: Declare Function SetWindowsHookEx Lib "user32" Alias "Setwindowshookexa" (ByVal Idhook as Long, ByVal LPFN as Long, by Val Hmod as Long, ByVal dwThreadID as Long) as Long function Description: This function is used to start hook settings. Idhook is the type of hook, the type of message that is processed. LPFN is the address pointer to the hook Cheng (function or procedure). If the dwThreadID parameter is 0 or an identity of a thread created by another process, LPFN must point to the hook thread in the DLL. In addition, LPFN can point to a section of the Hook subroutine code for the current process (that's what we're using). Hmod is a handle to an application instance that identifies the DLL that contains the Cheng that LPFN refers to. If dwThreadID identifies a thread created by the current process, and the subroutine code is in the current process, hmod must be null. dwThreadID is the identifier of the thread associated with the installation of the hook child threads relative, if it is associated with all threads for the 0,hook subroutine. Return value: The function succeeds returning the handle of the Hook subroutine, and the failure returns NULL. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal Hhook as Long) as Long function Description: This function is the use of lifting hooks. Hhook is the handle to the hook function. Declare Function CallNextHookEx Lib "user32" (ByVal hhook as Long, ByVal ncode as Long, ByVal WParam as Long, LParam A s any) as Long function Description: The function is to pass the hook information in the current hook chain to the next hook. The Hhook is the handle to the current hook, and an application receives the handle as a result of the previous call to the SetWindowsHookEx function. ncode refers to the hook code passed to the current hook process and the next hook process uses this code to determine how to handle the hook information. wparam refers to the wparam value passed to the current hook process, and its specific meaning is determined by the type of the associated hook in the current hook chain. lparam refers to the LPARAM value passed to the current hook process, and its specific meaning is determined by the type of the associated hook in the current hook chain. 2. The constants defined are:Public Hnexthookproc as Longpublic const Wh_keyboard = 2 ' This is to indicate that the type of hook is the keyboard hookpublic const PM_KEY_SPACE = &h20 ' SPACEBAR 3. Code SnippetsPublic Sub unhookkbd () ' Solve keyboard hook function If hnexthookproc <> 0 then UnhookWindowsHookEx hnexthookproc Hnexthookproc = 0 End IfEnd Sub public Function enablekbdhook () ' Set keyboard hook If hnexthookproc <> 0 then Exit Function end if Hnexthookproc = SetWindowsHookEx (Wh_keyboard, AddressOf _ Mykbhfunc, app.hinstance, 0) If hnexthookproc <> 0 then enablekbdhook = hnexthookproc & nbsp End IfEnd Function public Function Mykbhfunc (ByVal icode as Long, _ ByVal WParam as Long, ByVal lPa Ram as Long) as long Mykbhfunc = 0 if Icode < 0 then Mykbhfunc = CallNex Thookex (Hnexthookproc, Icode, WParam, LParam) Exit function End IfIf WParam = Pm_key_space then ' Reconnaissance has not been pressed to the SPACEBAR Mykbhfunc = 1 ' Add your own code, used to indicate response form1.text1.text= ' hook success! " End IfEnd Function 4. The code in form is simple:Private Sub Form_Load () calls Enablekbdhookend sub Private Sub Form_Unload (Cancel as Integer) call Unhookkbdend sub Finish Workers! Now, when you play the SPACEBAR in form form, you will respond to the code you wrote in the Mykbhfunc function.
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.