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程式的运行是依靠发生的事件来驱动.换句话说,程式不断等待一个消息的发生,然后对这个消息的类型进行判断,再做适当的处理.处理完此次消息后又回到 ...
随机推荐
- 关于如何用php 获取当前脚本的url
关于用php 获取当前脚本的url很多朋友会说很简单,但是要获取很详细的就要经过多次判断哦. $PHP_TIME = time();$PHP_SELF = isset($_SERVER['PHP_SE ...
- [转]ASP.NET会话(Session)保存模式
本文转自:http://blog.csdn.net/cityhunter172/article/details/727743 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦 ...
- 使用Eclipse开发Java Web过程中Debug调试的使用方法
里介绍的是在Eclipse中的Debug调试. 首先右击项目选择Debug As -- Debug on Server 或者点击Server面板的小昆虫图标,启动Debug模式. 运行web项目,进行 ...
- git 使用钩子直接推送到工作目录
远端机器 $ mkdir /www/teacherapi # 创建工作目录 $ cd /data/git $ git init teacherapi.git --bare --shared Init ...
- Unity3D之游戏架构脚本该如何来写(转)
这篇文章主要想大家说明一下我在Unity3D游戏开发中是如何写游戏脚本的,对于Unity3D这套游戏引擎来说入门极快,可是要想做好却非常的难.这篇文章的目的是让哪些已经上手Unity3D游戏引擎的朋友 ...
- wordpress自定义数据库出错页面
wordpress数据连接出错时,会有一个空白页面,有一行字:数据连接错误.这样当然不美观,好在这个页面是可以自定义的. 在/wp-content/目录下创建'db-error.php'文件,当数据库 ...
- wordpress the_date 方法 偶尔为空的问题
估计很多人遇到这个问题: 一来是the_title(),the_permalink(),the_date()一路用下来,很正常也很正确 不爱读官方文档,因为文档中有个特别提示 文档:http://co ...
- memcached学习笔记5--socke操作memcached 缓存系统
使用条件:当我们没有权限或者不能使用服务器的时候,我们需要用socket操作memcached memcached-client操作 特点: 无需开启memcache扩展 使用fsocketopen( ...
- Java 判断图片资源的存在否
question: 如题,举个例子吧 String image ="http://info-database.csdn.net/Upload/2010-10-30/735-60sap1030 ...
- 【转】javascript中this的四种用法
在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this 出现的场景 ...