熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中用鉤子(hook)實現的屏幕鍵盤

2013-11-13 09:41:59  來源: .NET編程 

  要實現一個屏幕鍵盤需要監聽所有鍵盤事件無論窗體是否被激活因此需要一個全局的鉤子也就
是系統范圍的鉤子

  什麼是鉤子(Hook)

  鉤子(Hook)是Windows提供的一種消息處理機制平台是指在程序正常運行中接受信息之前預先啟動的函數用來檢查和修改傳給該程序的信息(鉤子)實際上是一個處理消息的程序段通過系統調用把它掛入系統每當特定的消息發出在沒有到達目的窗口前鉤子程序就先捕獲該消息亦即鉤子函數先得到控制權這時鉤子函數即可以加工處理(改變)該消息也可以不作處理而繼續傳遞該消息還可以強制結束消息的傳遞注意安裝鉤子函數將會影響系統的性能監測系統范圍事件的系統鉤子特別明顯因為系統在處理所有的相關事件時都將調用您的鉤子函數這樣您的系統將會明顯的減慢所以應謹慎使用用完後立即卸載還有由於您可以預先截獲其它進程的消息所以一旦您的鉤子函數出了問題的話必將影響其它的進程

  鉤子的作用范圍

  一共有兩種范圍(類型)的鉤子局部的和遠程的局部鉤子僅鉤掛自己進程的事件遠程的鉤子還可以將鉤掛其它進程發生的事件遠程的鉤子又有兩種 基於線程的鉤子將捕獲其它進程中某一特定線程的事件簡言之就是可以用來觀察其它進程中的某一特定線程將發生的事件系統范圍的鉤子將捕捉系統中所有進程將發生的事件消息 

  Hook 類型

Windows共有種Hooks每一種類型的Hook可以使應用程序能夠監視不同類型的系統消息處理機制下面描述所有可以利用的Hook類型的發生時機詳細內容可以查閱MSDN這裡只介紹我們將要用到的兩種類型的鉤子

)WH_KEYBOARD_LL Hook

  WH_KEYBOARD_LL Hook監視輸入到線程消息隊列中的鍵盤消息

  ()WH_MOUSE_LL Hook

  WH_MOUSE_LL Hook監視輸入到線程消息隊列中的鼠標消息

  下面的 class 把 API 調用封裝起來以便調用

  

  

  // NativeMethodscs
using System;
using SystemRuntimeInteropServices;
using SystemDrawing;

