趁空閒的時間,對鍵盤鉤子進行了學習,通過C#這門語言來設計和實現:下面是我設計的類圖:
鍵盤鉤子包括兩類:全局鉤子和私有鉤子,這裡我分成兩個類來設計:
public delegate int HOOKPROC(int nCode, int wParam, int lParam);
public enum HookType
{
WH_KEYBOARD = 2,//私有鉤子
WH_KEYBOARD_LL = 13//全局鉤子
}
Hook基類實現
public abstract class hook
{
//設置鉤子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(HookType idHook, HOOKPROC lpfn, IntPtr hInstance, int threadId);
//抽調鉤子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("kernel32")]
public static extern int GetCurrentThreadId();
/// <summary>
/// 鉤子處理委托
/// </summary>
public HOOKPROC proc;
/// <summary>
/// 鉤子類型
/// </summary>
public&n