Pouknouki

VB.NET Keyboard Hook

Feb 26th, 2013
399
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class KeyboardHook
  4.  
  5.     <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
  6.     Private Overloads Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KBDLLHookProc, ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
  7.     End Function
  8.     <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
  9.     Private Overloads Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  10.     End Function
  11.     <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
  12.     Private Overloads Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
  13.     End Function
  14.  
  15.     <StructLayout(LayoutKind.Sequential)> _
  16.     Private Structure KBDLLHOOKSTRUCT
  17.         Public vkCode As UInt32
  18.         Public scanCode As UInt32
  19.         Public flags As KBDLLHOOKSTRUCTFlags
  20.         Public time As UInt32
  21.         Public dwExtraInfo As UIntPtr
  22.     End Structure
  23.  
  24.     <Flags()> _
  25.     Private Enum KBDLLHOOKSTRUCTFlags As UInt32
  26.         LLKHF_EXTENDED = &H1
  27.         LLKHF_INJECTED = &H10
  28.         LLKHF_ALTDOWN = &H20
  29.         LLKHF_UP = &H80
  30.     End Enum
  31.  
  32.     Public Shared Event KeyDown(ByVal Key As Keys)
  33.     Public Shared Event KeyUp(ByVal Key As Keys)
  34.  
  35.     Private Const WH_KEYBOARD_LL As Integer = 13
  36.     Private Const HC_ACTION As Integer = 0
  37.     Private Const WM_KEYDOWN = &H100
  38.     Private Const WM_KEYUP = &H101
  39.     Private Const WM_SYSKEYDOWN = &H104
  40.     Private Const WM_SYSKEYUP = &H105
  41.  
  42.     Private Delegate Function KBDLLHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  43.  
  44.     Private KBDLLHookProcDelegate As KBDLLHookProc = New KBDLLHookProc(AddressOf KeyboardProc)
  45.     Private HHookID As IntPtr = IntPtr.Zero
  46.  
  47.     Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  48.         If (nCode = HC_ACTION) Then
  49.             Dim struct As KBDLLHOOKSTRUCT
  50.             Select Case wParam
  51.                 Case WM_KEYDOWN, WM_SYSKEYDOWN
  52.                     RaiseEvent KeyDown(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
  53.                 Case WM_KEYUP, WM_SYSKEYUP
  54.                     RaiseEvent KeyUp(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
  55.             End Select
  56.         End If
  57.         Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
  58.     End Function
  59.  
  60.     Public Sub New()
  61.         HHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KBDLLHookProcDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
  62.         If HHookID = IntPtr.Zero Then
  63.             Throw New Exception("Could not set keyboard hook")
  64.         End If
  65.     End Sub
  66.  
  67.     Protected Overrides Sub Finalize()
  68.         If Not HHookID = IntPtr.Zero Then
  69.             UnhookWindowsHookEx(HHookID)
  70.         End If
  71.         MyBase.Finalize()
  72.     End Sub
  73.  
  74. End Class
RAW Paste Data