2010-06-08 3 views
1

J'ai un problème frustrant avec l'enregistrement du crochet global. J'utilise cette classe dans mon application Windows:Crochet de clavier bas niveau global gel dans C# .net 3.5

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

class InterceptKeys 
{ 
    private const int WH_KEYBOARD_LL = 13; 
    private const int WM_KEYDOWN = 0x0100; 
    private static LowLevelKeyboardProc _proc = HookCallback; 
    private static IntPtr _hookID = IntPtr.Zero; 

    public static void Hook() 
    { 
     _hookID = SetHook(_proc); 
    } 

    private static IntPtr SetHook(LowLevelKeyboardProc proc) 
    { 
     using (Process curProcess = Process.GetCurrentProcess()) 
     using (ProcessModule curModule = curProcess.MainModule) 
     { 
      return SetWindowsHookEx(WH_KEYBOARD_LL, proc, 
       GetModuleHandle(curModule.ModuleName), 0); 
     } 
    } 

    private delegate IntPtr LowLevelKeyboardProc(
     int nCode, IntPtr wParam, IntPtr lParam); 

    private static IntPtr HookCallback(
     int nCode, IntPtr wParam, IntPtr lParam) 
    { 
     if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) 
     { 
      MessageBox.Show("Test"); 
     } 
     return CallNextHookEx(_hookID, nCode, wParam, lParam); 
    } 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr SetWindowsHookEx(int idHook, 
     LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool UnhookWindowsHookEx(IntPtr hhk); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, 
     IntPtr wParam, IntPtr lParam); 

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

est ici Program.cs

namespace TestHooks 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      InterceptKeys.Hook(); 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 

    } 
} 

Tout fonctionne (MessageBox apparaît) mais les fenêtres se bloque pendant un moment et émet un signal sonore retentit. Le problème ne se produit pas sur Windows 7 uniquement sur Vista. Des suggestions s'il vous plaît?

Répondre

1

Vous ne pouvez pas utiliser MessageBox dans votre code. Il bloque l'exécution, le clavier s'éteint. Utilisez Console.WriteLine() par exemple, la sortie va à la fenêtre Visual Studio Output.

Attention, ce code ne fonctionnera plus sur .NET 4.0, GetModuleHandle() retournera NULL.

+0

merci beaucoup. J'utilise maintenant BeginInvoke pour déclencher des événements afin que je puisse gérer les touches pressées ... –

Questions connexes