namespace CnBlogsYouzaiScreenKeyboard {
    [StructLayout(LayoutKindSequential)]
    internal struct MOUSEINPUT {
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKindSequential)]
    internal struct KEYBDINPUT {
        public short wVk;
        public short wScan;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKindExplicit)]
    internal struct Input {
        [FieldOffset()]
        public int type;
        [FieldOffset()]
        public MOUSEINPUT mi;
        [FieldOffset()]
        public KEYBDINPUT ki;
        [FieldOffset()]
        public HARDWAREINPUT hi;
    }

    [StructLayout(LayoutKindSequential)]
    internal struct HARDWAREINPUT {
        public int uMsg;
        public short wParamL;
        public short wParamH;
    }

    internal class INPUT {
        public const int MOUSE = ;
        public const int KEYBOARD = ;
        public const int HARDWARE = ;
    }

    internal static class NativeMethods {
        [DllImport(Userdll CharSet = CharSetAuto SetLastError = false)]
        internal static extern IntPtr GetWindowLong(IntPtr hWnd int nIndex);

        [DllImport(Userdll CharSet = CharSetAuto SetLastError = false)]
        internal static extern IntPtr SetWindowLong(IntPtr hWnd int nIndex int dwNewLong);

        [DllImport(Userdll EntryPoint = SendInput CharSet = CharSetAuto)]
        internal static extern UInt SendInput(UInt nInputs Input[] pInputs Int cbSize);

        [DllImport(Kerneldll EntryPoint = GetTickCount CharSet = CharSetAuto)]
        internal static extern int GetTickCount();

        [DllImport(Userdll EntryPoint = GetKeyState CharSet = CharSetAuto)]
        internal static extern short GetKeyState(int nVirtKey);

        [DllImport(Userdll EntryPoint = SendMessage CharSet = CharSetAuto)]
        internal static extern IntPtr SendMessage(IntPtr hWnd int msg IntPtr wParam

  IntPtr lParam);
    }
}

  安裝鉤子

  使用SetWindowsHookEx函數(API函數)指定一個Hook類型自己的Hook過程是全局還是局部Hook同時給出Hook過程的進入點就可以輕松的安裝自己的Hook過程SetWindowsHookEx總是將你的Hook函數放置在Hook鏈的頂端你可以使用CallNextHookEx函數將系統消息傳遞給Hook鏈中的下一個函數

  對於某些類型的Hook系統將向該類的所有Hook函數發送消息這時Hook函數中的CallNextHookEx語句將被忽略全局(遠程鉤子)Hook函數可以攔截系統中所有線程的某個特定的消息為了安裝一個全局Hook過程必須在應用程序外建立一個DLL並將該Hook函數封裝到其中 應用程序在安裝全局Hook過程時必須先得到該DLL模塊的句柄將Dll名傳遞給LoadLibrary 函數就會得到該DLL模塊的句柄得到該句柄 後使用GetProcAddress函數可以得到Hook過程的地址最後使用SetWindowsHookEx將 Hook過程的首址嵌入相應的Hook鏈中SetWindowsHookEx傳遞一個模塊句柄它為Hook過程的進入點

  線程標識符置為該Hook過程同系統中的所有線程關聯如果是安裝局部Hook此時該Hook函數可以放置在DLL中也可以放置在應用程序的模塊段在C#中通過平台調用(前文已經介紹過)來調用API函數

  

  

      public void Start(bool installMouseHook bool installKeyboardHook) {
        if (hMouseHook == IntPtrZero && installMouseHook) {
            MouseHookProcedure = new HookProc(MouseHookProc);
            hMouseHook = SetWindowsHookEx(
                WH_MOUSE_LL
                MouseHookProcedure
                MarshalGetHINSTANCE(
                AssemblyGetExecutingAssembly()GetModules()[])
               
           );

            if (hMouseHook == IntPtrZero) {
                int errorCode = MarshalGetLastWinError();
                Stop(true false false);

                throw new WinException(errorCode);
            }
        }

        if (hKeyboardHook == IntPtrZero && installKeyboardHook) {
            KeyboardHookProcedure = new HookProc(KeyboardHookProc);
            //install hook
            hKeyboardHook = SetWindowsHookEx(
                WH_KEYBOARD_LL
                KeyboardHookProcedure
                MarshalGetHINSTANCE(
                AssemblyGetExecutingAssembly()GetModules()[])
                );
            // If SetWindowsHookEx fails
            if (hKeyboardHook == IntPtrZero) {
                // Returns the error code returned by the last
                // unmanaged function called using platform invoke
                // that has the DllImportAttributeSetLastError flag set
                int errorCode = MarshalGetLastWinError();
                //do cleanup
                Stop(false true false);
                //Initializes and throws a new instance of the
                // WinException class with the specified error
                throw new WinException(errorCode);
            }
        }
    }


使用完鉤子後要進行卸載這個可以寫在析構函數中

  

  

  
    public void Stop() {
        thisStop(true true true);
    }
   
    public void Stop(bool uninstallMouseHook bool uninstallKeyboardHook
        bool throwExceptions) {
        // if mouse hook set and must be uninstalled
        if (hMouseHook != IntPtrZero && uninstallMouseHook) {
            // uninstall hook
            bool retMouse = UnhookWindowsHookEx(hMouseHook);
            // reset invalid handle
            hMouseHook = IntPtrZero;
            // if failed and exception must be thrown
            if (retMouse == false && throwExceptions) {
                // Returns the error code returned by the last unmanaged function
                // called using platform invoke that has the DllImportAttribute
                // SetLastError flag set
                int errorCode = MarshalGetLastWinError();
                // Initializes and throws a new instance of the WinException class
                // with the specified error
                throw new WinException(errorCode);
            }
        }

        // if keyboard hook set and must be uninstalled
        if (hKeyboardHook != IntPtrZero && uninstallKeyboardHook) {
            // uninstall hook
            bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
            // reset invalid handle
            hKeyboardHook = IntPtrZero;
            // if failed and exception must be thrown
            if (retKeyboard == false && throwExceptions) {
                // Returns the error code returned by the last unmanaged function
                // called using platform invoke that has the DllImportAttribute
                // SetLastError flag set
                int errorCode = MarshalGetLastWinError();
                // Initializes and throws a new instance of the WinException class
                // with the specified error
                throw new WinException(errorCode);
            }
        }
    }


