用钩子(hook)实现C#的屏幕键盘效果

来源:互联网 发布:python help 用法 编辑:程序博客网 时间:2024/05/29 19:21

        要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活。因此需要一个全局的钩子,也就是系统范围的钩子。

什么是钩子(Hook)

       钩子(Hook)是Windows提供的一种消息处理机制平台,是指在程序正常运行中接受信息之前预先启动的函数,用来检查和修改传给该程序的信息,(钩子)实际上是一个处理消息的程序段,通过系统调用,把它挂入系统。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获该消息,亦即钩子函数先得到控制权。这时钩子函数即可以加工处理(改变)该消息,也可以不作处理而继续传递该消息,还可以强制结束消息的传递。注意:安装钩子函数将会影响系统的性能。监测“系统范围事件”的系统钩子特别明显。因为系统在处理所有的相关事件时都将调用您的钩子函数,这样您的系统将会明显的减慢。所以应谨慎使用,用完后立即卸载。还有,由于您可以预先截获其它进程的消息,所以一旦您的钩子函数出了问题的话必将影响其它的进程。

钩子的作用范围

       一共有两种范围(类型)的钩子,局部的和远程的。局部钩子仅钩挂自己进程的事件。远程的钩子还可以将钩挂其它进程发生的事件。远程的钩子又有两种: 基于线程的钩子将捕获其它进程中某一特定线程的事件。简言之,就是可以用来观察其它进程中的某一特定线程将发生的事件。系统范围的钩子将捕捉系统中所有进程将发生的事件消息。 

Hook 类型

       Windows共有14种Hooks,每一种类型的Hook可以使应用程序能够监视不同类型的系统消息处理机制。下面描述所有可以利用的Hook类型的发生时机。详细内容可以查阅MSDN,这里只介绍我们将要用到的两种类型的钩子。

(1)WH_KEYBOARD_LL Hook

WH_KEYBOARD_LL Hook监视输入到线程消息队列中的键盘消息。

(2)WH_MOUSE_LL Hook

WH_MOUSE_LL Hook监视输入到线程消息队列中的鼠标消息。

