VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.net / IO / 获取鼠标键盘事件的钩子函数(3)

If InstallAll Then StartHook(True, True): End Sub '创建一个全局鼠标键盘钩子,并决定安装钩子的类型 ''' <param name="InstallKeyboard">挂钩键盘消息</param> ''' <param name="InstallMouse">挂钩鼠标消息</param> Sub New(ByVal InstallKeyboard As Boolean, ByVal InstallMouse As Boolean) StartHook(InstallKeyboard, InstallMouse): End Sub ' 开始安装系统钩子 ''' <param name="InstallKeyboardHook">挂钩键盘消息</param> ''' <param name="InstallMouseHook">挂钩鼠标消息</param> Public Sub StartHook(Optional ByVal InstallKeyboardHook As Boolean = True, Optional ByVal InstallMouseHook As Boolean = True) '注册键盘钩子 If InstallKeyboardHook AndAlso hKeyboardHook = 0 Then KeyboardHookProcedure = New HookProc(AddressOf KeyboardHookProc) hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0) If hKeyboardHook = 0 Then: UnHook(True, False): myeor = "键盘钩子失败:" & Marshal.GetLastWin32Error: Else: myeor = "键盘钩子成功:" & hKeyboardHook.ToString: End If: End If If InstallMouseHook AndAlso hMouseHook = 0 Then MouseHookProcedure = New HookProc(AddressOf MouseHookProc): hMouseHook = SetWindowsHookExA(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0) If hMouseHook = 0 Then: UnHook(False, True): Throw New Win32Exception(Marshal.GetLastWin32Error): End If: End If: End Sub ' 立刻卸载系统钩子 ''' <param name="UninstallKeyboardHook">卸载键盘钩子</param> ''' <param name="ThrowExceptions">是否报告错误</param> Public Sub UnHook(Optional ByVal UninstallKeyboardHook As Boolean = True, Optional ByVal UninstallMouseHook As Boolean = True, Optional ByVal ThrowExceptions As Boolean = False) '卸载键盘钩子 If hKeyboardHook <> 0 AndAlso UninstallKeyboardHook Then Dim retKeyboard As Int32 = UnhookWindowsHookEx(hKeyboardHook): hKeyboardHook = 0 If ThrowExceptions AndAlso retKeyboard = 0 Then: Throw New Win32Exception(Marshal.GetLastWin32Error): End If: End If If hMouseHook <> 0 AndAlso UninstallMouseHook Then Dim retMouse As Integer = UnhookWindowsHookEx(hMouseHook): hMouseHook = 0 If ThrowExceptions AndAlso retMouse = 0 Then: Throw New Win32Exception(Marshal.GetLastWin32Error): End If: End If: End Sub '键盘消息的委托处理代码 Private Function KeyboardHookProc(ByVal nCode As Int32, ByVal wParam As Int32, ByVal lParam As IntPtr) As

相关教程