C#下usb条码扫描枪的钩子实现的改进

  【目前的条形码扫描器有点类似外接键盘(其实从消息传送上它就相当于一个键盘),把输入焦点定位到可输入的控件上,一扫描相应的条形码信息就输入到文本框中去了,但是如果没有输入焦点,或另一个不相干的程序获得输入焦点,那就有点乱套了。我想实现的是,不管什么情况,只要扫描器一工作,我的程序就能自动激活,并能获得当前输入的条形码信息。 实现思路:我用的是litele牌的USB口的红外条形码扫描器,仔细分析了一下,扫描成功后,以键盘按键消息的形式把条形码输入信息通知给系统。这样通过键盘钩子就可以方便的获得该信息了。但是,怎样区分信息是键盘还是条形码输入的哪?很简单,条形码扫描器在很短的时间内输入了至少3个字符以上信息,并且以“回车”作为结束字符,在这种思想指引下,很完美的实现了预定功能。】


frmMain:

  1. public BarCodeHook BarCode = new BarCodeHook();
  2. public delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);
  3. void ShowInfo(BarCodeHook.BarCodes barCode){
  4. textBox_barCode.Text = barCode.BarCode;
  5. buttonX2.Focus();
  6. }
  7. public void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
  8. {
  9. ShowInfo(barCode);
  10. }
  11. public frmMain()
  12. {
  13. InitializeComponent();
  14. BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);
  15. }

如果焦点本来就在textBox上,会产生多余的字符,所以在showInfo函数里,每次都手动让buttonX2成为焦点。




  1. public class BarCodeHook
  2. {
  3. public delegate void BarCodeDelegate(BarCodes barCode);
  4. public event BarCodeDelegate BarCodeEvent;
  5. public struct BarCodes
  6. {
  7. public int VirtKey; //虚拟码
  8. public int ScanCode; //扫描码
  9. public string KeyName; //键名
  10. public uint AscII; //AscII
  11. public char Chr; //字符
  12. public string BarCode; //条码信息
  13. public bool IsValid; //条码是否有效
  14. public DateTime Time; //扫描时间
  15. }
  16. private struct EventMsg
  17. {
  18. public int message;
  19. public int paramL;
  20. public int paramH;
  21. public int Time;
  22. public int hwnd;
  23. }
  24. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  25. private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  26. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  27. private static extern bool UnhookWindowsHookEx(int idHook);
  28. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  29. private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
  30. [DllImport("user32", EntryPoint = "GetKeyNameText")]
  31. private static extern int GetKeyNameText(int lParam, StringBuilder lpBuffer, int nSize);
  32. [DllImport("user32", EntryPoint = "GetKeyboardState")]
  33. private static extern int GetKeyboardState(byte[] pbKeyState);
  34. [DllImport("user32", EntryPoint = "ToAscii")]
  35. private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeyState, ref uint lpChar, int uFlags);
  36. delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
  37. BarCodes barCode = new BarCodes();
  38. int hKeyboardHook = 0;
  39. public string strBarCode = "";
  40. public int length;
  41. private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
  42. {
  43. barCode.IsValid = false;
  44. bool notChar = false;
  45. if (nCode == 0)
  46. {
  47. EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));
  48. if (wParam == 0x100) //WM_KEYDOWN = 0x100
  49. {
  50. barCode.VirtKey = msg.message & 0xff; //虚拟码
  51. barCode.ScanCode = msg.paramL & 0xff; //扫描码
  52. StringBuilder strKeyName = new StringBuilder(255);
  53. if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0)
  54. {
  55. barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
  56. }
  57. else
  58. {
  59. barCode.KeyName = "";
  60. }
  61. byte[] kbArray = new byte[256];
  62. uint uKey = 0;
  63. GetKeyboardState(kbArray);
  64. if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0))
  65. {
  66. barCode.AscII = uKey;
  67. barCode.Chr = Convert.ToChar(uKey);
  68. }
  69. else
  70. {
  71. notChar = true; //转到ascii字符失败,这不是一个正常字符,要去掉
  72. }
  73. if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 30) //30ms可以过滤掉连续按住一个键时的情况
  74. {
  75. if (notChar == false)
  76. strBarCode = barCode.Chr.ToString();
  77. else
  78. strBarCode = "";
  79. barCode.IsValid = false;
  80. }
  81. else
  82. {
  83. if (strBarCode.Length >= 5)
  84. {
  85. barCode.IsValid = true; //isValid为true表明这是个条码
  86. }
  87. if (notChar == false)
  88. {
  89. strBarCode += barCode.Chr.ToString();
  90. }
  91. barCode.BarCode = strBarCode;
  92. }
  93. barCode.Time = DateTime.Now;
  94. if (BarCodeEvent != null && barCode.IsValid) BarCodeEvent(barCode); //触发事件
  95. }
  96. }
  97. return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
  98. }
  99. // 安装钩子
  100. public bool Start()
  101. {
  102. if (hKeyboardHook == 0)
  103. {
  104. //WH_KEYBOARD_LL = 13
  105. hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
  106. }
  107. return (hKeyboardHook != 0);
  108. }
  109. // 卸载钩子
  110. public bool Stop()
  111. {
  112. if (hKeyboardHook != 0)
  113. {
  114. bool result = UnhookWindowsHookEx(hKeyboardHook);
  115. hKeyboardHook = 0; //将hKeyboardHook 置为0
  116. if (result)
  117. {
  118. //MessageBox.Show("true");
  119. }
  120. return result;
  121. }
  122. return true;
  123. }
  124. }

是如果扫到的是英文字符的话,会有一个多余的码无法从键盘码转到ascii码,需要去掉这个码。同时设为30ms可以过滤掉一直按住一个键的情况。