c# 使用hook來監控滑鼠鍵盤事件的範例程式碼

來源:互聯網
上載者:User

標籤:

如果這個程式在10幾年前,QQ剛剛興起的時候,有了這個代碼,就可實現盜號了.

當然使用鉤子我們更多的是實現"全域快速鍵"的需求.

比如 程式最小化隱藏後要"某快速鍵"來啟動它.

鉤子(hook),通俗的講,她可以捕獲到你的鍵盤和滑鼠的相關操作訊息.

 

關於hook的相關代碼網上一搜一籮筐,這是整理起來比較完善和使用最方便的.

    //Declare wrapper managed POINT class.    [StructLayout(LayoutKind.Sequential)]    public class POINT    {        public int x;        public int y;    }    //Declare wrapper managed MouseHookStruct class.    [StructLayout(LayoutKind.Sequential)]    public class MouseHookStruct    {        public POINT pt;        public int hwnd;        public int wHitTestCode;        public int dwExtraInfo;    }    //Declare wrapper managed KeyboardHookStruct class.    [StructLayout(LayoutKind.Sequential)]    public class KeyboardHookStruct    {        public int vkCode; //Specifies a virtual-key code. The code must be a value in the range 1 to 254.         public int scanCode; // Specifies a hardware scan code for the key.         public int flags; // Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.        public int time; // Specifies the time stamp for this message.        public int dwExtraInfo; // Specifies extra information associated with the message.     }        public class GlobalHook    {        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);        public delegate int GlobalHookProc(int nCode, Int32 wParam, IntPtr lParam);        public GlobalHook()        {            //Start();        }        ~GlobalHook()        {            Stop();        }        public event MouseEventHandler OnMouseActivity;        public event KeyEventHandler KeyDown;        public event KeyPressEventHandler KeyPress;        public event KeyEventHandler KeyUp;        /// <summary>        /// 定義滑鼠鉤子控制代碼.        /// </summary>        static int _hMouseHook = 0;        /// <summary>        /// 定義鍵盤鉤子控制代碼        /// </summary>        static int _hKeyboardHook = 0;        public int HMouseHook        {            get { return _hMouseHook; }        }        public int HKeyboardHook        {            get { return _hKeyboardHook; }        }                       /// <summary>        /// 滑鼠鉤子常量(from Microsoft SDK  Winuser.h )        /// </summary>        public const int WH_MOUSE_LL = 14;         /// <summary>        /// 鍵盤鉤子常量(from Microsoft SDK  Winuser.h )        /// </summary>        public const int WH_KEYBOARD_LL = 13;        /// <summary>        /// 定義滑鼠處理過程的委派物件        /// </summary>        GlobalHookProc MouseHookProcedure;        /// <summary>        /// 鍵盤處理過程的委派物件        /// </summary>        GlobalHookProc KeyboardHookProcedure;        //匯入window 鉤子擴充方法匯入        /// <summary>        /// 安裝鉤子方法        /// </summary>        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]        public static extern int SetWindowsHookEx(int idHook, GlobalHookProc lpfn,IntPtr hInstance, int threadId);                        /// <summary>        /// 卸載鉤子方法        /// </summary>        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]        public static extern bool UnhookWindowsHookEx(int idHook);        //Import for CallNextHookEx.        /// <summary>        /// 使用這個函數鉤資訊傳遞給鏈中的下一個鉤子過程。        /// </summary>        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);        public bool Start()        {            // install Mouse hook             if (_hMouseHook == 0)            {                // Create an instance of HookProc.                MouseHookProcedure = new GlobalHookProc(MouseHookProc);                try                {                    _hMouseHook = SetWindowsHookEx(WH_MOUSE_LL,                        MouseHookProcedure,                        Marshal.GetHINSTANCE(                        Assembly.GetExecutingAssembly().GetModules()[0]),                        0);                }                catch (Exception err)                { }                //如果安裝滑鼠鉤子失敗                if (_hMouseHook == 0)                {                    Stop();                    return false;                    //throw new Exception("SetWindowsHookEx failed.");                }            }            //安裝鍵盤鉤子            if (_hKeyboardHook == 0)            {                KeyboardHookProcedure = new GlobalHookProc(KeyboardHookProc);                try                {                    _hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,                        KeyboardHookProcedure,                        Marshal.GetHINSTANCE(                        Assembly.GetExecutingAssembly().GetModules()[0]),                        0);                }                catch (Exception err2)                { }                //如果安裝鍵盤鉤子失敗                if (_hKeyboardHook == 0)                {                                        Stop();                    return false;                    //throw new Exception("SetWindowsHookEx ist failed.");                }            }            return true;        }        public void Stop()        {            bool retMouse = true;            bool retKeyboard = true;            if (_hMouseHook != 0)            {                retMouse = UnhookWindowsHookEx(_hMouseHook);                _hMouseHook = 0;            }            if (_hKeyboardHook != 0)            {                retKeyboard = UnhookWindowsHookEx(_hKeyboardHook);                _hKeyboardHook = 0;            }            //If UnhookWindowsHookEx fails.            if (!(retMouse && retKeyboard))            {                //throw new Exception("UnhookWindowsHookEx ist failed.");            }                        }        /// <summary>        /// 卸載hook,如果進程強制結束,記錄上次鉤子id,並把根據鉤子id來卸載它        /// </summary>        public void Stop(int hMouseHook, int hKeyboardHook)        {            if (hMouseHook != 0)            {                UnhookWindowsHookEx(hMouseHook);            }            if (hKeyboardHook != 0)            {                UnhookWindowsHookEx(hKeyboardHook);            }        }        private const int WM_MOUSEMOVE = 0x200;        private const int WM_LBUTTONDOWN = 0x201;        private const int WM_RBUTTONDOWN = 0x204;        private const int WM_MBUTTONDOWN = 0x207;        private const int WM_LBUTTONUP = 0x202;        private const int WM_RBUTTONUP = 0x205;        private const int WM_MBUTTONUP = 0x208;        private const int WM_LBUTTONDBLCLK = 0x203;        private const int WM_RBUTTONDBLCLK = 0x206;        private const int WM_MBUTTONDBLCLK = 0x209;        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)        {            if ((nCode >= 0) && (OnMouseActivity != null))            {                MouseButtons button = MouseButtons.None;                switch (wParam)                {                    case WM_LBUTTONDOWN:    //左鍵按下                        //case WM_LBUTTONUP:    //右鍵按下                        //case WM_LBUTTONDBLCLK:   //同步選取                        button = MouseButtons.Left;                        break;                    case WM_RBUTTONDOWN:                        //case WM_RBUTTONUP:                         //case WM_RBUTTONDBLCLK:                         button = MouseButtons.Right;                        break;                }                int clickCount = 0;                if (button != MouseButtons.None)                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK)                        clickCount = 2;                    else clickCount = 1;                //Marshall the data from callback.                MouseHookStruct MyMouseHookStruct =                    (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));                MouseEventArgs e = new MouseEventArgs(                    button,                    clickCount,                    MyMouseHookStruct.pt.x,                    MyMouseHookStruct.pt.y,                    0);                OnMouseActivity(this, e);            }            return CallNextHookEx(_hMouseHook, nCode, wParam, lParam);        }        //The ToAscii function translates the specified virtual-key code and keyboard state to the corresponding character or characters. The function translates the code using the input language and physical keyboard layout identified by the keyboard layout handle.        [DllImport("user32")]        public static extern int ToAscii(int uVirtKey, //[in] Specifies the virtual-key code to be translated.             int uScanCode, // [in] Specifies the hardware scan code of the key to be translated. The high-order bit of this value is set if the key is up (not pressed).             byte[] lpbKeyState, // [in] Pointer to a 256-byte array that contains the current keyboard state. Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down (pressed). The low bit, if set, indicates that the key is toggled on. In this function, only the toggle bit of the CAPS LOCK key is relevant. The toggle state of the NUM LOCK and SCROLL LOCK keys is ignored.            byte[] lpwTransKey, // [out] Pointer to the buffer that receives the translated character or characters.             int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise.         //The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer.         [DllImport("user32")]        public static extern int GetKeyboardState(byte[] pbKeyState);        private const int WM_KEYDOWN = 0x100;        private const int WM_KEYUP = 0x101;        private const int WM_SYSKEYDOWN = 0x104;        private const int WM_SYSKEYUP = 0x105;        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)        {            // it was ok and someone listens to events            if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))            {                KeyboardHookStruct MyKeyboardHookStruct =                    (KeyboardHookStruct)Marshal.PtrToStructure(lParam,                    typeof(KeyboardHookStruct));                // raise KeyDown                if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))                {                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;                    KeyEventArgs e = new KeyEventArgs(keyData);                    KeyDown(this, e);                }                // raise KeyPress                if (KeyPress != null && wParam == WM_KEYDOWN)                {                    byte[] keyState = new byte[256];                    GetKeyboardState(keyState);                    byte[] inBuffer = new byte[2];                    if (ToAscii(MyKeyboardHookStruct.vkCode,                        MyKeyboardHookStruct.scanCode,                        keyState,                        inBuffer,                        MyKeyboardHookStruct.flags) == 1)                    {                        KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);                        KeyPress(this, e);                    }                }                // raise KeyUp                if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))                {                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;                    KeyEventArgs e = new KeyEventArgs(keyData);                    KeyUp(this, e);                }            }            return CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam);        }    }

  


