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

Int32 Dim handled As Boolean = False If nCode = 0 Then Dim MyKeyboardHookStruct As KeyboardHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(KeyboardHookStruct)), KeyboardHookStruct) '激活KeyDown If wParam = WM_KEYDOWN OrElse wParam = WM_SYSKEYDOWN Then Dim e As New KeyEventArgs(MyKeyboardHookStruct.vkCode) RaiseEvent KeyDown(Me, e) '激活事件 handled = handled Or e.Handled: End If '激活KeyUp If wParam = WM_KEYUP OrElse wParam = WM_SYSKEYUP Then Dim e As New KeyEventArgs(MyKeyboardHookStruct.vkCode) RaiseEvent KeyUp(Me, e) handled = handled Or e.Handled: End If '激活KeyPress If wParam = WM_KEYDOWN Then Dim isDownShift As Boolean = (GetKeyState(VK_SHIFT)) Dim isDownCapslock As Boolean = (GetKeyState(VK_CAPITAL)) Dim keyState(256) As Byte GetKeyboardState(keyState) Dim inBuffer(2) As Byte If ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.ScanCode, keyState, inBuffer, MyKeyboardHookStruct.Flags) = 1 Then Dim key As Char = Chr(inBuffer(0)) If isDownCapslock Xor isDownShift And Char.IsLetter(key) Then key = Char.ToUpper(key) Dim e As New KeyPressEventArgs(key) RaiseEvent KeyPress(Me, e) handled = handled Or e.Handled: End If: End If '取消或者激活下一个钩子 If handled Then Return 1 Else Return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam): End If: End Function Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As Int32, ByVal lParam As IntPtr) As Int32 If nCode = 0 Then Dim mouseHookStruct As MouseLLHookStruct = Marshal.PtrToStructure(lParam, GetType(MouseLLHookStruct)) Dim moubut As MouseButtons = MouseButtons.None '鼠标按键 Dim mouseDelta As Integer = 0 '滚轮值 Dim mousePoint As Point = New Point(0, 0) Select Case wParam Case WM_LBUTTONDOWN: moubut = MouseButtons.Left Case WM_RBUTTONDOWN: moubut = MouseButtons.Right Case WM_MBUTTONDOWN: moubut = MouseButtons.Middle Case WM_MOUSEWHEEL Dim int As Integer = mouseHookStruct.MouseData ') And &HFFFF If int = Short.MaxValue Then mouseDelta = int - 65536 Else mouseDelta = int Case WM_MOUSEMOVE If mouseHookStruct.X >= 0 And mouseHookStruct.X <= 99999999 Then mousePoint.X = mouseHookStruct.X If

相关教程