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

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


frmMain:

        public BarCodeHook BarCode = new BarCodeHook();
        public delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);
        void ShowInfo(BarCodeHook.BarCodes barCode){
         textBox_barCode.Text = barCode.BarCode;
         buttonX2.Focus();
        }
        public void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
        {
            ShowInfo(barCode);
        }


        public frmMain()
        {
            InitializeComponent();
            BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);

        }

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




 public 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;
        public string strBarCode = "";
        public int length;

        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            barCode.IsValid = false;
            bool notChar = false;
            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);
                    }
                    else
                    {
                        notChar = true;   //转到ascii字符失败,这不是一个正常字符,要去掉
                    }


                            if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 30)     //30ms可以过滤掉连续按住一个键时的情况
                            {
                                if (notChar == false)
                                    strBarCode = barCode.Chr.ToString();
                                else
                                    strBarCode = "";
                                barCode.IsValid = false;
                            }
                            else
                            {
                                if (strBarCode.Length >= 5)  
                                {
                                    barCode.IsValid = true;      //isValid为true表明这是个条码
                                }
                                if (notChar == false)
                                {
                                    strBarCode += barCode.Chr.ToString();
                                }
                                barCode.BarCode = strBarCode;
                            }


                    


                    barCode.Time = DateTime.Now;
                    if (BarCodeEvent != null && barCode.IsValid) BarCodeEvent(barCode);    //触发事件
                    
                }
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);          
        }
        
        // 安装钩子 
        public bool Start()
        {
            if (hKeyboardHook == 0)
            {
                //WH_KEYBOARD_LL = 13
                hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
            }
            return (hKeyboardHook != 0);
        }

        // 卸载钩子 
        public bool Stop()
        {
            if (hKeyboardHook != 0)
            {
                bool result = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = 0;         //将hKeyboardHook 置为0
                if (result)
                {

                    //MessageBox.Show("true");
                }
                return result;
            }
            
            return true;
        }
    }

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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值