欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
如何在C#中使用全局鼠標、鍵盤(pán)Hook

如何在C#中使用全局鼠標、鍵盤(pán)Hook

  今天,有個(gè)同事問(wèn)我,怎樣在C#中使用全局鉤子?以前寫(xiě)的全局鉤子都是用unmanaged C或C++寫(xiě)個(gè)DLL來(lái)實(shí)現,可大家都知道,C#是基于.Net Framework的,是managed,怎么實(shí)現全局鉤子呢?于是開(kāi)始到網(wǎng)上搜索,好不容易找到一篇,318804 - HOW TO: Set a Windows Hook in Visual C# .NET,里面詳細的說(shuō)明了如何使用鼠標鉤子捕獲鼠標的移動(dòng)等,可是,它只能在A(yíng)pplication里起作用,出了Application就沒(méi)用了,就是說(shuō)它還是沒(méi)有實(shí)現全局鉤子,而且文章結尾處說(shuō):“Global Hooks are not supported in the .NET Framework...”,這可怎么辦呢?

  別擔心,辦法總是有的,經(jīng)過(guò)一番摸索以后,發(fā)現WH_KEYBORAD_LL和WH_MOUSE_LL這兩個(gè)low-level的hook可以被安裝成全局的,這就好辦了,我們不妨用這兩個(gè)low-level的hook替換掉WH_KEYBORAD和WH_MOUSE,于是開(kāi)始測試。結果成功了,在C#里實(shí)現了全局鉤子。

  我們來(lái)看一下主要代碼段。示例源碼下載地址請訪(fǎng)問(wèn)我的網(wǎng)站:http://www.vczx.com/article/show.php?id=1672

  首先倒入所需要的windows函數,主要有三個(gè),SetWindowsHookEX用來(lái)安裝鉤子,UnhookWindowsHookEX用來(lái)卸載鉤子以及CallNextHookEX用來(lái)將hook信息傳遞到鏈表中下一個(gè)hook處理過(guò)程。

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

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

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

  下面是有關(guān)這兩個(gè)low-level hook在Winuser.h中的定義:

/// <summary>
        
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.
        
/// </summary> 
        private const int WH_MOUSE_LL       = 14;
        /// <summary>
        
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard  input events.
        
/// </summary> 
        private const int WH_KEYBOARD_LL    = 13;

  在安裝全局鉤子的時(shí)候,我們就要做替換了,將WH_MOUSE和WH_KEYBORAD分別換成WH_MOUSE_LL和WH_KEYBORAD_LL:

//install hook
                hMouseHook = SetWindowsHookEx(
                    WH_MOUSE_LL, //原來(lái)是WH_MOUSE
                    MouseHookProcedure,
                    Marshal.GetHINSTANCE(
                        Assembly.GetExecutingAssembly().GetModules()[0]),
                    0);

//install hook
                hKeyboardHook = SetWindowsHookEx(
                    WH_KEYBOARD_LL, //原來(lái)是WH_KEYBORAD
                    KeyboardHookProcedure,
                    Marshal.GetHINSTANCE(
                    Assembly.GetExecutingAssembly().GetModules()[0]),
                    0);
  這樣替換了之后,我們就可以實(shí)現全局鉤子了,而且,不需要寫(xiě)DLL??匆幌鲁绦蜻\行情況:



  下面是關(guān)于鼠標和鍵盤(pán)的兩個(gè)Callback函數:

private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            // if ok and someone listens to our events
            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button = MouseButtons.None;
                short mouseDelta = 0;
                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;
                    case WM_MOUSEWHEEL:
                        //If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta. 
                        
//One wheel click is defined as WHEEL_DELTA, which is 120. 
                        
//(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                        mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);
                        //TODO: X BUTTONS (I havent them so was unable to test)
                        
//If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, 
                        
//or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, 
                        
//and the low-order word is reserved. This value can be one or more of the following values. 
                        
//Otherwise, mouseData is not used. 
                        break;
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;
                    else clickCount = 1;

                //generate event 
                 MouseEventArgs e = new MouseEventArgs(
                                                    button,
                                                    clickCount,
                                                    mouseHookStruct.pt.x,
                                                    mouseHookStruct.pt.y,
                                                    mouseDelta);
                //raise it
                OnMouseActivity(this, e);
            }
            //call next hook
            return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
        }
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;
            //it was ok and someone listens to events
            if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
            {
                //read structure KeyboardHookStruct at lParam
                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);
                    handled = handled || e.Handled;
                }

                // raise KeyPress
                if (KeyPress != null && wParam == WM_KEYDOWN)
                {
                    bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (ToAscii(MyKeyboardHookStruct.vkCode,
                              MyKeyboardHookStruct.scanCode,
                              keyState,
                              inBuffer,
                              MyKeyboardHookStruct.flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);
                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        KeyPress(this, e);
                        handled = handled || e.Handled;
                    }
                }

                // raise KeyUp
                if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keyData);
                    KeyUp(this, e);
                    handled = handled || e.Handled;
                }

            }

            //if event handled in application do not handoff to other listeners
            if (handled)
                return 1;
            else
                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }

參考資料:MSDN
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
淺談HOOK技術(shù)在VC編程中的應用—編程愛(ài)好者網(wǎng)站 programfan.com
[鉤子技術(shù)]簡(jiǎn)單的鼠標鉤子
Windows消息攔截技術(shù)的應用
C#實(shí)現鼠標鍵盤(pán)的監控和屏蔽 - 技術(shù)應用 - 豆豆網(wǎng)
Windows API 教程(七) hook 鉤子監聽(tīng)
屏蔽Win鍵、Alt Tab鍵的響應
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久