C# 键盘钩子类
键盘钩子类代码如下
class globalKeyboardHook { #region Constant, Structure and Delegate Definitions /// <summary> /// defines the callback type for the hook /// </summary> public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam); public struct keyboardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } ; const int WM_KEYDOWN = 0x100; const int WM_KEYUP = 0x101; const int WM_SYSKEYDOWN = 0x104; const int WM_SYSKEYUP = 0x105; #endregion #region Instance Variables /// <summary> /// The collections of keys to watch for /// </summary> //public List<Keys> HookedKeys = new List<Keys>(); /// <summary> /// Handle to the hook, need this to unhook and call the next hook /// </summary> IntPtr hhook = IntPtr.Zero; #endregion #region Events /// <summary> /// Occurs when one of the hooked keys is pressed /// </summary> public event KeyEventHandler KeyDown; /// <summary> /// Occurs when one of the hooked keys is released /// </summary> public event KeyEventHandler KeyUp; #endregion #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="globalKeyboardHook"/> class and installs the keyboard hook. /// </summary> public globalKeyboardHook() { hook(); } /// <summary> /// Releases unmanaged resources and performs other cleanup operations before the /// <see cref="globalKeyboardHook"/> is reclaimed by garbage collection and uninstalls the keyboard hook. /// </summary> ~globalKeyboardHook() { unhook(); } #endregion #region Public Methods /// <summary> /// Installs the global hook /// </summary> public void hook() { IntPtr hInstance = LoadLibrary("User32"); hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, ); } /// <summary> /// Uninstalls the global hook /// </summary> public void unhook() { UnhookWindowsHookEx(hhook); } /// <summary> /// The callback for the keyboard hook /// </summary> /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param> /// <param name="wParam">The event type</param> /// <param name="lParam">The keyhook event information</param> /// <returns></returns> public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) { ) { Keys key = (Keys)lParam.vkCode; //if (HookedKeys.Contains(key)) //{ KeyEventArgs kea = new KeyEventArgs(key); if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null)) { KeyDown(this, kea); } else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null)) { KeyUp(this, kea); } if (kea.Handled) ; //} } return CallNextHookEx(hhook, code, wParam, ref lParam); } #endregion #region DLL imports /// <summary> /// Sets the windows hook, do the desired event, one of hInstance or threadId must be non-null /// </summary> /// <param name="idHook">The id of the event you want to hook</param> /// <param name="callback">The callback.</param> /// <param name="hInstance">The handle you want to attach the event to, can be null</param> /// <param name="threadId">The thread you want to attach the event to, can be null</param> /// <returns>a handle to the desired hook</returns> [DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId); /// <summary> /// Unhooks the windows hook. /// </summary> /// <param name="hInstance">The hook handle that was returned from SetWindowsHookEx</param> /// <returns>True if successful, false otherwise</returns> [DllImport("user32.dll")] static extern bool UnhookWindowsHookEx(IntPtr hInstance); /// <summary> /// Calls the next hook. /// </summary> /// <param name="idHook">The hook id</param> /// <param name="nCode">The hook code</param> /// <param name="wParam">The wparam.</param> /// <param name="lParam">The lparam.</param> /// <returns></returns> [DllImport("user32.dll")] static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam); /// <summary> /// Loads the library. /// </summary> /// <param name="lpFileName">Name of the library</param> /// <returns>A handle to the library</returns> [DllImport("kernel32.dll")] static extern IntPtr LoadLibrary(string lpFileName); #endregion }
可用于记录键盘键入键值,代码如下
private void frm_Load(object sender, System.EventArgs e) { //gkh.HookedKeys.Add(Keys.A); //gkh.HookedKeys.Add(Keys.C); //gkh.HookedKeys.Add(Keys.B); //gkh.HookedKeys.Add(Keys.LControlKey); //gkh.HookedKeys.Add(Keys.Menu); gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); //gkh.KeyUp += new KeyEventHandler(gkh_KeyUp); } //private void gkh_KeyUp(object sender, KeyEventArgs e) //{ // k = k + e.KeyCode.ToString(); //} private void gkh_KeyDown(object sender, KeyEventArgs e) { k = k + e.KeyCode.ToString(); this.txt.Text = this.k; }
C# 键盘钩子类的更多相关文章
- C# 封装一个钩子类
利用C#设置钩子函数,并封装成类.如果想要实现全局钩子的话,必须将实现的代码封装到类库里. using System; using System.Collections.Generic; using ...
- c# 钩子类
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- CApiHook__Api钩子类
见过网上有很多ApiHook的类,但是都不尽入人意,要么就是写的不够好不够完善,要么就是跑不起来. 用别人写的代码总是有种不安心,所以自己就花了一晚上写了CApiHook类.已经尽量确保自己写的类是非 ...
- [转发]将Delphi的对象方法设为回调函数
心血来潮,为了实现更好的通用性和封装性,需要把类方法作为回调函数,搜得一篇好文,节选转发.命名似乎应该是MethodToCallback才合适,可惜调试时总是报错,debugging. 原文地址:ht ...
- 从普通函数到对象方法 ------Windows窗口过程的面向对象封装
原文地址:http://blog.csdn.net/linzhengqun/article/details/1451088 从普通函数到对象方法 ------Windows窗口过程的面向对象封装 开始 ...
- 将Delphi的对象方法设为回调函数
心血来潮,为了实现更好的通用性和封装性,需要把类方法作为回调函数,搜得一篇好文,节选转发.命名似乎应该是MethodToCallback才合适,可惜调试时总是报错,debugging. 原文地址:ht ...
- windows 勾子简介
近段时间因朋友催促让试着写一个监控系统,主要是用来管理孩子使用电脑,帮助孩子合理使用电脑.在网上查询了相关内容发现没有这方面的资料,所以只有自已来试试,要用到钩子来对windows应用程序进行监控,也 ...
- Windows消息拦截技术的应用
Windows消息拦截技术的应用 民航合肥空管中心 周毅 一.前 言 众所周知,Windows程式的运行是依靠发生的事件来驱动.换句话说,程式不断等待一个消息的发生,然后对这个消息的类型进行判断,再做 ...
- Windows消息拦截技术的应用(作者博客里有许多相关文章)
民航合肥空管中心 周毅 一.前 言 众所周知,Windows程式的运行是依靠发生的事件来驱动.换句话说,程式不断等待一个消息的发生,然后对这个消息的类型进行判断,再做适当的处理.处理完此次消息后又回到 ...
随机推荐
- Grunt - grunt-contrib-connect
快速搭建本地化服务 推荐加强版 : http://www.cnblogs.com/CyLee/p/5331055.html 首先要安装全局的grunt-cli sudo npm install gru ...
- Android消息处理机制
Android消息处理机制 Android应用程序消息处理机制(深入到native,实际由管道实现-pipe&epoll)
- 在archlinux上搭建twitter storm cluster
本文详细描述如何在archlinux上搭建twitter storm cluster,转载请注明出处,谢谢. 有关archlinux基本系统安装,请参照archlinux简明安装指南一文,下面以上述为 ...
- Mininet实验 基于Mininet实现BGP路径挟持攻击实验
参考:基于Mininet实现BGP路径挟持攻击实验 实验目的: 掌握如何mininet内模拟AS. 掌握BGP路径挟持的原理和分析过程. 实验原理: 互联网是由相互连接的自治系统AS组成的,通过一个通 ...
- 常用的API接口,返回JSON格式的服务API接口
物流接口 快递接口: http://www.kuaidi100.com/query?type=快递公司代号&postid=快递单号 ps:快递公司编码:申通="shentong&qu ...
- 语艺杂谈1 – MAP赋值与插入
MAP赋值和插入,对于相同ID的处理方式不同,前者为替换 后者为插入失败 #include <map> #include <string> #include <iostr ...
- the core or essence of a computer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...
- Python在windows下的安装与配置
安装python 文件准备: A. python安装文件:我用的是python-3.4.3.amd64.msi: 安装很简单,直接双击点下一步即可: 配置环境变量,在windows系统变量中找到pat ...
- addevent兼容函数 && 阻止默认行为 && 阻止传播
function addEvent(a, b, c, d) { a.addEventListener ? a.addEventListener(b, c, d) : a.attachEvent(&qu ...
- java JDK8 学习笔记——第13章 时间与日期
第十三章 时间与日期 13.1 认识时间与日期 13.1.1 时间的度量 1.格林威治标准时间GMT 格林威治标准时间的正午是太阳抵达天空最高点之时.现在已经不作为标准时间使用. 2.世界时UT世界时 ...