將這個文件編譯成一個dll即可在應用程序中調用通過它提供的事件便可監聽所有的鍵盤事件
但是這只能監聽鍵盤事件沒有鍵盤的情況下怎麼會有鍵盤事件?其實很簡單通過SendInput
API函數提供虛擬鍵盤代碼的調用即可模擬鍵盤輸入下面的代碼模擬一個 KeyDown 和 KeyUp 過程
把他們連接起來就是一次按鍵過程

  

  

      private void SendKeyDown(short key) {
        Input[] input = new Input[];
        input[]type = INPUTKEYBOARD;
        input[]kiwVk = key;
        input[]kitime = NativeMethodsGetTickCount();

        if (NativeMethodsSendInput((uint)inputLength input MarshalSizeOf(input[]))
            < inputLength) {
            throw new WinException(MarshalGetLastWinError());
        }
    }

    private void SendKeyUp(short key) {
        Input[] input = new Input[];
        input[]type = INPUTKEYBOARD;
        input[]kiwVk = key;
        input[]kidwFlags = KeyboardConstaintKEYEVENTF_KEYUP;
        input[]kitime = NativeMethodsGetTickCount();

        if (NativeMethodsSendInput((uint)inputLength input MarshalSizeOf(input[]))
            < inputLength) {
            throw new WinException(MarshalGetLastWinError());
        }
    }

  自己實現一個 KeyBoardButton 控件用作按鈕用 Visual Studio 或者 SharpDevelop 為屏幕鍵盤設計 UI然後
在這些 Button 的 Click 事件裡面模擬一個按鍵過程

  

  

  
    private void ButtonOnClick(object sender EventArgs e) {
        KeyboardButton btnKey = sender as KeyboardButton;
        if (btnKey == null) {
            return;
        }

        SendKeyCommand(btnKey);
    }
   
    private void SendKeyCommand(KeyboardButton keyButton) {
        short key = keyButtonVKCode;
        if (combinationVKButtonsMapContainsKey(key)) {
            if (keyButtonChecked) {
                SendKeyUp(key);
            } else {
                SendKeyDown(key);
            }
        } else {
            SendKeyDown(key);
            SendKeyUp(key);
        }
    }

  其中 combinationVKButtonsMap 是一個 IDictionary>, key 存儲的是VK_SHIFT, VK_CONTROL 等組合鍵的鍵盤碼。TW.wiNGwIt.CoM左右兩個按鈕對應同一個鍵盤碼,因此需要放在一個 List 裡。
標准鍵盤上的每一個鍵都有虛擬鍵碼( VK_CODE)與之對應。

  還有一些其他的常量,
把它寫在一個靜態 class 裡吧。

  

  

  1    // KeyboardConstaint.cs
