using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; namespace **.Forms { /// <summary> /// 获取键盘输入或者USB扫描枪数据 可以是没有焦点 应为使用的是全局钩子 /// USB扫描枪 是模拟键盘按下 /// 这里主要处理扫描枪的值,手动输入的值不太好处理 /// </summary> public class BardCodeHook { public delegate void BardCodeDeletegate(BarCodes barCode); public event BardCodeDeletegate BarCodeEvent; //定义成静态,这样不会抛出回收异常 private static HookProc hookproc; public struct BarCodes { public int VirtKey;//虚拟吗 public int ScanCode;//扫描码 public string KeyName;//键名 public uint Ascll;//Ascll public char Chr;//字符 public string OriginalChrs; //原始 字符 public string OriginalAsciis;//原始 ASCII public string OriginalBarCode; //原始数据条码 public bool IsValid;//条码是否有效 public DateTime Time;//扫描时间, public string BarCode;//条码信息 保存最终的条码 } private struct EventMsg { public int message; public int paramL; public int paramH; public int Time; public int hwnd; } #region DllImport [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 IParam, 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[] lpKeySate, ref uint lpChar, int uFlags); [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string name); #endregion delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); BarCodes barCode = new BarCodes(); int hKeyboardHook = 0; StringBuilder sbBarCode = new StringBuilder(); private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { int i_calledNext = -10; 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(225); if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0) { barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '