Hello all, HEre is my dilema. I have an application that will be running at the same time as another and what i want to do is to capture certain key combinations namely Ctrl + 1 through 0 and Alt + 1 through 0 and send them to my application running in the background instead of the current application. Through my searching I hav found many examples of Keyboard hooking that should do this, however I can't seen to get an to work. This is the class I am using to hook, but when i runs the chechHook function it says not hooked. Can anyone help me with what may be wrong? Thanks in advance

VB Code:
  1. Imports System.Runtime.InteropServices
  2. Imports System.Reflection
  3. Imports System.Drawing
  4. Imports System.Threading
  5. Module keyHook
  6.  
  7.     Public Declare Function UnhookWindowsHookEx Lib "user32" _
  8.       (ByVal hHook As Integer) As Integer
  9.  
  10.     Public Declare Function SetWindowsHookEx Lib "user32" _
  11.       Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
  12.       ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
  13.       ByVal dwThreadId As Integer) As Integer
  14.  
  15.     Private Declare Function GetAsyncKeyState Lib "user32" _
  16.       (ByVal vKey As Integer) As Integer
  17.  
  18.     Private Declare Function CallNextHookEx Lib "user32" _
  19.       (ByVal hHook As Integer, _
  20.       ByVal nCode As Integer, _
  21.       ByVal wParam As Integer, _
  22.       ByVal lParam As KBDLLHOOKSTRUCT) As Integer
  23.  
  24.     Public Structure KBDLLHOOKSTRUCT
  25.         Public vkCode As Integer
  26.         Public scanCode As Integer
  27.         Public flags As Integer
  28.         Public time As Integer
  29.         Public dwExtraInfo As Integer
  30.     End Structure
  31.  
  32.     ' Low-Level Keyboard Constants
  33.     Private Const HC_ACTION As Integer = 0
  34.     Private Const LLKHF_EXTENDED As Integer = &H1
  35.     Private Const LLKHF_INJECTED As Integer = &H10
  36.     Private Const LLKHF_ALTDOWN As Integer = &H20
  37.     Private Const LLKHF_UP As Integer = &H80
  38.  
  39.     ' Virtual Keys
  40.     Public Const VK_TAB = &H9
  41.     Public Const VK_CONTROL = &H11
  42.     Public Const VK_ESCAPE = &H1B
  43.     Public Const VK_DELETE = &H2E
  44.  
  45.     Private Const WH_KEYBOARD_LL As Integer = 13&
  46.     Public KeyboardHandle As Integer
  47.  
  48.  
  49.     ' Implement this function to block as many
  50.     ' key combinations as you'd like
  51.     Public Function IsHooked( _
  52.       ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
  53.  
  54.         Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
  55.         Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
  56.         Debug.WriteLine(Hookstruct.vkCode = VK_TAB)
  57.  
  58.         If (Hookstruct.vkCode = VK_ESCAPE) And _
  59.           CBool(GetAsyncKeyState(VK_CONTROL) _
  60.           And &H8000) Then
  61.  
  62.             Call HookedState("Ctrl + Esc blocked")
  63.             Return True
  64.         End If
  65.  
  66.         If (Hookstruct.vkCode = VK_TAB) And _
  67.           CBool(Hookstruct.flags And _
  68.           LLKHF_ALTDOWN) Then
  69.  
  70.             Call HookedState("Alt + Tab blockd")
  71.             Return True
  72.         End If
  73.  
  74.         If (Hookstruct.vkCode = VK_ESCAPE) And _
  75.           CBool(Hookstruct.flags And _
  76.             LLKHF_ALTDOWN) Then
  77.  
  78.             Call HookedState("Alt + Escape blocked")
  79.             Return True
  80.         End If
  81.  
  82.         Return False
  83.     End Function
  84.  
  85.     Private Sub HookedState(ByVal Text As String)
  86.         Debug.WriteLine(Text)
  87.     End Sub
  88.  
  89.     Public Function KeyboardCallback(ByVal Code As Integer, _
  90.       ByVal wParam As Integer, _
  91.       ByRef lParam As KBDLLHOOKSTRUCT) As Integer
  92.  
  93.         If (Code = HC_ACTION) Then
  94.             Debug.WriteLine("Calling IsHooked")
  95.  
  96.             If (IsHooked(lParam)) Then
  97.                 Return 1
  98.             End If
  99.  
  100.         End If
  101.  
  102.         Return CallNextHookEx(KeyboardHandle, _
  103.           Code, wParam, lParam)
  104.  
  105.     End Function
  106.  
  107.  
  108.     Public Delegate Function KeyboardHookDelegate( _
  109.       ByVal Code As Integer, _
  110.       ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
  111.                    As Integer
  112.  
  113.     <MarshalAs(UnmanagedType.FunctionPtr)> _
  114.     Private callback As KeyboardHookDelegate
  115.  
  116.     Public Sub HookKeyboard()
  117.         callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
  118.  
  119.         KeyboardHandle = SetWindowsHookEx( _
  120.           WH_KEYBOARD_LL, callback, _
  121.           Marshal.GetHINSTANCE( _
  122.           [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
  123.  
  124.         Call CheckHooked()
  125.     End Sub
  126.  
  127.     Public Sub CheckHooked()
  128.         If (Hooked()) Then
  129.             MsgBox("Keyboard hooked")
  130.         Else
  131.             MsgBox("Keyboard hook failed: " & Err.LastDllError)
  132.         End If
  133.     End Sub
  134.  
  135.     Private Function Hooked()
  136.         Hooked = KeyboardHandle <> 0
  137.     End Function
  138.  
  139.     Public Sub UnhookKeyboard()
  140.         If (Hooked()) Then
  141.             Call UnhookWindowsHookEx(KeyboardHandle)
  142.         End If
  143.     End Sub
  144.  
  145. End Module