Csharp/C#教程:C#使用钩子获得按键信息的方法分享

本文实例讲述了C#使用钩子获得按键信息的方法。分享给大家供大家参考。具体如下:

窗体相关代码:

usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Runtime.InteropServices; usingSystem.Threading; usingReadBadCode; namespacegouzi { publicpartialclassForm2:Form { BarCodeHookBarCode=newBarCodeHook(); publicForm2() { InitializeComponent(); BarCode.BarCodeEvent+=newBarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent); } privatedelegatevoidShowInfoDelegate(BarCodeHook.BarCodesbarCode); privatevoidShowInfo(BarCodeHook.BarCodesbarCode) { if(this.InvokeRequired) { this.BeginInvoke(newShowInfoDelegate(ShowInfo),newobject[]{barCode}); } else { textBox1.Text=barCode.KeyName;//键名 textBox2.Text=barCode.VirtKey.ToString();//虚拟码 textBox3.Text=barCode.ScanCode.ToString();//扫描码 textBox4.Text=barCode.AscII.ToString();//AscII textBox5.Text=barCode.Chr.ToString();//字符 textBox6.Text=barCode.IsValid?barCode.BarCode:""; //在这里进行键入值 } } voidBarCode_BarCodeEvent(BarCodeHook.BarCodesbarCode) { ShowInfo(barCode); } privatevoidForm2_Load(objectsender,EventArgse) { BarCode.Start(); } privatevoidForm2_StyleChanged(objectsender,EventArgse) { BarCode.Stop(); } } }

后台类代码:

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Runtime.InteropServices; usingSystem.Reflection; namespaceReadBadCode { classBarCodeHook { publicdelegatevoidBarCodeDelegate(BarCodesbarCode); publiceventBarCodeDelegateBarCodeEvent; publicstructBarCodes { publicintVirtKey;//虚拟码 publicintScanCode;//扫描码 publicstringKeyName;//键名 publicuintAscII;//AscII publiccharChr;//字符 publicstringBarCode;//条码信息 publicboolIsValid;//条码是否有效 publicDateTimeTime;//扫描时间 } privatestructEventMsg { publicintmessage; publicintparamL; publicintparamH; publicintTime; publicinthwnd; } //安装钩子 [DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] privatestaticexternintSetWindowsHookEx(intidHook,HookProclpfn,IntPtrhInstance,intthreadId); //卸载钩子 [DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] privatestaticexternboolUnhookWindowsHookEx(intidHook); //继续下一个钩子 [DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] privatestaticexternintCallNextHookEx(intidHook,intnCode,Int32wParam,IntPtrlParam); //获取键名的字符串 [DllImport("user32",EntryPoint="GetKeyNameText")] privatestaticexternintGetKeyNameText(intlParam,StringBuilderlpBuffer,intnSize); //将256个虚拟键复制到指定的缓冲区中 [DllImport("user32",EntryPoint="GetKeyboardState")] privatestaticexternintGetKeyboardState(byte[]pbKeyState); //将指定的虚拟键码和键盘状态为相应的字符串 [DllImport("user32",EntryPoint="ToAscii")] privatestaticexternboolToAscii(intVirtualKey,intScanCode,byte[]lpKeyState,refuintlpChar,intuFlags); //声明定义回调函数 delegateintHookProc(intnCode,Int32wParam,IntPtrlParam); BarCodesbarCode=newBarCodes(); inthKeyboardHook=0; stringstrBarCode=""; privateintKeyboardHookProc(intnCode,Int32wParam,IntPtrlParam) { if(nCode==0) { EventMsgmsg=(EventMsg)Marshal.PtrToStructure(lParam,typeof(EventMsg)); if(wParam==0x100)//WM_KEYDOWN=0x100 { barCode.VirtKey=msg.message&0xff;//虚拟码 barCode.ScanCode=msg.paramL&0xff;//扫描码 StringBuilderstrKeyName=newStringBuilder(255); if(GetKeyNameText(barCode.ScanCode*65536,strKeyName,255)>0) { barCode.KeyName=strKeyName.ToString().Trim(newchar[]{'',''}); } else { barCode.KeyName=""; } byte[]kbArray=newbyte[256]; uintuKey=0; GetKeyboardState(kbArray); if(ToAscii(barCode.VirtKey,barCode.ScanCode,kbArray,refuKey,0)) { barCode.AscII=uKey; barCode.Chr=Convert.ToChar(uKey); } if(DateTime.Now.Subtract(barCode.Time).TotalMilliseconds>50) { strBarCode=barCode.Chr.ToString(); } else { if((msg.message&0xff)==13&&strBarCode.Length>3) //回车 { barCode.BarCode=strBarCode; barCode.IsValid=true; } strBarCode+=barCode.Chr.ToString(); } barCode.Time=DateTime.Now; if(BarCodeEvent!=null)BarCodeEvent(barCode); //触发事件 barCode.IsValid=false; } } returnCallNextHookEx(hKeyboardHook,nCode,wParam,lParam); } //安装钩子 publicboolStart() { if(hKeyboardHook==0) { //WH_KEYBOARD_LL=13 hKeyboardHook=SetWindowsHookEx(13,newHookProc(KeyboardHookProc),Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0); } return(hKeyboardHook!=0); } //卸载钩子 publicboolStop() { if(hKeyboardHook!=0) { returnUnhookWindowsHookEx(hKeyboardHook); } returntrue; } } }

【注意】要想测试实际的效果,必须执行编译后的Exe文件,在开发环境直接运行会没有效果的。

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:C#实现的鼠标钩子C#键盘鼠标钩子实例基于C#实现的HOOK键盘钩子实例代码C#Hook钩子实例代码截取键盘输入c#钩子学习笔记C#WinForm捕获未处理的异常实例解析C#Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法C#通过接口与线程通信(捕获线程状态)示例代码C#WinForm捕获全局变量异常SamWang解决方法C#实现可捕获几乎所有键盘鼠标事件的钩子类完整实例

标签: 方法

C++中this指针用法详解及实例

C语言中的参数传递机制详解

上述就是C#学习教程:C#使用钩子获得按键信息的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906409.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