下面的 class 把 API 调用封装起来以便调用。

  1. // NativeMethods.cs
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Drawing;
  5. namespace CnBlogs.Youzai.ScreenKeyboard
  6. {
  7.     [StructLayout(LayoutKind.Sequential)]
  8.     internal struct MOUSEINPUT
  9.     {
  10.         public int dx;
  11.         public int dy;
  12.         public int mouseData;
  13.         public int dwFlags;
  14.         public int time;
  15.         public IntPtr dwExtraInfo;
  16.     }
  17.     [StructLayout(LayoutKind.Sequential)]
  18.     internal struct KEYBDINPUT
  19.     {
  20.         public short wVk;
  21.         public short wScan;
  22.         public int dwFlags;
  23.         public int time;
  24.         public IntPtr dwExtraInfo;
  25.     }
  26.     [StructLayout(LayoutKind.Explicit)]
  27.     internal struct Input
  28.     {
  29.         [FieldOffset(0)]
  30.         public int type;
  31.         [FieldOffset(4)]
  32.         public MOUSEINPUT mi;
  33.         [FieldOffset(4)]
  34.         public KEYBDINPUT ki;
  35.         [FieldOffset(4)]
  36.         public HARDWAREINPUT hi;
  37.     }
  38.     [StructLayout(LayoutKind.Sequential)]
  39.     internal struct HARDWAREINPUT
  40.     {
  41.         public int uMsg;
  42.         public short wParamL;
  43.         public short wParamH;
  44.     }
  45.     internal class INPUT
  46.     {
  47.         public const int MOUSE = 0;
  48.         public const int KEYBOARD = 1;
  49.         public const int HARDWARE = 2;
  50.     }
  51.     internal static class NativeMethods
  52.     {
  53.         [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  54.         internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
  55.         [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  56.         internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  57.         [DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
  58.         internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);
  59.         [DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
  60.         internal static extern int GetTickCount();
  61.         [DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
  62.         internal static extern short GetKeyState(int nVirtKey);
  63.         [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
  64.         internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  65.     }
  66. }

安装钩子

       使用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过程的进入点,线程标识符置为0,该Hook过程同系统中的所有线程关联。如果是安装局部Hook此时该Hook函数可以放置在DLL中,也可以放置在应用程序的模块段。在C#中通过平台调用(前文已经介绍过)来调用API函数。

  1. public void Start(bool installMouseHook, bool installKeyboardHook)
  2. {
  3.     if (hMouseHook == IntPtr.Zero && installMouseHook)
  4.     {
  5.         MouseHookProcedure = new HookProc(MouseHookProc);
  6.         hMouseHook = SetWindowsHookEx(
  7.                         WH_MOUSE_LL,
  8.                         MouseHookProcedure,
  9.                         Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
  10.         if (hMouseHook == IntPtr.Zero)
  11.         {
  12.             int errorCode = Marshal.GetLastWin32Error();
  13.             Stop(truefalsefalse);
  14.             throw new Win32Exception(errorCode);
  15.         }
  16.     }
  17.     if (hKeyboardHook == IntPtr.Zero && installKeyboardHook)
  18.     {
  19.         KeyboardHookProcedure = new HookProc(KeyboardHookProc);
  20.         //install hook
  21.         hKeyboardHook = SetWindowsHookEx(
  22.         WH_KEYBOARD_LL,
  23.         KeyboardHookProcedure,
  24.         Marshal.GetHINSTANCE(
  25.         Assembly.GetExecutingAssembly().GetModules()[0]),
  26.         0);
  27.         // If SetWindowsHookEx fails.
  28.         if (hKeyboardHook == IntPtr.Zero)
  29.         {
  30.             // Returns the error code returned by the last 
  31.             // unmanaged function called using platform invoke 
  32.             // that has the DllImportAttribute.SetLastError flag set. 
  33.             int errorCode = Marshal.GetLastWin32Error();
  34.             //do cleanup
  35.             Stop(falsetruefalse);
  36.             //Initializes and throws a new instance of the 
  37.             // Win32Exception class with the specified error. 
  38.             throw new Win32Exception(errorCode);
  39.         }
  40.     }
  41. }

使用完钩子后,要进行卸载,这个可以写在析构函数中。

  1. public void Stop()
  2. {
  3.     this.Stop(truetruetrue);
  4. }
  5. public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook,
  6.         bool throwExceptions)
  7. {
  8.     // if mouse hook set and must be uninstalled
  9.     if (hMouseHook != IntPtr.Zero && uninstallMouseHook)
  10.     {
  11.         // uninstall hook
  12.         bool retMouse = UnhookWindowsHookEx(hMouseHook);
  13.         // reset invalid handle
  14.         hMouseHook = IntPtr.Zero;
  15.         // if failed and exception must be thrown
  16.         if (retMouse == false && throwExceptions)
  17.         {
  18.             // Returns the error code returned by the last unmanaged function 
  19.             // called using platform invoke that has the DllImportAttribute.
  20.             // SetLastError flag set. 
  21.             int errorCode = Marshal.GetLastWin32Error();
  22.             // Initializes and throws a new instance of the Win32Exception class 
  23.             // with the specified error. 
  24.             throw new Win32Exception(errorCode);
  25.         }
  26.     }
  27.     // if keyboard hook set and must be uninstalled
  28.     if (hKeyboardHook != IntPtr.Zero && uninstallKeyboardHook)
  29.     {
  30.         // uninstall hook
  31.         bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
  32.         // reset invalid handle
  33.         hKeyboardHook = IntPtr.Zero;
  34.         // if failed and exception must be thrown
  35.         if (retKeyboard == false && throwExceptions)
  36.         {
  37.             // Returns the error code returned by the last unmanaged function 
  38.             // called using platform invoke that has the DllImportAttribute.
  39.             // SetLastError flag set. 
  40.             int errorCode = Marshal.GetLastWin32Error();
  41.             // Initializes and throws a new instance of the Win32Exception class 
  42.             // with the specified error. 
  43.             throw new Win32Exception(errorCode);
  44.         }
  45.     }
  46. }

将这个文件编译成一个dll,即可在应用程序中调用。通过它提供的事件,便可监听所有的键盘事件。
但是,这只能监听键盘事件,没有键盘的情况下,怎么会有键盘事件?其实很简单,通过SendInput
API函数提供虚拟键盘代码的调用即可模拟键盘输入。下面的代码模拟一个 KeyDown 和 KeyUp 过程,
把他们连接起来就是一次按键过程。

  1. private void SendKeyDown(short key)
  2. {
  3.     Input[] input = new Input[1];
  4.     input[0].type = INPUT.KEYBOARD;
  5.     input[0].ki.wVk = key;
  6.     input[0].ki.time = NativeMethods.GetTickCount();
  7.     if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
  8.     {
  9.         throw new Win32Exception(Marshal.GetLastWin32Error());
  10.     }
  11. }
  12. private void SendKeyUp(short key)
  13. {
  14.     Input[] input = new Input[1];
  15.     input[0].type = INPUT.KEYBOARD;
  16.     input[0].ki.wVk = key;
  17.     input[0].ki.dwFlags = KeyboardConstaint.KEYEVENTF_KEYUP;
  18.     input[0].ki.time = NativeMethods.GetTickCount();
  19.     if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])) < input.Length)
  20.     {
  21.         throw new Win32Exception(Marshal.GetLastWin32Error());
  22.     }
  23. }

      自己实现一个 KeyBoardButton 控件用作按钮,用 Visual Studio 或者 SharpDevelop 为屏幕键盘设计 UI,然后
在这些 Button 的 Click 事件里面模拟一个按键过程。

  1. private void ButtonOnClick(object sender, EventArgs e)
  2. {
  3.     KeyboardButton btnKey = sender as KeyboardButton;
  4.     if (btnKey == null)
  5.     {
  6.         return;
  7.     }
  8.     SendKeyCommand(btnKey);
  9. }
  10. private void SendKeyCommand(KeyboardButton keyButton)
  11. {
  12.     short key = keyButton.VKCode;
  13.     if (combinationVKButtonsMap.ContainsKey(key))
  14.     {
  15.         if (keyButton.Checked)
  16.         {
  17.             SendKeyUp(key);
  18.         }
  19.         else
  20.         {
  21.             SendKeyDown(key);
  22.         }
  23.     }
  24.     else
  25.     {
  26.         SendKeyDown(key);
  27.         SendKeyUp(key);
  28.     }
  29. }

其中 combinationVKButtonsMap 是一个 IDictionary>, key 存储的是VK_SHIFT, VK_CONTROL 等组合键的键盘码。左右两个按钮对应同一个键盘码,因此需要放在一个 List 里。
标准键盘上的每一个键都有虚拟键码( VK_CODE)与之对应。还有一些其他的常量,
把它写在一个静态 class 里吧。

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

屏幕键盘必须是一个不能获得输入焦点的窗体,在这个窗体的构造函数里,可以安装一个全局鼠标钩子,再通过调用 SetWindowLong API 函数完成。

  1. UserActivityHook hook = new UserActivityHook(truetrue);
  2. hook.MouseActivity += HookOnMouseActivity;
  3. private void HookOnMouseActivity(object sener, HookEx.MouseExEventArgs e)
  4. {
  5.     Point location = e.Location;
  6.     if (e.Button == MouseButtons.Left)
  7.     {
  8.         Rectangle captionRect = new Rectangle(this.Location, new Size(this.Width, SystemInformation.CaptionHeight));
  9.         if (captionRect.Contains(location))
  10.         {
  11.             NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE, (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) & (~KeyboardConstaint.WS_DISABLED));
  12.             NativeMethods.SendMessage(this.Handle, KeyboardConstaint.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
  13.         }
  14.         else
  15.         {
  16.             NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE, (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) | KeyboardConstaint.WS_DISABLED);
  17.         }
  18.     }
  19. }
  鼠标单击标题栏,让屏幕键盘可以接收焦点,并激活,单击其他部分则不激活窗体(如果激活了,其他程序必然取消激活,输入就无法进行了),这样才可以进行输入,并且保证了可以拖动窗体到其他位置。
 
  至此,一个屏幕键盘程序差不多完成了,能够实现与实际键盘完全同步。至于窗体,按键重绘,以及 Num Lock, Caps Lock,Scroll Lock 等键盘灯的模拟,这里就不讲了,如果有兴趣,可以下载完整的代码。
 
  说明:本程序参考了 Jeffrey Richter 先生的著作 CLR via C#, Second Edition, MSDN 以及一些网络资料。
 
  这是微软技术的一贯特点,使用简单。但是如果要深入的话,还是要投入不少精力的
原创粉丝点击