代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
无焦点下获取条码枪返回值的Hook(改良版)
作者:
我叫李冠男
/ 发布于
2013/12/26
/
618
此版本是改良过网络上流行的代码的 1.去除了.net4.0的MDA风险; 2.修正了原有代码中,扫描出的内容有问题部分
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Reflection; namespace BookLibraryManagement.CommonTools { class BarCodeHook { public delegate void BarCodeDelegate(BarCodes barCode); public event BarCodeDelegate BarCodeEvent; public struct BarCodes { public int VirtKey; //虚拟码 public int ScanCode; //扫描码 public string KeyName; //键名 public uint AscII; //AscII public char Chr; //字符 public string BarCode; //条码信息 public bool IsValid; //条码是否有效 public DateTime Time; //扫描时间 } private struct EventMsg { public int message; public int paramL; public int paramH; public int Time; public int hwnd; } [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); [DllImport("user32", EntryPoint = "GetKeyNameText")] private static extern int GetKeyNameText(int lParam, StringBuilder lpBuffer, int nSize); [DllImport("user32", EntryPoint = "GetKeyboardState")] private static extern int GetKeyboardState(byte[] pbKeyState); [DllImport("user32", EntryPoint = "ToAscii")] private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeyState, ref uint lpChar, int uFlags); delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); BarCodes barCode = new BarCodes(); int hKeyboardHook = 0; //此处使用char List 避免了原有代码中扫描出的结果是乱码的情况 List<char> _barcode = new List<char>(100); private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { if (nCode == 0) { EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg)); if (wParam == 0x100) //WM_KEYDOWN = 0x100 { barCode.VirtKey = msg.message & 0xff; //虚拟码 barCode.ScanCode = msg.paramL & 0xff; //扫描码 StringBuilder strKeyName = new StringBuilder(255); if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0) { barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' }); } else { barCode.KeyName = ""; } byte[] kbArray = new byte[256]; uint uKey = 0; GetKeyboardState(kbArray); if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0)) { barCode.AscII = uKey; barCode.Chr = Convert.ToChar(uKey); } if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 100) { _barcode.Clear(); } else { if ((msg.message & 0xff) == 13 && _barcode.Count > 0) //回车 { barCode.BarCode = new String(_barcode.ToArray()); barCode.IsValid = true; } _barcode.Add(Convert.ToChar(msg.message & 0xff)); } barCode.Time = DateTime.Now; if (BarCodeEvent != null) BarCodeEvent(barCode); //触发事件 barCode.IsValid = false; } } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } //增加了一个静态变量,放置GC将钩子回收掉 private static HookProc hookproc; // 安装钩子 public bool Start() { if (hKeyboardHook == 0) { hookproc = new HookProc(KeyboardHookProc); //WH_KEYBOARD_LL = 13 hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); } return (hKeyboardHook != 0); } // 卸载钩子 public bool Stop() { if (hKeyboardHook != 0) { return UnhookWindowsHookEx(hKeyboardHook); } return true; } } }
试试其它关键字
获取条码枪
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
我叫李冠男
贡献的其它代码
(
2
)
.
通过双缓冲减少闪烁
.
无焦点下获取条码枪返回值的Hook(改良版)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3