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

本文介绍了Windows中的钩子(Hook)机制,特别是WH_KEYBOARD_LL和WH_MOUSE_LL钩子,用于监听键盘和鼠标消息。通过C#编程,展示了如何设置和卸载钩子,以及如何模拟键盘输入。文章还提供了使用NativeMethods类封装的API调用来实现屏幕键盘功能。
摘要由CSDN通过智能技术生成
要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活。因此需要一个全局的钩子,也就是系统范围的钩子。

什么是钩子(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. }

安装钩子

       使用SetWindowsHoo

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值