[DllImport("kernel32")] public static extern int GetCurrentThreadId();
[DllImport( "user32", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern int SetWindowsHookEx( HookType idHook, HOOKPROC lpfn, int hmod, int dwThreadId);
public enum HookType { WH_KEYBOARD = 2 }
public delegate int HOOKPROC(int nCode, int wParam, int lParam);
public void SetHook() { // set the keyboard hook SetWindowsHookEx(HookType.WH_KEYBOARD, new HOOKPROC(this.MyKeyboardProc), 0, GetCurrentThreadId()); }
public int MyKeyboardProc(int nCode, int wParam, int lParam) { //在這裡放置你的處理代碼 return 0; } } 使用方法 可以在Form的構造函數裡放入 Win32Hook hook = new Win32Hook(); hook.SetHook();