2    internal static class KeyboardConstaint {
3        internal static readonly short VK_F1 = 0x70;
4        internal static readonly short VK_F2 = 0x71;
5        internal static readonly short VK_F3 = 0x72;
6        internal static readonly short VK_F4 = 0x73;
7        internal static readonly short VK_F5 = 0x74;
8        internal static readonly short VK_F6 = 0x75;
9        internal static readonly short VK_F7 = 0x76;
10        internal static readonly short VK_F8 = 0x77;
11        internal static readonly short VK_F9 = 0x78;
12        internal static readonly short VK_F10 = 0x79;
13        internal static readonly short VK_F11 = 0x7A;
14        internal static readonly short VK_F12 = 0x7B;
15
16        internal static readonly short VK_LEFT = 0x25;
17        internal static readonly short VK_UP = 0x26;
18        internal static readonly short VK_RIGHT = 0x27;
19        internal static readonly short VK_DOWN = 0x28;
20
21        internal static readonly short VK_NONE = 0x00;
22        internal static readonly short VK_ESCAPE = 0x1B;
23        internal static readonly short VK_EXECUTE = 0x2B;
24        internal static readonly short VK_CANCEL = 0x03;
25        internal static readonly short VK_RETURN = 0x0D;
26        internal static readonly short VK_ACCEPT = 0x1E;
27        internal static readonly short VK_BACK = 0x08;
28        internal static readonly short VK_TAB = 0x09;
29        internal static readonly short VK_DELETE = 0x2E;
30        internal static readonly short VK_CAPITAL = 0x14;
31        internal static readonly short VK_NUMLOCK = 0x90;
32        internal static readonly short VK_SPACE = 0x20;
33        internal static readonly short VK_DECIMAL = 0x6E;
34        internal static readonly short VK_SUBTRACT = 0x6D;
35
36        internal static readonly short VK_ADD = 0x6B;
37        internal static readonly short VK_DIVIDE = 0x6F;
38        internal static readonly short VK_MULTIPLY = 0x6A;
39        internal static readonly short VK_INSERT = 0x2D;
40
41        internal static readonly short VK_OEM_1 = 0xBA;  // ';:' for US
42        internal static readonly short VK_OEM_PLUS = 0xBB;  // '+' any country
43
44        internal static readonly short VK_OEM_MINUS = 0xBD;  // '-' any country
45
46        internal static readonly short VK_OEM_2 = 0xBF;  // '/?' for US
47        internal static readonly short VK_OEM_3 = 0xC0;  // '`~' for US
48        internal static readonly short VK_OEM_4 = 0xDB;  //  '[{' for US
49        internal static readonly short VK_OEM_5 = 0xDC;  //  '\|' for US
50        internal static readonly short VK_OEM_6 = 0xDD;  //  ']}' for US
51        internal static readonly short VK_OEM_7 = 0xDE;  //  ''"' for US
52        internal static readonly short VK_OEM_PERIOD = 0xBE;  // '.>' any country
53        internal static readonly short VK_OEM_COMMA = 0xBC;  // ',<' any country
54        internal static readonly short VK_SHIFT = 0x10;
55        internal static readonly short VK_CONTROL = 0x11;
56        internal static readonly short VK_MENU = 0x12;
57        internal static readonly short VK_LWIN = 0x5B;
58        internal static readonly short VK_RWIN = 0x5C;
59        internal static readonly short VK_APPS = 0x5D;
60
61        internal static readonly short VK_LSHIFT = 0xA0;
62        internal static readonly short VK_RSHIFT = 0xA1;
63        internal static readonly short VK_LCONTROL = 0xA2;
64        internal static readonly short VK_RCONTROL = 0xA3;
65        internal static readonly short VK_LMENU = 0xA4;
66        internal static readonly short VK_RMENU = 0xA5;
67
68        internal static readonly short VK_SNAPSHOT = 0x2C;
69        internal static readonly short VK_SCROLL = 0x91;
70        internal static readonly short VK_PAUSE = 0x13;
71        internal static readonly short VK_HOME = 0x24;
72
73        internal static readonly short VK_NEXT = 0x22;
74        internal static readonly short VK_PRIOR = 0x21;
75        internal static readonly short VK_END = 0x23;
76
77        internal static readonly short VK_NUMPAD0 = 0x60;
78        internal static readonly short VK_NUMPAD1 = 0x61;
79        internal static readonly short VK_NUMPAD2 = 0x62;
80        internal static readonly short VK_NUMPAD3 = 0x63;
81        internal static readonly short VK_NUMPAD4 = 0x64;
82        internal static readonly short VK_NUMPAD5 = 0x65;
83        internal static readonly short VK_NUMPAD5NOTHING = 0x0C;
84        internal static readonly short VK_NUMPAD6 = 0x66;
85        internal static readonly short VK_NUMPAD7 = 0x67;
86        internal static readonly short VK_NUMPAD8 = 0x68;
87        internal static readonly short VK_NUMPAD9 = 0x69;
88
89        internal static readonly short KEYEVENTF_EXTENDEDKEY    = 0x0001;
90        internal static readonly short KEYEVENTF_KEYUP          = 0x0002;
91
92        internal static readonly int GWL_EXSTYLE    = -20;
93        internal static readonly int WS_DISABLED    = 0X8000000;
94        internal static readonly int WM_SETFOCUS    = 0X0007;
95    }

  屏幕鍵盤必須是一個不能獲得輸入焦點的窗體,在這個窗體的構造函數裡,可以安裝
一個全局鼠標鉤子,再通過調用 SetWindowLong API 函數完成。

  

  

  1UserActivityHook hook = new UserActivityHook(true, true);
2hook.MouseActivity += HookOnMouseActivity;
3
4private void HookOnMouseActivity(object sener, HookEx.MouseExEventArgs e) {
5    Point location = e.Location;
6
7    if (e.Button == MouseButtons.Left) {
8        Rectangle captionRect = new Rectangle(this.Location, new Size(this.Width,
9            SystemInformation.CaptionHeight));
10        if (captionRect.Contains(location)) {
11            NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE,
12                (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE)
13                 & (~KeyboardConstaint.WS_DISABLED));
14            NativeMethods.SendMessage(this.Handle, KeyboardConstaint.WM_SETFOCUS, IntPtr.Zero,

  IntPtr.Zero);
15        } else {
16            NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE,
17                (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) |
18                 KeyboardConstaint.WS_DISABLED);
19        }
20    }
21}

  鼠標單擊標題欄,讓屏幕鍵盤可以接收焦點,並激活,單擊其他部分則不激活窗體(如果激活了,其他程序必然取消激活,
輸入就無法進行了),這樣才可以進行輸入,並且保證了可以拖動窗體到其他位置。

  至此,一個屏幕鍵盤程序差不多完成了,能夠實現與實際鍵盤完全同步。至於窗體,按鍵重繪,以及 Num Lock, Caps Lock,
Scroll Lock 等鍵盤燈的模擬,這裡就不講了,如果有興趣,可以下載完整的代碼。

  說明:本程序參考了 Jeffrey Richter 先生的著作 CLR via C#, Second Edition, MSDN 以及一些網絡資料。

  這是微軟技術的一貫特點,使用簡單。但是如果要深入的話,還是要投入不少精力的


From:http://tw.wingwit.com/Article/program/net/201311/11360.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.