注意:
如果運行中出現SetWindowsHookEx的傳回值為0,這是因為.net 偵錯模式的問題,具體的做法是禁用宿主進程,在 Visual Studio 中開啟項目。
在“項目”菜單上單擊“屬性”。
單擊“調試”選項卡。
清除“啟用 Visual Studio 宿主進程(啟用windows承載進程)”複選框 或 勾選啟用Unmanaged 程式碼調試

使用樣本:

      

/// <summary>        /// 聲明一個hook對象        /// </summary>        GlobalHook hook;        private void Form1_Load(object sender, EventArgs e)        {            btnInstallHook.Enabled = true;            btnUnInstall.Enabled = false;            //初始化鉤子物件            if (hook == null)            {                hook = new GlobalHook();                hook.KeyDown += new KeyEventHandler(hook_KeyDown);                hook.KeyPress += new KeyPressEventHandler(hook_KeyPress);                hook.KeyUp += new KeyEventHandler(hook_KeyUp);                hook.OnMouseActivity += new MouseEventHandler(hook_OnMouseActivity);            }        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            if (btnUnInstall.Enabled == true)            {                hook.Stop();            }        }        private void btnInstallHook_Click(object sender, EventArgs e)        {            if (btnInstallHook.Enabled == true)            {                bool r = hook.Start();                if (r)                {                    btnInstallHook.Enabled = false;                    btnUnInstall.Enabled = true;                    MessageBox.Show("安裝鉤子成功!");                }                else                {                    MessageBox.Show("安裝鉤子失敗!");                }            }        }        private void btnUnInstall_Click(object sender, EventArgs e)        {            if (btnUnInstall.Enabled == true)            {                hook.Stop();                btnUnInstall.Enabled = false;                btnInstallHook.Enabled = true;                MessageBox.Show("卸載鉤子成功!");            }        }        /// <summary>        /// 滑鼠移動事件        /// </summary>        void hook_OnMouseActivity(object sender, MouseEventArgs e)        {            lbMouseState.Text = "X:" + e.X + " Y:" + e.Y;        }        /// <summary>        /// 鍵盤抬起        /// </summary>        void hook_KeyUp(object sender, KeyEventArgs e)        {            lbKeyState.Text = "鍵盤抬起, " + e.KeyData.ToString() + " 鍵碼:" + e.KeyValue;        }        /// <summary>        /// 鍵盤輸入        /// </summary>        void hook_KeyPress(object sender, KeyPressEventArgs e)        { }        /// <summary>        /// 鍵盤按下        /// </summary>        void hook_KeyDown(object sender, KeyEventArgs e)        {            lbKeyState.Text = "鍵盤按下, " + e.KeyData.ToString() + " 鍵碼:" + e.KeyValue;        }

 

 如果想捕獲滑鼠按下抬起的事件,可以修改  GlobalHook 類的 MouseHookProc方法相關代碼

 

 範例程式碼下載 

 

c# 使用hook來監控滑鼠鍵盤事件的範例程式碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.