2009-11-17 29 views
0

J'utilise SetWindowsHookEx() pour créer un hook clavier. La création semble avoir du succès mais la procédure qui est enregistrée n'est jamais appelée. Y a-t-il quelque chose que je fais de mal?SetWindowsHookEx WH_KEYBOARD_LL n'obtenant pas d'événements

#region Windows API Functions Declarations 

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); 

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
private static extern bool UnhookWindowsHookEx(int idHook); 

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
private static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam); 

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
private static extern IntPtr GetModuleHandle(string lpModuleName); 

#endregion 

=

private void CreateHook() 
{ 
    int id_hook = (int)HookType.WH_KEYBOARD_LL; 
    HookProc lpfn = new HookProc(this.KeyboardHookProc); 

    using (ProcessModule curModule = Process.GetCurrentProcess().MainModule) 
     hHook = SetWindowsHookEx(id_hook, lpfn, GetModuleHandle(curModule.ModuleName), 0); 

    if (hHook == 0) 
     throw new Exception("could not start monitoring mouse events"); 
} 

=

private int KeyboardHookProc(int code, IntPtr wParam, IntPtr lParam) 
{ 
    if (code >= 0) 
     Console.WriteLine((Keys)wParam.ToInt32()); 
    return CallNextHookEx(0, code, wParam, lParam); 
} 

=

Répondre

6

Votre déclaration P/Invoke sont mal, vous utilisez int où IntPtr est nécessaire et mélanger idHook et hHook . Après avoir modifié votre code, cela a fonctionné:

IntPtr hHook; 
private delegate IntPtr HookProc(int nCode, IntPtr wp, IntPtr lp); 
HookProc lpfn; 

private IntPtr KeyboardHookProc(int code, IntPtr wParam, IntPtr lParam) { 
    if (code >= 0) 
    Console.WriteLine((Keys)wParam.ToInt32()); 
    return CallNextHookEx(hHook, code, wParam, lParam); 
} 

private void CreateHook() { 
    int id_hook = 13; 
    lpfn = new HookProc(this.KeyboardHookProc); 
    using (ProcessModule curModule = Process.GetCurrentProcess().MainModule) 
    hHook = SetWindowsHookEx(id_hook, lpfn, GetModuleHandle(curModule.ModuleName), 0); 
    if (hHook == IntPtr.Zero) 
    throw new Exception("could not start monitoring mouse events"); 
} 
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); 
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
private static extern bool UnhookWindowsHookEx(IntPtr hHook); 
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
private static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam); 
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
private static extern IntPtr GetModuleHandle(string lpModuleName); 
+0

Les fonctions semblent faire quelque chose parce que quand la fonction attache à la boucle du clavier, l'entrée du clavier sera retardée pendant un certain temps, et lors de la fixation à la boucle de la souris, l'entrée de la souris sera lent pendant un certain temps. Mais le KeyboardHookProc ne semble pas être touché. J'utilise .NET 3.5 en passant. – Nippysaurus