| 发表于:2007-03-08 00:14:552楼 得分:100 |
谁说不支持了, imports system.runtime.interopservices public class myhook private m_ikeyhandle as integer = 0 private m_clshookcallback as hookcallback private const hc_action as integer = 0 private const wm_key_enter as integer = &hd private const wh_keyboard_ll = 13 private const wm_keydown as integer = &h100 private const wm_keyup as integer = &h101 private const wm_syskeydown as integer = &h104 private const wm_syskeyup as integer = &h105 private fix_cocd as hookcallback '定义引用变量防止callbackoncollecteddelegate错误的产生 public delegate function hookcallback(byval ncode as integer, byval wparam as intptr, byval lparam as intptr) as integer public declare function getmodulehandle lib "kernel32.dll " alias "getmodulehandlea " (byval modulename as string) as intptr <dllimport( "user32.dll ", charset:=charset.auto, callingconvention:=callingconvention.stdcall)> _ public overloads shared function setwindowshookex(byval idhook as integer, byval hookproc as hookcallback, byval hinstance as intptr, byval wparam as integer) as integer end function <dllimport( "user32.dll ", charset:=charset.auto, callingconvention:=callingconvention.stdcall)> _ public overloads shared function callnexthookex(byval idhook as integer, byval ncode as integer, byval wparam as intptr, byval lparam as intptr) as integer end function <dllimport( "user32.dll ", charset:=charset.auto, callingconvention:=callingconvention.stdcall)> _ public overloads shared function unhookwindowshookex(byval idhook as integer) as boolean end function 'public event keydown as system.windows.forms.keyeventhandler 'public event keypress as system.windows.forms.keypresseventhandler public event keyup as system.windows.forms.keyeventhandler public structure keyboardhookstruct ' specifies a virtual-key code. the code must be a value in the range 1 to 254. public vkcode as integer 'specifies a hardware scan code for the key. public scancode as integer 'specifies the extended-key flag, event-injected flag, context code, and transition-state flag. public flags as integer 'specifies the time stamp for this message. public time as integer 'specifies extra information associated with the message. public dwextrainfo as integer end structure private function keyboardproc(byval ncode as integer, byval wparam as intptr, byval lparam as intptr) as integer dim newptr as keyboardhookstruct = ctype(marshal.ptrtostructure(lparam, newptr.gettype()), keyboardhookstruct) if (ncode = hc_action) then if wparam.toint32 = wm_keyup or wparam.toint32 = wm_syskeyup then if newptr.vkcode = wm_key_enter then raiseevent keyup(me, new windows.forms.keyeventargs(ctype(newptr.vkcode, windows.forms.keys))) end if end if end if return callnexthookex(m_ikeyhandle, ncode, wparam, lparam) end function public sub sethook() fix_cocd = new hookcallback(addressof keyboardproc) m_ikeyhandle = setwindowshookex(wh_keyboard_ll, fix_cocd, getmodulehandle(process.getcurrentprocess().mainmodule.modulename), 0) if m_ikeyhandle = 0 then throw new system.exception( "hook failed. ") else end if end sub public sub unhook() if (m_ikeyhandle <> 0) then unhookwindowshookex(m_ikeyhandle) end sub end class | | |
|